从Java中的Epoch开始,获取天,周和月的数量 [英] Get the number of days, weeks, and months, since Epoch in Java

查看:857
本文介绍了从Java中的Epoch开始,获取天,周和月的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Java中获取Epoch之后的天数,周数,月数。

I'm trying to get the number of days, weeks, months since Epoch in Java.

Java Calendar类提供类似calendar.get(GregorianCalendar.DAY_OF_YEAR)或Calendar.get(GregorianCalendar.WEEK_OF_YEAR)的东西,这是一个好的开始,

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.

推荐答案

方案

您可以使用 Joda时间库轻松完成此操作 - 我使用它对于任何时间相关的,而不是使用标准的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. 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中的Epoch开始,获取天,周和月的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆