commons lang3 日期处理

DateUtils常用方法

package sms;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Date_02 {
    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        Date now = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        //获取本星期第1天
        cal.setTime(now);
        int wich = cal.get(Calendar.DAY_OF_WEEK);
        System.out.println(wich);
        cal.add(Calendar.WEEK_OF_YEAR, -1);
        Date time = cal.getTime();

        System.out.println(sdf.format(now));
        /*
        2020-03-01 14:11:15.420
        2020-03-01 00:00:00.000
        2020-03-01 00:00:00.000
        2020-03-01 14:00:00.000
        2020-03-01 14:11:00.000
         */
        System.out.println(sdf.format(DateUtils.truncate(now, Calendar.MONTH)));
        System.out.println(sdf.format(DateUtils.truncate(now, Calendar.DAY_OF_MONTH)));
        System.out.println(sdf.format(DateUtils.truncate(now, Calendar.HOUR)));
        System.out.println(sdf.format(DateUtils.truncate(now, Calendar.MINUTE)));

        //格式化日期
        String str = DateFormatUtils.format(now, "yyyyMMdd");
        System.out.println(str);
        System.out.println(JSONObject.toJSONString(time));
    }
}
posted @ 2020-03-01 14:15  没有理由不会呀  阅读(1501)  评论(0编辑  收藏  举报