7

I am using unix timestamp to store the purchase date in my application. sample data: 1371463066

I want to do some manipulation based on the difference in number of days and current day timestamp. for example: If the number of days between the purchase date and current date is 5 days, then send an email regarding feedback again.

how to get the difference in days between two timestamps using java?

4

3 回答 3

10

我还没有测试过,但你可以尝试做这样的事情:

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) )
于 2013-06-17T09:12:25.520 回答
3

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

整数差异 = (ts1 - ts2) / 3600 / 24

于 2013-06-17T09:14:45.647 回答
0

您可以尝试使用日历(这也将允许您使用时区):

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
于 2013-06-17T09:16:21.787 回答