Reputation: 9
I am trying to add 89 days to CurrentDate
through GregorianCalendar
which is returning 31/04/2015. Below is code snippet.
Calendar now = Calendar.getInstance();
String dt="31012013";
now.set(Integer.parseInt(dt.substring(4)),Integer.parseInt(dt.substring(2,4)),Integer.parseInt(dt.substring(0,2)));
now.add(Calendar.DATE, 89);
String matdate=Integer.toString(now.get(Calendar.DATE))+ "/"+ (now.get(Calendar.MONTH) ) +"/" + now.get(Calendar.YEAR);
After executing this code, matdate
value is coming as 31/04/2013
Upvotes: 0
Views: 920
Reputation: 135992
This is because java.util.Calendar
months start with 0 not 1, that is, 4 is May. Besides the best way to format dates is java.text.SimpleDateFormat
String matdate = new SimpleDateFormat("dd/MM/yyyy").format(now.getTime()));
Or better use Joda-Time library for calendar and time related codes.
Upvotes: 5