可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 Calendar.get(GregorianCalendar.WEEK_OF_YEAR), which is a good start but it doesn't do exactly what I need.
Is there an elegant way to do this in Java?
回答1:
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
回答2:
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
回答3:
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));
回答4:
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()
回答5:
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.
回答6:
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);
回答7:
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;