I am using GregorianCalander and when i tried to get todays date using the following code i am getting a date which is backdated to one month. The code i have used is as follows.
Calendar gcal = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
today = getTime(gcal);
//date = dateFormat.format(calendar.getTime());
System.out.println("Today: " + today);
Please help me to solve this issue.
The output is :
Today: Thu Apr 28 00:00:00 NZST 2011
EDIT
private Date getTime(Calendar gcal) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String day = form_helper.round(gcal.get(GregorianCalendar.DAY_OF_MONTH));
String month = form_helper.round(gcal.get(GregorianCalendar.MONTH));
String year = form_helper.round(gcal.get(GregorianCalendar.YEAR));
String date = day + "/" + month + "/" + year;
System.out.println(sdf.parse(date));
return sdf.parse(date);
} catch (ParseException ex) {
Logger.getLogger(timesheet_utility.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
I think internal numbering of months starts with 0, not 1. So, you probably need to somewhere add +1.
Edit: after you showed some more code: The needed change is