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

前端 未结 7 745
被撕碎了的回忆
被撕碎了的回忆 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:20

    I'm kind of surprised that almost all answers are actually calculating days between epoch and now. With java.time.LocalDate it's as simple as:

    LocalDate.now().toEpochDay()
    
    0 讨论(0)
  • 2020-12-01 18:22

    Date.getTime() - Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

    You can use this and knowledge of how many milliseconds are in the intervals you care about to do the calculations.

    0 讨论(0)
  • 2020-12-01 18:27

    I wouldn't expect there to be an elegant way of doing it since it is not a very common requirement. I can't help but wonder why you want to do it...

    But anyway, the way I would do it is to subtract the epoch date from the Calendar and then get the fields you want:

    Calendar timeSinceEpoch = Calendar.getInstance();
    timeSinceEpoch.add(Calendar.YEAR, -1970);
    
    int yearsSinceEpoch = timeSinceEpoch.get(Calendar.YEAR);
    int monthsSinceEpoch = timeSinceEpoch.get(Calendar.MONTH) + 12 * yearsSinceEpoch;
    
    0 讨论(0)
  • 2020-12-01 18:31
    Long currentMilli = System.currentTimeMillis();
    Long seconds = currentMilli / 1000;
    Long minutes = seconds / 60;
    Long hours = minutes / 60;
    Long days = hours / 24;
    System.out.println("Days since epoch : "  + days);
    

    or

    System.out.println("Days since epoch : "  + ((int) currentMilli / 86400000));
    
    0 讨论(0)
  • 2020-12-01 18:34

    You can use the Joda Time library to do this pretty easily - I use it for anything time related other than using the standard Java Date and Calendar classes. Take a look at the example below using the library:

    MutableDateTime epoch = new MutableDateTime();
    epoch.setDate(0); //Set to Epoch time
    DateTime now = new DateTime();
    
    Days days = Days.daysBetween(epoch, now);
    Weeks weeks = Weeks.weeksBetween(epoch, now);
    Months months = Months.monthsBetween(epoch, now);
    
    System.out.println("Days Since Epoch: " + days.getDays());
    System.out.println("Weeks Since Epoch: " + weeks.getWeeks());
    System.out.println("Months Since Epoch: " + months.getMonths());
    

    When I run this I get the following output:

    Days Since Epoch: 15122
    Weeks Since Epoch: 2160
    Months Since Epoch: 496
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题