获取 Java 中自纪元以来的天数、周数和月数

发布于 2024-11-10 04:35:25 字数 216 浏览 0 评论 0原文

我正在尝试获取自 Java 纪元以来的天数、周数、月数。

Java Calendar 类提供了诸如calendar.get(GregorianCalendar.DAY_OF_YEAR) 或Calendar.get(GregorianCalendar.WEEK_OF_YEAR) 之类的功能,这是一个好的开始,但它并不能完全满足我的需要。

Java 有没有一种优雅的方法来做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

苍风燃霜 2024-11-17 04:35:25

java.time

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

输出

Days: 16857
Weeks: 2408
Months: 553

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
是你 2024-11-17 04:35:25

您可以使用 Joda Time 库 轻松完成此操作 - 我随时使用它除了使用标准 Java Date 和 Calendar 类之外的其他相关内容。使用该库看一下下面的示例:

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

当我运行它时,我得到以下输出:

Days Since Epoch: 15122
Weeks Since Epoch: 2160
Months Since Epoch: 496

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
情绪少女 2024-11-17 04:35:25

让我有点惊讶的是,几乎所有答案实际上都是在计算epochnow之间的天数。使用 java.time.LocalDate 就这么简单:

LocalDate.now().toEpochDay()

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()
素食主义者 2024-11-17 04:35:25
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);

或者

System.out.println("Days since epoch : "  + ((int) currentMilli / 86400000));
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));
情场扛把子 2024-11-17 04:35:25
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);
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);
盛夏尉蓝 2024-11-17 04:35:25

我不希望有一种优雅的方式来做到这一点,因为这不是一个很常见的要求。我忍不住想知道你为什么要这样做......

但无论如何,我这样做的方法是从 Calendar 中减去纪元日期,然后获取你想要的字段:

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

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

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;
静赏你的温柔 2024-11-17 04:35:25

Date.getTime( ) - 返回此 Date 对象表示的自 1970 年 1 月 1 日 00:00:00 GMT 以来的毫秒数。

您可以使用它以及您关心的时间间隔中有多少毫秒的知识来进行计算。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文