I'm trying to have the following output of the program:
01/01/2010
But when I run the program I get :
01/01/10
How would I format the date to be like I want?
This is the code I have:
import java.util.Calendar;
import java.util.GregorianCalendar;
public class HelloWorld{
public static void main(String []args){
Calendar calendar = new GregorianCalendar();
//set date to last day of 2009
calendar.set(Calendar.YEAR, 2009);
calendar.set(Calendar.MONTH, 11); // 11 = december
calendar.set(Calendar.DAY_OF_MONTH, 31); // new years eve
//add one day
calendar.add(Calendar.DAY_OF_MONTH, 1);
//date is now jan. 1st 2010
int year = calendar.get(Calendar.YEAR); // now 2010
int month = calendar.get(Calendar.MONTH); // now 0 (Jan = 0)
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); // now 1
//System.out.println(month + "/" + dayOfMonth + "/" + year);
System.out.format("%tD%n",calendar);
}
}
I know that if I change the format to
System.out.format("%tY%n",calendar);
will give the output of "2010", but the 01/01 will not be shown.
Also, how would I make the program to execute a set of ten dates for example? I want to get the result of:
01/01/2010
01/02/2010
...
01/10/2010
I will apreciate your help.