Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

date

I'm working with a date in this format: yyyy-mm-dd.

How can I increment this date by one day?

like image 260
user48094 Avatar asked Jan 09 '09 17:01

user48094


People also ask

How do I add 30 days to a specific date in Java?

time. ZonedDateTime zdtMonthLater = zdt. plusMonths( 1 );

How do I add days to a date in Java?

Use the add() method of the calendar class to add days to the date. The add method() takes two parameter, i.e., calendar field and amount of time that needs to be added. Get the new date from the calendar and set the format of the SimpleDateFormat class to show the calculated new date on the screen.

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

DateTime yesterday = new DateTime(). minusDays(1);

How can I increase my one day date?

Date today = new Date(); Date tomorrow = new Date(today. getTime() + (1000 * 60 * 60 * 24));


1 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 
like image 155
Dave Avatar answered Oct 01 '22 14:10

Dave