从Java中的Epoch开始,获取天,周和月的数量 [英] Get the number of days, weeks, and months, since Epoch in Java
问题描述
我想从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屋!