001package edu.pdx.cs410J.datesAndText;
002
003import java.text.*;
004import java.util.*;
005
006/**
007 * This class demonstrates how to use the Java's day and time
008 * facilities. 
009 */
010public class DateDemo {
011
012  private static DateFormat dfShort;
013  private static DateFormat dfMedium;
014  private static DateFormat dfLong;
015  private static DateFormat dfFull;
016
017  /**
018   * This main method works with dates.  If there are any arguments on
019   * the command line, they are interpreted as a date to be parsed.
020   * Otherwise, the current day/time are used.
021   */
022  public static void main(String[] args) {
023
024    // Set up the DateFormats
025    dfShort = DateFormat.getDateTimeInstance(DateFormat.SHORT,
026                                             DateFormat.SHORT );
027    dfMedium = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
028                                              DateFormat.MEDIUM);
029    dfLong = DateFormat.getDateTimeInstance(DateFormat.LONG,
030                                            DateFormat.LONG);
031    dfFull = DateFormat.getDateTimeInstance(DateFormat.FULL,
032                                            DateFormat.FULL);
033    
034    Date day = null;
035
036    if (args.length == 0) {
037      // Use the current day/time
038      day = new Date();
039
040    } else {
041      // Parse the command line as if it were a date
042      StringBuffer sb = new StringBuffer();
043      for (int i = 0; i < args.length; i++) {
044        sb.append(args[i]);
045        sb.append(' ');
046      }
047
048      try {
049        day = dfShort.parse(sb.toString());
050
051      } catch (ParseException ex) {
052        System.err.println("** Malformatted date: " + sb);
053        System.exit(1);
054      }
055    }
056
057    // Print out the date in several formats
058    System.out.println("Short: " + dfShort.format(day));
059    System.out.println("Medium: " + dfMedium.format(day));
060    System.out.println("Long: " + dfLong.format(day));
061    System.out.println("Full: " + dfFull.format(day));
062
063  }
064
065}