I am getting date in the format as yyyy-mm-dd. I need to increment this by one day. How can I do this?
+2
A:
Convert it to a date, add one day and convert it back to the string with the specific format.
Mehrdad Afshari
2009-01-09 17:22:35
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.
Mark Robinson
2009-01-09 17:48:41
See other answers. I prefer not to give "perfect code snippets" to homework style questions.
Mehrdad Afshari
2009-01-09 18:14:19
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.
Mark Robinson
2009-01-09 18:29:47
Mark: I would have done it with the calendar object. Not sure if there is another good way.
Mehrdad Afshari
2009-01-09 18:38:10
Ok thanks Mehrdad just wondering.
Mark Robinson
2009-01-09 18:40:33
This is not an answer, please put this in a comment or edit your question.
Geoffrey Chetwood
2009-01-09 17:29:04
A reasonable request, but not an answer. Could you please move this to a comment on his answer instead?
Michael Myers
2009-01-09 17:29:42
You should either update your question or add a comment to the answer in question. Do not add a further question as an answer.
krosenvold
2009-01-09 17:29:46
+2
A:
Construct a Calendar object and use the method add(Calendar.DATE, 1);
krosenvold
2009-01-09 17:27:26
+1
A:
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.
Ross
2009-01-09 17:28:48
+5
A:
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
Calendar cal = Calendar.getInstance();
cal.setTime( dateFormat.parse( inputString ) );
cal.add( Calendar.DATE, 1 );
Alex B
2009-01-09 17:30:46
Downvoted: This answer assumes Calendar is a GregorianCalendar, which ignores the current Locale. Use Calendar.getInstance() instead.
MetroidFan2002
2009-01-10 06:29:01
+14
A:
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
Dave
2009-01-09 17:33:53
@Esko, c.roll(Calendar.DATE, true) won't roll the month on the last day of the month.
Sam Hasler
2009-07-31 22:38:32
+6
A:
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));
Willi aus Rohr
2009-01-09 18:13:05
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.
MetroidFan2002
2009-01-10 06:33:31