/** * Example of using a Calendar object * prints the day of the week and date * (for today). */ import java.util.*; class CalPlay { /** * define an array of strings that can translate between integer * DAY_OF_WEEK and strings. Sunday is the first day of the week for * the GregorianCalendar (although this can be changed). * Note: The DAY_OF_WEEK value used by Calendar objects start at 1, * (and our array of strings starts at 0)! */ final static String[] DAYS ={ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; // main prints out the current date static public void main(String []args) { // create a Calendar object that holds the current // time and date Calendar c = Calendar.getInstance(); // print out the current day and date System.out.println("Today is " + dayName(c) + ", " + (c.get(Calendar.MONTH)+1) + "/" + c.get(Calendar.DATE) + "/" + c.get(Calendar.YEAR)); System.out.println(c); } /** * dayName looks up the string version of the day of the week * for the date in a Calendar object * @param c the Calendar object holding the date * @return an string holding the (english) day of the week */ static String dayName( Calendar c) { // Need to subtract 1, since the first DAY_OF_WEEK is 1 ! return(DAYS[c.get(Calendar.DAY_OF_WEEK)-1]); } }