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

2018-12-31 01:23发布

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

How can I increment this date by one day?

标签: java date
25条回答
旧人旧事旧时光
2楼-- · 2018-12-31 02:19

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
查看更多
登录 后发表回答