码迷,mamicode.com
首页 > 编程语言 > 详细

java日常问题和技巧2

时间:2020-04-23 21:13:11      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:pac   private   12px   实体类   map   json   索引数据   alibaba   格式   

整理到一个工具类里面,用到了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;
}

/**
* Objectlist
*/
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;
}


/**
* 获取某天开始时间戳,000000
*/
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;
}

/**
* 获取某天开始时间戳,000000
*/
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;
}

/**
* Objectdouble
*
*/
private Double object2Double(Object obj) {
if (obj != null) {
return Double.parseDouble(obj.toString());
}
return 0.00;
}

/**
* Objectint
*/
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);
}


}

java日常问题和技巧2

标签:pac   private   12px   实体类   map   json   索引数据   alibaba   格式   

原文地址:https://www.cnblogs.com/lgqrlchinese/p/12763075.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!