How can I increment a date by one day in Java

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

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

Answers

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

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/428918/how-can-i-increment-a-date-by-one-day-in-java

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils