public static void main(String[] args) {
String dateStr = "2021-05-25 22:33:23";
Date date = DateUtil.parse(dateStr);
//减3毫秒
DateUtils.addMilliseconds(date, -3);
//DateUtils.date(date, 14, -3);
//减3秒
DateUtils.addSeconds(date, -3);
//DateUtils.date(date, 13, -3);
//减3分钟
DateUtils.addMinutes(date, -3);
//DateUtils.date(date, 12, -3);
//减3小时
DateUtils.addHours(date, -3);
//DateUtils.date(date, 11, -3);
//减3天
DateUtils.addDays(date, -3);
//DateUtils.date(date, 5, -3);
//减3周
DateUtils.addWeeks(date, -3);
//DateUtils.date(date, 3, -3);
//减3月
DateUtils.addMonths(date, -3);
//DateUtils.date(date, 2, -3);
//减3年
DateUtils.addYears(date, -3);
//DateUtils.date(date, 1, -3);
//原文链接:https://blog.csdn.net/qq_38974638/article/details/117264363
}
String dateStr = "2021-05-25 22:33:23";
Date date = DateUtil.parse(dateStr);
//减3毫秒
DateUtil.offsetMillisecond(date, -3);
DateUtil.offset(date, DateField.MILLISECOND, -3);
//减3秒
DateUtil.offsetSecond(date, -3);
DateUtil.offset(date, DateField.SECOND, -3);
//减3分钟
DateUtil.offsetMinute(date, -3);
DateUtil.offset(date, DateField.MINUTE, -3);
//减3小时
DateUtil.offsetHour(date, -3);
DateUtil.offset(date, DateField.HOUR_OF_DAY, -3);
//减3天
DateUtil.offsetDay(date, -3);
DateUtil.offset(date, DateField.DAY_OF_YEAR, -3);
//减3周
DateUtil.offsetWeek(date, -3);
DateUtil.offset(date, DateField.WEEK_OF_YEAR, -3);
//减3月
DateUtil.offsetMonth(date, -3);
DateUtil.offset(date, DateField.MONTH, -3);
//减3年
DateUtil.offset(date, DateField.YEAR, -3);
//昨天
DateUtil.yesterday();
//明天
DateUtil.tomorrow();
//上周
DateUtil.lastWeek();
//下周
DateUtil.nextWeek();
//上个月
DateUtil.lastMonth();
//下个月
DateUtil.nextMonth();
//原文链接:https://blog.csdn.net/qq_38974638/article/details/117264363
Date startDate=DateUtil.parse("2021-04-20 02:00:00");
Date endDate=DateUtil.parse("2021-04-21 05:10:00");
//相差周数
long week=DateUtil.between(startDate,endDate,DateUnit.WEEK);
//相差天数
long day=DateUtil.between(startDate,endDate,DateUnit.DAY);
//相差小时数
long hour=DateUtil.between(startDate,endDate,DateUnit.HOUR);
//相差分钟数
long minute=DateUtil.between(startDate,endDate,DateUnit.MINUTE);
//相差秒数
long second=DateUtil.between(startDate,endDate,DateUnit.SECOND);
//相差毫秒数
long millis=DateUtil.between(startDate,endDate,DateUnit.MS);
// 把领导绩效考评划分为定性考评 定量考评的依据是 考评方式
//领导者善于洞察和满足下属的需要 激发其实现目标的动机,保持其工作的热情。者体现了领导者的 激励能力;
String format = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
String formata = DateUtil.format(new Date(), "yyyyMMddHHmmss");
System.out.println(formata+"---format---"+format);
long minutes=DateUtil.between(DateUtil.parse("2022-07-28 18:23:45"),new Date(),DateUnit.MINUTE);
System.out.println("---format---"+minutes);
}
public void testBeginOfYear(){
//beginOfYear() 获取一年最开始的时间
System.out.println(DateUtil.beginOfYear(new Date()));
//beginOfMonth() 获取当前月第一天的时期
System.out.println(DateUtil.beginOfMonth(new Date()));
//beginOfDay 获取一天最开始的时间
System.out.println(DateUtil.beginOfDay(DateUtil.date()));
//betweenDay(Date beginDate, Date endDate, boolean isReset) 计算时间天数差
Date d1=new Date(2018,10,22);
Date d2=new Date(2017,10,22);
System.out.println(DateUtil.betweenDay(d1, d2, true));
//DateUnit 可以用来设置时间单位的间隔
//between(Date beginDate, Date endDate, DateUnit unit)
Date date= new Date();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Date date1=new Date();
System.out.println(DateUtil.between(date, date1, DateUnit.SECOND));
}
public void testFormatDate(){
System.out.println(DateUtil.formatDate(DateUtil.date()));
//parse() 自动识别时间格式
String dateStr = "2017-03-01";
System.out.println(DateUtil.parse(dateStr));
System.out.println("--------------------------------------");
System.out.println(DateUtil.parse(dateStr, "yyyy-MM-dd"));
}
public void testIsSameDay(){
String date1="2008-05-23";
String date2="2009-05-23";
System.out.println(DateUtil.isSameDay(DateUtil.parse(date1), DateUtil.parse(date2)));
//offset() 时间偏移量
Date date = DateUtil.parse("2017-03-01 22:33:23");
//2017-03-02 22:33:23 时间偏移一天
System.out.println(DateUtil.offset(date, DateField.DAY_OF_MONTH,1));
//2017-03-03 22:33:23 时间偏移两天
System.out.println(DateUtil.offsetDay(date,2));
//2017-03-01 20:33:23 时间偏移-2小时
System.out.println(DateUtil.offsetHour(date,-2));
//昨天
System.out.println(DateUtil.yesterday());
//明天
System.out.println(DateUtil.tomorrow());
//上周
System.out.println(DateUtil.lastWeek());
//下周
System.out.println(DateUtil.nextWeek());
//上一个月
System.out.println(DateUtil.lastMonth());
//下一个月
System.out.println(DateUtil.nextMonth());
}