java日常问题和技巧2
整理到一个工具类里面,用到了spring和alibaba.fastjson
package common; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import org.springframework.cglib.beans.BeanMap; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; /** * 〈一句话功能简述〉 * 〈功能详细描述〉 * * @author 赵学壮 * @date 2020/4/22-16:14 * @since [v1.0] */ public class Utils { /** * 将list拆分成多给指定的大小的list */ public static <T> List<List<T>> createList(List<T> target, int size) { List<List<T>> listArr = new ArrayList<List<T>>(); //获取被拆分的数组个数 int arrSize = target.size() % size == 0 ? target.size() / size : target.size() / size + 1; for (int i = 0; i < arrSize; i++) { List<T> sub = new ArrayList<T>(); //把指定索引数据放入到list中 for (int j = i * size; j <= size * (i + 1) - 1; j++) { if (j <= target.size() - 1) { sub.add(target.get(j)); } } listArr.add(sub); } return listArr; } /** * 将Object转list */ public static <T> List<T> castList(Object obj, Class<T> clazz) { List<T> result = new ArrayList<T>(); if (obj instanceof List<?>) { for (Object o : (List<?>) obj) { result.add(clazz.cast(o)); } return result; } return null; } /** * 将map转成实体类 */ public static <T> T mapToBean(Map<String, Object> map, T bean) { BeanMap beanMap = BeanMap.create(bean); beanMap.putAll(map); return bean; } /** * 获取某天开始时间戳,0点0分0秒000 */ public static Long date2StartTimeStamp(String date_str, String format) throws ParseException { long timeStart = 0; timeStart = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").parse(date_str+" 00:00:00:000").getTime(); return timeStart; } /** * 获取某天开始时间戳,0点0分0秒000 */ public static Long date2EndTimeStamp(String date_str, String format) throws ParseException { long timeStart = 0; timeStart = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").parse(date_str+" 23:59:59:999").getTime(); return timeStart; } /** * 按格式获取当前时间获取时间 */ public static String getDateString(String format) { Date date = new Date(System.currentTimeMillis()); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); String processCd = simpleDateFormat.format(date); return processCd; } /** * Object转double * */ private Double object2Double(Object obj) { if (obj != null) { return Double.parseDouble(obj.toString()); } return 0.00; } /** * Object转int */ private int object2Int(Object obj) { if (obj != null) { return Integer.parseInt(obj.toString()); } return 0; } /** * 其他常用基本方法 */ public static void otherFunction(String format) { //List<Object>转List<String> List<Object> objectList = new ArrayList<Object>(); List<String> stringList = (List<String>)(List)objectList; //将List<String>转成按某字符拼接成字符串 String stringList2String = String.join(",", stringList); //将字符串按照某字符拆分成List<String> List<String> string2StringList = Arrays.asList(stringList2String.split(",")); //String[]转List<String> List<String> stringList1 = Arrays.asList(new String[]{"str1","str2"}); //此时,stringList1不能使用add方法,会报错Exception in thread "main" java.lang.UnsupportedOperationException,要进行以下处理 List<String> stringList2 = new ArrayList<>(Arrays.asList(new String[]{"str1","str2"})); //将json中对应字段转成实体类 JSONObject json = new JSONObject(); Bean processInfoBean = JSONObject.parseObject(json.toJSONString(), Bean.class); } }
package common;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.*;import org.springframework.cglib.beans.BeanMap;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject;/** * 〈一句话功能简述〉* 〈功能详细描述〉* * @author 赵学壮* @date 2020/4/22-16:14 * @since [v1.0] */public class Utils {/** * 将list拆分成多给指定的大小的list */public static <T> List<List<T>> createList(List<T> target, int size) { List<List<T>> listArr = new ArrayList<List<T>>();//获取被拆分的数组个数int arrSize = target.size() % size == 0 ? target.size() / size : target.size() / size + 1;for (int i = 0; i < arrSize; i++) { List<T> sub = new ArrayList<T>();//把指定索引数据放入到list中for (int j = i * size; j <= size * (i + 1) - 1; j++) {if (j <= target.size() - 1) { sub.add(target.get(j)); } } listArr.add(sub); }return listArr; }/** * 将Object转list */public static <T> List<T> castList(Object obj, Class<T> clazz) { List<T> result = new ArrayList<T>();if (obj instanceof List<?>) {for (Object o : (List<?>) obj) { result.add(clazz.cast(o)); }return result; }return null; }/** * 将map转成实体类*/public static <T> T mapToBean(Map<String, Object> map, T bean) { BeanMap beanMap = BeanMap.create(bean); beanMap.putAll(map);return bean; }/** * 获取某天开始时间戳,0点0分0秒000 */public static Long date2StartTimeStamp(String date_str, String format) throws ParseException {long timeStart = 0; timeStart = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").parse(date_str+" 00:00:00:000").getTime();return timeStart; }/** * 获取某天开始时间戳,0点0分0秒000 */public static Long date2EndTimeStamp(String date_str, String format) throws ParseException {long timeStart = 0; timeStart = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").parse(date_str+" 23:59:59:999").getTime();return timeStart; }/** * 按格式获取当前时间获取时间*/public static String getDateString(String format) { Date date = new Date(System.currentTimeMillis()); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); String processCd = simpleDateFormat.format(date);return processCd; }/** * Object转double * */private Double object2Double(Object obj) {if (obj != null) {return Double.parseDouble(obj.toString()); }return 0.00; }/** * Object转int */private int object2Int(Object obj) {if (obj != null) {return Integer.parseInt(obj.toString()); }return 0; }/** * 其他常用基本方法*/public static void otherFunction(String format) {//List<Object>转List<String>List<Object> objectList = new ArrayList<Object>(); List<String> stringList = (List<String>)(List)objectList;//将List<String>转成按某字符拼接成字符串String stringList2String = String.join(",", stringList);//将字符串按照某字符拆分成List<String>List<String> string2StringList = Arrays.asList(stringList2String.split(","));//String[]转List<String>List<String> stringList1 = Arrays.asList(new String[]{"str1","str2"});//此时,stringList1不能使用add方法,会报错Exception in thread "main" java.lang.UnsupportedOperationException,要进行以下处理List<String> stringList2 = new ArrayList<>(Arrays.asList(new String[]{"str1","str2"}));//将json中对应字段转成实体类JSONObject json = new JSONObject(); Bean processInfoBean = JSONObject.parseObject(json.toJSONString(), Bean.class); }}
相关推荐
cxcxrs 2020-06-26
zuixin 2020-02-01
vivenwan 2020-01-31
秋风瑟瑟 2019-10-28
秋风瑟瑟 2019-10-21
辛苦的字幕君小样 2011-07-01
mbcsdn 2019-07-01
xiaozaq 2019-06-29
pythonxuexi 2019-06-28
夫子与歌 2016-08-23
zhouguizhi 2011-08-11
80357518 2018-10-24
成长之路 2018-10-18
jinzhentao 2017-04-07
84533871 2015-11-19
wgPython 2019-04-20
87407607 2013-12-23
csssy00 2016-07-12