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);    }}

相关推荐