Java – add date string 1 day
•
Java
I have a date string newdate = "31.05.2001"
I must add one day
I tried the following code:
String dateToIncr = "31.12.2001"; String dt=""; SimpleDateFormat sdf = new SimpleDateFormat("dd.mm.yyyy"); Calendar c = Calendar.getInstance(); try { c.setTime(sdf.parse(dateToIncr)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } c.add(Calendar.DAY_OF_MONTH,1); // number of days to add dt = sdf.format(c.getTime()); System.out.println("final date Now : " + dt);
But with this code, it just tries to add day, 31.05 The output of 2001 will be 1.05 2001, keep the month and year unchanged! Please help me with this
I tried, too
c.roll(Calendar.DATE,1); // number of days to add
Solution
You should use the new simpledateformat ("dd.mm. Yyyy");
'mm 'means minute and' mm 'means month
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码