Possible Duplicate:
Get yesterday's date using Date可能重复:使用日期获取昨天的日期
What is an elegant way set to a Java Date object's value to yesterday?
什么是设置为Java Date对象的值的优雅方式到昨天?
6 个解决方案
#1
14
Do you mean to go back 24 hours in time.
你的意思是24小时回去。
Date date = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000L);
or to go back one day at the time same time (this can be 23 or 25 hours depending on daylight savings)
或者在同一时间返回一天(这可能是23或25小时,具体取决于夏令时)
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
These are not exactly the same due to daylight saving.
由于夏令时,这些并不完全相同。
#2
14
With JodaTime
随着JodaTime
LocalDate today = LocalDate.now();
LocalDate yesterday = today.minus(Period.days(1));
System.out.printf("Today is : %s, Yesterday : %s", today.toString("yyyy-MM-dd"), yesterday.toString("yyyy-MM-dd"));
#3
1
Convert the Date
to a Calendar
object and "roll" it back a single day. Something like this helper method take from here:
将日期转换为日历对象并将其“回滚”一天。像这个帮助方法的东西从这里采取:
public static void addDays(Date d, int days)
{
Calendar c = Calendar.getInstance();
c.setTime(d);
c.add(Calendar.DATE, days);
d.setTime(c.getTime().getTime());
}
For your specific case, just pass in days
as -1
and you should be done. Just make sure you take into consideration the timezone/locale if doing extensive date specific manipulations.
对于您的具体情况,只需将日期作为-1传递,您应该完成。如果进行广泛的日期特定操作,请确保考虑时区/区域设置。
#4
0
you can try the follwing code:
Calendar cal = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Today's date is "+dateFormat.format(cal.getTime()));
cal.add(Calendar.DATE, -1);
System.out.println("Yesterday's date was "+dateFormat.format(cal.getTime()));
#5
0
As many people have already said use Calendar rather than date.
很多人已经说过使用Calendar而不是date。
If you find you really want to use dates:
如果您发现您真的想使用日期:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, -24);
cal.getTime();//returns a Date object
Calendar cal1 = Calendar.getInstance();
cal1.add(Calendar.DAY_OF_MONTH, -1);
cal1.getTime();//returns a Date object
I hope this helps. tomred
我希望这有帮助。 tomred
#6
0
You can try the following example to set it to previous date.
您可以尝试以下示例将其设置为上一个日期。
Calendar cal = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
System.out.println("Today's date is " +dateFormat.format(cal.getTime()));
cal.add(Calendar.DATE, -1);
System.out.println("Yesterday's date was "+dateFormat.format(cal.getTime()));
#1
14
Do you mean to go back 24 hours in time.
你的意思是24小时回去。
Date date = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000L);
or to go back one day at the time same time (this can be 23 or 25 hours depending on daylight savings)
或者在同一时间返回一天(这可能是23或25小时,具体取决于夏令时)
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
These are not exactly the same due to daylight saving.
由于夏令时,这些并不完全相同。
#2
14
With JodaTime
随着JodaTime
LocalDate today = LocalDate.now();
LocalDate yesterday = today.minus(Period.days(1));
System.out.printf("Today is : %s, Yesterday : %s", today.toString("yyyy-MM-dd"), yesterday.toString("yyyy-MM-dd"));
#3
1
Convert the Date
to a Calendar
object and "roll" it back a single day. Something like this helper method take from here:
将日期转换为日历对象并将其“回滚”一天。像这个帮助方法的东西从这里采取:
public static void addDays(Date d, int days)
{
Calendar c = Calendar.getInstance();
c.setTime(d);
c.add(Calendar.DATE, days);
d.setTime(c.getTime().getTime());
}
For your specific case, just pass in days
as -1
and you should be done. Just make sure you take into consideration the timezone/locale if doing extensive date specific manipulations.
对于您的具体情况,只需将日期作为-1传递,您应该完成。如果进行广泛的日期特定操作,请确保考虑时区/区域设置。
#4
0
you can try the follwing code:
Calendar cal = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Today's date is "+dateFormat.format(cal.getTime()));
cal.add(Calendar.DATE, -1);
System.out.println("Yesterday's date was "+dateFormat.format(cal.getTime()));
#5
0
As many people have already said use Calendar rather than date.
很多人已经说过使用Calendar而不是date。
If you find you really want to use dates:
如果您发现您真的想使用日期:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, -24);
cal.getTime();//returns a Date object
Calendar cal1 = Calendar.getInstance();
cal1.add(Calendar.DAY_OF_MONTH, -1);
cal1.getTime();//returns a Date object
I hope this helps. tomred
我希望这有帮助。 tomred
#6
0
You can try the following example to set it to previous date.
您可以尝试以下示例将其设置为上一个日期。
Calendar cal = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
System.out.println("Today's date is " +dateFormat.format(cal.getTime()));
cal.add(Calendar.DATE, -1);
System.out.println("Yesterday's date was "+dateFormat.format(cal.getTime()));