如何在java中获取以前的日期
我有一个格式为yyyyMMdd的字符串对象。有一种简单的方法来获取具有相同格式的前一个日期的字符串吗? 谢谢
我会稍微改写这些答案。
您可以使用
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); // Get a Date object from the date string Date myDate = dateFormat.parse(dateString); // this calculation may skip a day (Standard-to-Daylight switch)... //oneDayBefore = new Date(myDate.getTime() - (24 * 3600000)); // if the Date->time xform always places the time as YYYYMMDD 00:00:00 // this will be safer. oneDayBefore = new Date(myDate.getTime() - 2); String result = dateFormat.format(oneDayBefore);
获得与使用Calendar计算的结果相同的结果。
如果没有 Joda Time,这是怎么做的:
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Main { public static String previousDateString(String dateString) throws ParseException { // Create a date formatter using your format string DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); // Parse the given date string into a Date object. // Note: This can throw a ParseException. Date myDate = dateFormat.parse(dateString); // Use the Calendar class to subtract one day Calendar calendar = Calendar.getInstance(); calendar.setTime(myDate); calendar.add(Calendar.DAY_OF_YEAR, -1); // Use the date formatter to produce a formatted date string Date previousDate = calendar.getTime(); String result = dateFormat.format(previousDate); return result; } public static void main(String[] args) { String dateString = "20100316"; try { // This will print 20100315 System.out.println(previousDateString(dateString)); } catch (ParseException e) { System.out.println("Invalid date string"); e.printStackTrace(); } } }
您可以使用:
Calendar cal = Calendar.getInstance(); //subtracting a day cal.add(Calendar.DATE, -1); SimpleDateFormat s = new SimpleDateFormat("yyyyMMdd"); String result = s.format(new Date(cal.getTimeInMillis()));
没有库支持,它比Java更难。
您可以使用SimpleDateFormat类的实例将给定的String解析为Date对象。
然后你可以使用Calendar的add()来减去一天。
然后,您可以使用SimpleDateFormat的format()将格式化日期作为String。
Joda Time库是一个更容易的API。
使用SimpleDateFormat将String解析为Date,然后减去一天。 之后再将日期转换为String。
HI,
我想提前20天到现在的日期,
Calendar cal = Calendar.getInstance(); Calendar xdate = (Calendar)cal.clone(); xdate.set(Calendar.DAY_OF_YEAR, - 20); System.out.println(" Current Time "+ cal.getTime().toString()); System.out.println(" X Time "+ xdate.getTime().toString());
我有一些联合国预期的结果,当我在1月11日尝试时,
当前时间1月1日星期二12:32:16 IST 2011 X时间星期六12月11日12:32:16 IST 2010
Calendar cal = Calendar.getInstance(); Calendar xdate = (Calendar)cal.clone(); xdate.set(Calendar.DAY_OF_YEAR,cal.getTime().getDate() - 20 ); System.out.println(" Current Time "+ cal.getTime().toString()); System.out.println(" X Time "+ xdate.getTime().toString());
这段代码解决了我的问题。
如果您愿意使用第三方实用程序Joda-Time ,这里是使用Java 7上的Joda-Time 2.3的示例代码。只需两行。
String dateAsString = "20130101"; org.joda.time.LocalDate someDay = org.joda.time.LocalDate.parse(dateAsString, org.joda.time.format.DateTimeFormat.forPattern("yyyymmdd")); org.joda.time.LocalDate dayBefore = someDay.minusDays(1);
看结果:
System.out.println("someDay: " + someDay ); System.out.println("dayBefore: " + dayBefore );
运行时:
someDay: 2013-01-01 dayBefore: 2012-12-31
此代码假定您没有时区。 缺少一个时区很少是一件好事,但如果这是你的情况,那么该代码可能适合你。 如果您有时区,请使用DateTime对象而不是LocalDate 。
关于那个示例代码和关于Joda-Time ……
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so. // Joda-Time - The popular alternative to Sun/Oracle's notoriously bad date, time, and calendar classes bundled with Java 7 and earlier. // http://www.joda.org/joda-time/ // Joda-Time will become outmoded by the JSR 310 Date and Time API introduced in Java 8. // JSR 310 was inspired by Joda-Time but is not directly based on it. // http://jcp.org/en/jsr/detail?id=310 // By default, Joda-Time produces strings in the standard ISO 8601 format. // https://en.wikipedia.org/wiki/ISO_8601
Calendar cal2 = Calendar.getInstance(); cal2.add(Calendar.YEAR, -1); Date dt2 = new Date(cal2.getTimeInMillis()); System.out.println(dt2);