简体   繁体   English

获取自Java中的Epoch以来的天数,周数和月数

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

I'm trying to get the number of days, weeks, months since Epoch in Java. 我试图获得自Java中Epoch以来的天数,周数,月数。

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. Java Calendar类提供了诸如calendar.get(GregorianCalendar.DAY_OF_YEAR)或Calendar.get(GregorianCalendar.WEEK_OF_YEAR)之类的东西,这是一个很好的开始,但它并不能完全满足我的需要。

Is there an elegant way to do this in Java? 在Java中有一种优雅的方法吗?

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. 您可以使用Joda Time库轻松完成此操作 - 除了使用标准的Java Date和Calendar类之外,我还可以将它用于任何相关的时间。 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

java.time java.time

Use the java.time classes built into Java 8 and later. 使用Java 8及更高版本中内置的java.time类。

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
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));

I'm kind of surprised that almost all answers are actually calculating days between epoch and now . 我有点惊讶的是,几乎所有答案实际上都在计算epochnow之间的天数。 With java.time.LocalDate it's as simple as: 使用java.time.LocalDate就像这样简单:

LocalDate.now().toEpochDay()

Date.getTime() - Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object. Date.getTime() - 返回自此Date对象表示的1970年1月1日00:00:00 GMT以来的毫秒数。

You can use this and knowledge of how many milliseconds are in the intervals you care about to do the calculations. 您可以使用此信息并了解您关心的间隔中有多少毫秒进行计算。

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);

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减去纪元日期,然后得到你想要的字段:

Calendar timeSinceEpoch = Calendar.getInstance();
timeSinceEpoch.add(Calendar.YEAR, -1970);

int yearsSinceEpoch = timeSinceEpoch.get(Calendar.YEAR);
int monthsSinceEpoch = timeSinceEpoch.get(Calendar.MONTH) + 12 * yearsSinceEpoch;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM