Tuesday, April 5, 2011

How can I increment a date by one day in Java?

I am getting date in the format as yyyy-mm-dd. I need to increment this by one day. How can I do this?

From stackoverflow
  • Convert it to a date, add one day and convert it back to the string with the specific format.

    Mark Robinson : How would add one day to a java.sql.Date object? The only way I can see is to add milliseconds but you running in to issues with day light savings this way.
    Mehrdad Afshari : See other answers. I prefer not to give "perfect code snippets" to homework style questions.
    Mark Robinson : Other answers use Calendar object, I ask b/c i've used the Date object in the past and ran into the day light saving problem. Just curious to see if there was another way to do it that i didn't think of.
    Mehrdad Afshari : Mark: I would have done it with the calendar object. Not sure if there is another good way.
    Mark Robinson : Ok thanks Mehrdad just wondering.
  • can u pleae give me example to do that afshari

    Geoffrey Chetwood : This is not an answer, please put this in a comment or edit your question.
    Michael Myers : A reasonable request, but not an answer. Could you please move this to a comment on his answer instead?
    krosenvold : You should either update your question or add a comment to the answer in question. Do not add a further question as an answer.
  • Construct a Calendar object and use the method add(Calendar.DATE, 1);

  • Use the DateFormat API to convert the String into a Date object, then use the Calendar API to add one day. Let me know if you want specific code examples, and I can update my answer.

  • SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
    Calendar cal = Calendar.getInstance();
    cal.setTime( dateFormat.parse( inputString ) );
    cal.add( Calendar.DATE, 1 );
    
    MetroidFan2002 : Downvoted: This answer assumes Calendar is a GregorianCalendar, which ignores the current Locale. Use Calendar.getInstance() instead.
  • Something like this should do the trick:

    String dt = "2008-01-01";  // Start date
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    c.setTime(sdf.parse(dt));
    c.add(Calendar.DATE, 1);  // number of days to add
    dt = sdf.format(c.getTime());  // dt is now the new date
    
    Esko : c.roll(Calendar.DATE, true); would be somewhat better for clarity.
    Sam Hasler : @Esko, c.roll(Calendar.DATE, true) won't roll the month on the last day of the month.
  • Take a look at Joda-Time (http://joda-time.sourceforge.net/).

    DateTimeFormatter parser = ISODateTimeFormat.date();
    
    DateTime date = parser.parseDateTime(dateString);
    
    String nextDay = parser.print(date.plusDays(1));
    
    MetroidFan2002 : You can remove the parser calls for constructing the DateTime. Use DateTime date = new DateTime(dateString); Then, nextDay is ISODateTimeFormat.date().print(date.plusDays(1)); See http://joda-time.sourceforge.net/api-release/org/joda/time/DateTime.html#DateTime(java.lang.Object) for more info.
  • Date d1 = new Date();

    Date d2 = new Date();

    d2.setTime(d1.getTime() + 1 * 24 * 60 * 60 * 1000);

0 comments:

Post a Comment