如何在java中查找两个unix时间戳之间的天数

时间:2013-06-17 09:00:58

标签: java timestamp

我使用unix时间戳将购买日期存储在我的应用程序中。 样本数据:1371463066

我想根据天数和当天时间戳的差异进行一些操作。 例如:如果购买日期与当前日期之间的天数为5天,则再次发送有关反馈的电子邮件。

如何使用java获得两个时间戳之间的天数差异?

3 个答案:

答案 0 :(得分:9)

我没有测试过,但你可能会尝试这样做:

Date purchasedDate = new Date ();
//multiply the timestampt with 1000 as java expects the time in milliseconds
purchasedDate.setTime((long)purchasedtime*1000);

Date currentDate = new Date ();
currentDate .setTime((long)currentTime*1000);

//To calculate the days difference between two dates 
int diffInDays = (int)( (currentDate.getTime() - purchasedDate.getTime()) 
                 / (1000 * 60 * 60 * 24) )

答案 1 :(得分:2)

Unix时间戳是自1.1.1970以来的秒数。如果你有2个unix时间戳,那么整天的差异是

int diff =(ts1 - ts2)/ 3600/24

答案 2 :(得分:0)

您可以尝试使用日历(也可以使用TimeZones):

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(1371427200l * 1000l);
Calendar newCalendar = Calendar.getInstance();
newCalendar.setTimeInMillis(1371527200l * 1000l);
// prints the difference in days between newCalendar and calendar
System.out.println(newCalendar.get(Calendar.DAY_OF_YEAR) - calendar.get(Calendar.DAY_OF_YEAR));

输出:

1
相关问题