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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:11:27  来源:igfitidea点击:

how to get the timestamp of previous dates in java?

javatimestamp

提问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 Calendarclass:

使用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 ndays 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 currentTimestampmust 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

使用Java.util.Calendar 类

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);