tags:

views:

17405

answers:

8

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
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
See other answers. I prefer not to give "perfect code snippets" to homework style questions.
Mehrdad Afshari
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
Mark: I would have done it with the calendar object. Not sure if there is another good way.
Mehrdad Afshari
Ok thanks Mehrdad just wondering.
Mark Robinson
A: 

can u pleae give me example to do that afshari

This is not an answer, please put this in a comment or edit your question.
Geoffrey Chetwood
A reasonable request, but not an answer. Could you please move this to a comment on his answer instead?
Michael Myers
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
+2  A: 

Construct a Calendar object and use the method add(Calendar.DATE, 1);

krosenvold
+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
+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
Downvoted: This answer assumes Calendar is a GregorianCalendar, which ignores the current Locale. Use Calendar.getInstance() instead.
MetroidFan2002
+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
c.roll(Calendar.DATE, true); would be somewhat better for clarity.
Esko
@Esko, c.roll(Calendar.DATE, true) won't roll the month on the last day of the month.
Sam Hasler
+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
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
A: 

Date d1 = new Date();

Date d2 = new Date();

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