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

前端 未结 7 746
被撕碎了的回忆
被撕碎了的回忆 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:41
    Calendar now = Calendar.getInstance();
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(0); // start at EPOCH
    
    int days = 0
    while (cal.getTimeInMillis() < now.getTimeInMillis()) {
      days += 1
      cal.add(Calendar.DAY_OF_MONTH, 1) // increment one day at a time
    }
    System.out.println("Days since EPOCH = " + days);
    
    0 讨论(0)
提交回复
热议问题