001package edu.pdx.cs410J.datesAndText; 002 003import java.io.*; 004import java.text.*; 005import java.util.*; 006 007/** 008 * This program reads in {@link Date}s from standard in using {@link 009 * DateFormat#SHORT}. Then it adds them to a {@link SortedSet} to see 010 * how their sorted. One of my students claimed that {@link 011 * DateFormat#SHORT} didn't parse 4-digit dates correct. This test 012 * will see if she was right. 013 * 014 * @author David Whitlock 015 * @version $Revision: 1.2 $ 016 */ 017public class SortDateFormatSHORT { 018 019 public static void main(String[] args) throws IOException { 020 int f = DateFormat.SHORT; 021 DateFormat df = DateFormat.getDateTimeInstance(f, f); 022 df.setLenient(false); 023 024 System.out.println("An example of DateFormat.SHORT: " + 025 df.format(new Date())); 026 System.out.println("\nEnter some dates to sort:"); 027 028 SortedSet<Date> sorted = new TreeSet<Date>(); 029 BufferedReader br = 030 new BufferedReader(new InputStreamReader(System.in)); 031 while (true) { 032 String line = br.readLine(); 033 try { 034 sorted.add(df.parse(line)); 035 036 } catch (ParseException ex) { 037 break; 038 } 039 } 040 041 System.out.println(sorted.size() + " sorted dates:"); 042 for (Iterator iter = sorted.iterator(); iter.hasNext(); ) { 043 System.out.print(" "); 044 System.out.println(iter.next()); 045 } 046 } 047 048}