// Showtime.java (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2004-08-19 File created. // 2005-06-14 Last modification. // In the GregorianCalendar class, days of week are indicated // by numbers from 1 to 7 so that 1 means Sunday and 7 means Saturday. // To learn more about the printf() method, see the comment at // the end of this program. import java.util.* ; class Showtime { public static void main( String[] not_in_use ) { String[] names_of_days_of_week = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" } ; String[] names_of_months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" } ; Calendar date_and_time_now = new GregorianCalendar() ; System.out.printf( "\n Current time is: %d:%02d:%02d.%03d \n", date_and_time_now.get( Calendar.HOUR_OF_DAY ), date_and_time_now.get( Calendar.MINUTE ), date_and_time_now.get( Calendar.SECOND ), date_and_time_now.get( Calendar.MILLISECOND ) ) ; System.out.print( "\n Current date is: " + names_of_days_of_week [ date_and_time_now.get( Calendar.DAY_OF_WEEK ) - 1 ] + ", day " + date_and_time_now.get( Calendar.DAY_OF_MONTH ) + " of " + names_of_months[ date_and_time_now.get( Calendar.MONTH ) ] + " in year " + date_and_time_now.get( Calendar.YEAR ) + ".\n" ) ; System.out.print( "\n Time zone is: " + date_and_time_now.getTimeZone().getDisplayName() ) ; System.out.print( "\n Difference from UTC/GMT in hours : " + date_and_time_now.get(Calendar.ZONE_OFFSET)/(60*60*1000) ) ; System.out.printf( "\n\n Short 24-h time: %tR", date_and_time_now ) ; System.out.printf( "\n Long 24-h time: %tT", date_and_time_now ) ; System.out.printf( "\n Long 12-h time: %tr", date_and_time_now ) ; System.out.printf( "\n MM/DD/YY date: %tD", date_and_time_now ) ; System.out.printf( "\n ISO date: %tF", date_and_time_now ) ; System.out.printf( "\n Date and time: %tc", date_and_time_now ) ; System.out.printf( "\n Textual date: %1$tA, %1$tB %1$td, %1$tY \n", date_and_time_now ) ; } } /************ The last statements above could be written alternatively as shown below. The method System.currentTimeMillis() returns a long value that contains the current time of the computer in milliseconds since 1970-01-01 00:00. The printf() method can interpret this value and print time/date information according to given format specifiers. long current_time_ticks = System.currentTimeMillis() ; System.out.printf( "\n\n Short 24-h time: %tR", current_time_ticks ) ; System.out.printf( "\n Long 24-h time: %tT", current_time_ticks ) ; System.out.printf( "\n Long 12-h time: %tr", current_time_ticks ) ; System.out.printf( "\n MM/DD/YY date: %tD", current_time_ticks ) ; System.out.printf( "\n ISO date: %tF", current_time_ticks ) ; System.out.printf( "\n Date and time: %tc", current_time_ticks ) ; System.out.printf( "\n Textual date: %1$tA, %1$tB %1$td, %1$tY \n", current_time_ticks ) ; ****************/