Java examples for Date Time:Calendar
Using the GregorianCalendar Class
import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class Main { public static void main(String[] args) { GregorianCalendar gc = new GregorianCalendar(); System.out.println("Current Date: " + getStr(gc)); // Add 1 year gc.add(Calendar.YEAR, 1); System.out.println("After adding a year: " + getStr(gc)); // Add 15 days gc.add(Calendar.DATE, 15); System.out.println("After adding 15 days: " + getStr(gc)); long millis = gc.getTimeInMillis(); Date dt = gc.getTime();//from ww w . j a v a 2 s .c om System.out.println("Time in millis: " + millis); System.out.println("Time as Date: " + dt); } public static String getStr(GregorianCalendar gc) { int day = gc.get(Calendar.DAY_OF_MONTH); int month = gc.get(Calendar.MONTH); int year = gc.get(Calendar.YEAR); int hour = gc.get(Calendar.HOUR); int minute = gc.get(Calendar.MINUTE); int second = gc.get(Calendar.SECOND); String str = day + "/" + (month + 1) + "/" + year + " " + hour + ":" + minute + ":" + second; return str; } }