关于日期的计算,很多朋友们都喜欢用时间戳来直接相加,比如希望得到当前时间之后30天的时间,会这么写代码: 直接把 new Data().getTime() 方法得到的时间戳加上30天对应的毫秒数,也就是30天 * 24小时 * 3600秒 * 1000毫秒
Date today = new Date(); Date nextMonth = new Date(today.getTime()+30*24*3600*1000); System.out.println(today); System.out.println(nextMonth);
得到的结果可能会让我失败:
Sat Jul 10 07:41:30 CST 2021Sun Jun 20 14:38:43 CST 2021
得到的日期竟然比当前的日期还好早,你说怎么回事呢?
原因是:
因为 int 发生了溢出
怎样修改呢?我们只要把 30改成30L即可,让其成为long
Date today = new Date(); Date nextMonth = new Date(today.getTime()+30L*24*3600*1000); System.out.println(today); System.out.println(nextMonth);
结果:
Sat Jul 10 07:44:38 CST 2021Mon Aug 09 07:44:38 CST 2021
使用Calendar 在java8之前,我们一般使用Calendar类来实现
Calendar c = Calendar.getInstance(); c.setTime(new Date()); System.out.println(c.getTime()); c.add(Calendar.DAY_OF_MONTH,30); System.out.println(c.getTime());
结果:
Sat Jul 10 07:47:25 CST 2021Mon Aug 09 07:47:25 CST 2021
java 8 日期时间 使用 Java 8 的日期时间类型,可以直接进行各种计算,更加简洁和方便:
LocalDateTime localDateTime = LocalDateTime.now(); System.out.println(localDateTime.plusDays(30));
下面介绍LocalDateTime的基本用法:
1) 获取一年、月、日
private LocalDateTime localDateTime = null; @Before public void init(){ localDateTime = LocalDateTime.now(); } @Test public void test1(){ System.out.println("Year:"+localDateTime.getYear()); System.out.println("Month:"+localDateTime.getMonth().getValue()); System.out.println("Day of Month:"+localDateTime.getDayOfMonth()); System.out.println("Day of Week:"+ localDateTime.getDayOfWeek()); System.out.println("Day of Year:"+localDateTime.getDayOfYear()); }
结果:
Year:2021Month:7Day of Month:10Day of Week:SATURDAYDay of Year:191
- 获取小时、分钟、秒
System.out.println("Hour:"+localDateTime.getHour()); System.out.println("Minute :"+localDateTime.getMinute()); System.out.println("Second:"+localDateTime.getSecond()); System.out.println("Nano:"+localDateTime.getNano());
结果:
Hour:8Minute :17Second:32Nano:50000000
- 对日期做加减
可以使用各种 minus 和 plus 方法直接对日期进行加减操作,比如如下代码实现了减一天和加一天,以及减一个月和加一个月
System.out.println("minus days:"+ localDateTime.minusDays(1)); System.out.println("minus months:"+localDateTime.minusMonths(1)); System.out.println("minus year: "+localDateTime.minusYears(1)); System.out.println("minus Hours:"+localDateTime.minusHours(1)); System.out.println("minus seconds:"+localDateTime.minusSeconds(1));
- 时间的比较
LocalDateTime 类提供以下API比较LocalDateTime 对象在Java中。
boolean isAfter(ChronoLocalDateTime other):检查此日期时间是否在指定日期时间之后。 boolean isBefore(ChronoLocalDateTime other) boolean isEqual(ChronoLocalDateTime other) int compareTo(ChronoLocalDateTime other) 将此日期时间与其他日期时间进行比较。 LocalDateTime dateTime1 = LocalDateTime.of(2021,5,7,9,22,22); LocalDateTime dateTime2 = LocalDateTime.of(2021,6,7,9,22,22); LocalDateTime dateTime3 = LocalDateTime.of(2021,5,7,9,22,22); if(dateTime1.isBefore(dateTime2)){ System.out.println("dateTime1 is before dateTime2"); } if(dateTime2.isAfter(dateTime3)){ System.out.println("dateTime2 is after dateTime3"); } if(dateTime1.equals(dateTime3)){ System.out.println("dateTime1 is equal to dateTime3"); } if(dateTime1.compareTo(dateTime3) ==0){ System.out.println("dateTime1 is equal to dateTime3"); }
- 通过 with 方法进行快捷时间调节
System.out.println("//本月的第一天"); System.out.println(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth())); System.out.println("//今年的程序员日"); System.out.println(LocalDate.now().with(TemporalAdjusters.firstDayOfYear()).plusDays(255)); System.out.println("//今天之前的一个周六"); System.out.println(LocalDate.now().with(TemporalAdjusters.previous(DayOfWeek.SATURDAY))); System.out.println("//本月最后一个工作日"); System.out.println(LocalDate.now().with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY)));
- 可以直接使用 lanbda 表达式进行自定义的时间调整
System.out.println(LocalDate.now().with(temporal -> temporal.plus(ThreadLocalRandom.current().nextInt(100), ChronoUnit.DAYS)));
除了计算外,还可以判断日期是否符合某个条件。比如,自定义函数,判断指定日期是否是家庭成员的生日:
public class DateTimeTest { private static LocalDateTime localDateTime = LocalDateTime.now(); public static void main(String[] args) { System.out.println(isFamilyBirthday(localDateTime)); } public static Boolean isFamilyBirthday(LocalDateTime date) { int month = date.getMonthValue(); int day = date.getDayOfMonth(); if (month == Month.JULY.getValue() && day == 10) return Boolean.TRUE; if (month == Month.SEPTEMBER.getValue() && day == 21) return Boolean.TRUE; if (month == Month.MAY.getValue() && day == 22) return Boolean.TRUE; return Boolean.FALSE; }}