I wrote this following java code to format the date and time in specific formats. You can see the below code at ideone .
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
class timeAndDateTransformation{
public static void main(String[] argv){
Calendar newDate = new GregorianCalendar(2009,7,1,15,20,00);
SimpleDateFormat dateFormant = new SimpleDateFormat("yyyy/MM/dd");
SimpleDateFormat timeFormant = new SimpleDateFormat("HH:mm:ss");
System.out.println(dateFormant.format(newDate.getTime()).toString());
System.out.println(timeFormant.format(newDate.getTime()).toString());
}
}
Its giving me following output:
2009/08/01
15:20:00
In this output rest all it perfectly okay, except the month. I passed 7 as a month but in this for-matter output its giving 8 as a output. Please point me where am i doing wrong. I am not very familiar with the date/calendar classes of java, so please bear with me.
Months are 0-based, you passed in 7 so that resolves to August.
From the api docs for java.util.Date:
It’s really counter-intuitive to make the month zero-based. I think we’ve all gotten burned by that one at some point.