/*
 * [InaugurationParse.java]
 *
 * Summary: Demonstrates parsing a date from a String.
 *
 * Copyright: (c) 2000-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.1 2005-06-30
 *  1.2 2006-03-05 reformat with IntelliJ, add Javadoc.
 *  1.3 2009-01-20 change to Obama
 */
package com.mindprod.inauguration;

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

import static java.lang.System.*;

/**
 * Demonstrates parsing a date from a String.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.3 2009-01-20 change to Obama
 * @since 2000
 */
public final class InaugurationParse
    {
    /**
     * mask for: Tuesday 2009-01-20 12:00 AM EST : Eastern Standard Time
     * define an elaborate date format
     */
    private static final SimpleDateFormat SDF_FANCY =
            new SimpleDateFormat( "EEEE yyyy-MM-dd hh:mm aa zz : zzzzzz" );

    /**
     * mask for yyyy-mm-dd, define a plain format for the date
     */
    private static final SimpleDateFormat SDF_PLAIN = new SimpleDateFormat( "yyyy-MM-dd" );

    /**
     * Main method.
     *
     * @param args not used
     */
    public static void main( String[] args )
        {
        final String dateString = "2017-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 )
            {
            out.println( "bad date" );
            }
        // date contains the date in GMT.
        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 );
        out.println( "inauguration is "
                     + SDF_FANCY.format( inauguration.getTime() ) );
        }
    }