栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

DateUtil

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

DateUtil

我们要使用StringUtil工具类,所以首先导入;


            org.apache.commons
            commons-lang3
            3.1
 	
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;


public class DateUtil {
    
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    
    public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
    
    public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    
    public static final String CN_DATE_FORMAT = "yyyy年MM月dd日";

//   1. 时间转换String
    
    public static String dateToString(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }
    
    public static String dateToString(long date ,String pattern){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date nowDate = new Date(date * 1000);
        return sdf.format(nowDate);
    }

//    2. String转时间
    
    public static Date StringToDate(String date ,String pattern){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date nowDate = new Date();
        try{
            nowDate =  sdf.parse(date);
        } catch (Exception e){
            e.printStackTrace();
        }
        return nowDate;
    }

    
    public static long StringToUnixLong(String date){
        Date nowDate = StringToDate(date, "");
        Timestamp timestamp = new Timestamp(nowDate.getTime());
        return timestamp.getTime() / 1000;
    }
//    3. 获取当前时间
    
    public static String getDate(String pattern){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return  sdf.format(new Date());
    }

    
    public static long getUnixLong(){
        return System.currentTimeMillis();
    }

// 4. 日期计算
    // 4.1 日期分割算法 - 根据指定天数进行分割
    public static List> splitDateList (String startDateStr, String endDateStr, int dayNum, String pattern) throws Exception{
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        if(dayNum < 1){ return null; }
        List> dateList = new ArrayList>();
        //时间转换
        Date startDate = df.parse(startDateStr);
        Date endDate = df.parse(endDateStr);
        //创建日历
        Calendar cal = Calendar.getInstance();
        //设置时间
        cal.setTime(startDate);
        while(true){
            Map dateMap = new HashMap();
            //遍历设置天数
            cal.add(Calendar.DATE, dayNum-1);
            if(cal.getTime().getTime() < endDate.getTime()){
                dateMap.put("startDate", df.format(startDate));
                dateMap.put("endDate", df.format(cal.getTime()));
                dateList.add(dateMap);
            }else{
                dateMap.put("startDate", df.format(startDate));
                dateMap.put("endDate", df.format(endDate));
                dateList.add(dateMap);
                break;
            }
            //重新设置天数
            cal.add(Calendar.DATE, 1);
            startDate = cal.getTime();
        }
        return dateList;
    }




}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/425367.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号