关于日期。。。
1.求日期间隔
Period period=Period.between(date1, date2); ----日期之差
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());
System.out.println(period.toTotalMonths())
Duration d4 = Duration.between(date1, date2);----时间量之差
Duration d1 = Duration.between(time1, time2);
Duration d2 = Duration.between(dateTime1, dateTime2);
Duration d3 = Duration.between(instant1, instant2);
System.out.println("LocalTime持续秒数:" + d1.getSeconds());
System.out.println("LocalDateTime持续秒数:" + d2.getSeconds());
System.out.println("Instant持续秒数" + d3.getSeconds());
使用duration时的报错:
unspported unit seconds:此时提醒不支持秒 这个时候需要换period来取时间之差
获取十分钟前的时间
Date now = new Date();
Date now_10 = new Date(now.getTime() - 600000); //10分钟前的时间
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
String nowTime_10 = dateFormat.format(now_10);
网友评论