java 如何在java中获取以前日期的时间戳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17167694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get the timestamp of previous dates in java?
提问by Poppy
In my shopping cart application, I am storing all the purchase dates in timestamp.
在我的购物车应用程序中,我将所有购买日期存储在时间戳中。
Suppose, I want to get the timestamp of purchase date n days before (n is configurable). How will I get it using java?
假设,我想在 n 天前获取购买日期的时间戳(n 是可配置的)。我将如何使用 java 获得它?
example: something like purchasedateBefore5days = currentTimestamp_in_days - 5; I am getting current timestamp using
例如:像purchaseateBefore5days = currentTimestamp_in_days - 5; 我正在使用当前时间戳
long currentTimestamp = Math.round(System.currentTimeMillis() / 1000);
How can i subtract n days from it. I am a beginner. Please help on this.
我怎样才能从中减去 n 天。我是初学者。请帮忙解决这个问题。
回答by cmbaxter
Use the Calendar
class:
使用Calendar
类:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, -5);
long fiveDaysAgo = cal.getTimeInMillis();
回答by NINCOMPOOP
You can use the Calendarclass in Java , use its set()method to add/subtract the required number of days from the Calendar.DATEfield.
您可以使用Java 中的Calendar类,使用其set()方法从Calendar.DATE字段中添加/减去所需的天数。
To subtract n
days from today , Use c.get(Calendar.DATE)-n
. Sample code :
要从n
今天减去天数,请使用c.get(Calendar.DATE)-n
. 示例代码:
Calendar c = Calendar.getInstance();
System.out.println(c.getTime()); // Tue Jun 18 17:07:45 IST 2013
c.set(Calendar.DATE, c.get(Calendar.DATE)-5);
System.out.println(c.getTime()); // Thu Jun 13 17:07:45 IST 2013
回答by voidMainReturn
Date d = initDate();//intialize your date to any date
Date dateBefore = new Date(d.getTime() - n * 24 * 3600 * 1000 ); //Subtract n days
Also possible duplicate .
回答by voidMainReturn
That currentTimestamp
must be passed to a Calendar instance.
from Calendar you can subtract X days.
Get the milliseconds from calendar and is done.
那currentTimestamp
必须传递给 Calendar 实例。从日历中您可以减去 X 天。从日历中获取毫秒并完成。
回答by Peter Jaloveczki
You can play aorund with the following code snippet, to form it the way you want:
您可以使用以下代码片段玩 aorund,以您想要的方式形成它:
Calendar c = Calendar.getInstance();
c.setTimeInMillis(milliSeconds);
c.add(Calendar.DATE, -5);
Date date = c.getTime();
回答by bNd
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.DATE, -5);
using Java.util.Calendar Class
Calendar.DATE
日历.DATE
This is the field number for get and set indicating the day of the month. , to subtract 5 days from the current time of the calendar, you can achieve it by calling.
This is the field number for get and set indicating the day of the month. , to subtract 5 days from the current time of the calendar, you can achieve it by calling.
回答by Anand Devaraj
Try this..
试试这个..
long timeInMillis = System.currentTimeMillis();
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timeInMillis);
cal.set(Calendar.DATE, cal.get(Calendar.DATE)-5);
java.util.Date date1 = cal.getTime();
System.out.println(date1);