Get the number of days, weeks, and months, since Epoch in Java

前端 未结 7 750
被撕碎了的回忆
被撕碎了的回忆 2020-12-01 18:05

I\'m trying to get the number of days, weeks, months since Epoch in Java.

The Java Calendar class offers things like calendar.get(GregorianCalendar.DAY_OF_YEAR), or

7条回答
  •  离开以前
    2020-12-01 18:37

    java.time

    Use the java.time classes built into Java 8 and later.

    LocalDate now = LocalDate.now();
    LocalDate epoch = LocalDate.ofEpochDay(0);
    
    System.out.println("Days: " + ChronoUnit.DAYS.between(epoch, now));
    System.out.println("Weeks: " + ChronoUnit.WEEKS.between(epoch, now));
    System.out.println("Months: " + ChronoUnit.MONTHS.between(epoch, now));
    

    Output

    Days: 16857
    Weeks: 2408
    Months: 553
    

提交回复
热议问题