Here is a Java Program to Demonstrate Calendar
Output of Above Java Program
Date: Feb 18 2012
Time: 5:40:59
Updated time: 10:29:22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import java.util.Calendar; class CalendarDemo { public static void main(String args[]) { String months[] = { "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec" }; // Create a calendar initialized with the // current date and time in the default // locale and timezone. Calendar calendar = Calendar.getInstance(); // Display current time and date information. System.out.print( "Date: " ); System.out.print(months[calendar.get(Calendar.MONTH)]); System.out.print( " " + calendar.get(Calendar.DATE) + " " ); System.out.println(calendar.get(Calendar.YEAR)); System.out.print( "Time: " ); System.out.print(calendar.get(Calendar.HOUR) + ":" ); System.out.print(calendar.get(Calendar.MINUTE) + ":" ); System.out.println(calendar.get(Calendar.SECOND)); // Set the time and date information and display it. calendar.set(Calendar.HOUR, 10 ); calendar.set(Calendar.MINUTE, 29 ); calendar.set(Calendar.SECOND, 22 ); System.out.print( "Updated time: " ); System.out.print(calendar.get(Calendar.HOUR) + ":" ); System.out.print(calendar.get(Calendar.MINUTE) + ":" ); System.out.println(calendar.get(Calendar.SECOND)); } } |
Output of Above Java Program
Date: Feb 18 2012
Time: 5:40:59
Updated time: 10:29:22