package com.mindprod.inauguration;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * Demonstrates parsing a date from a String
 *
 * @author Roedy Green
 * @Version 1.1 2005-06-30
 */
public final class InaugurationParse
    {
    // ------------------------------ FIELDS ------------------------------

    /**
     * mask for: Tuesday 2009-01-20 12:00 AM EST : Eastern Standard Time
     */
    private static final SimpleDateFormat SDF_FANCY =
            new SimpleDateFormat( "EEEE yyyy-MM-dd hh:mm aa zz : zzzzzz" );
    /**
     * define the format of the date
     */
    private static final SimpleDateFormat SDF_PLAIN = new SimpleDateFormat( "yyyy/MM/dd" );

    // --------------------------- main() method ---------------------------

    /**
     * Main method.
     *
     * @param args not used
     */
    public static void main( String[] args )
        {
        final String dateString = "2009/01/20";

        // set timezone to use in interpreting the date
        TimeZone est = TimeZone.getTimeZone( "America/New_York" );
        GregorianCalendar inauguration = new GregorianCalendar( est );
        SDF_PLAIN.setCalendar( inauguration );
        Date date = null;
        try
            {
            // set timestamp
            date = SDF_PLAIN.parse( dateString );
            inauguration.setTime( date );
            // inauguration is now ready with both timezone and timestamp
            }
        catch ( ParseException e )
            {
            System.out.println( "bad date" );
            }
        // date contains the date in GMT.
        System.out.println( "millis since 1970 is " + date.getTime() );
        // display timestamp in default format EST.
        // Same code automatically adjusts for DST in the summer.
        // set timezone
        SDF_FANCY.setCalendar( inauguration );
        System.out
                .println( "inauguration is "
                          + SDF_FANCY.format( inauguration.getTime() ) );
        }
    }