/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

    import java.time.Duration;
    import java.time.LocalDateTime;
    import java.time.LocalTime;
    import java.time.ZoneId;
    import java.time.ZoneOffset;
    import java.time.ZonedDateTime;
    import java.time.format.DateTimeFormatter;
    import java.time.format.DateTimeParseException;
    import java.util.Locale;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{

            // Run with out time zone (assumes UTC).
            Duration dShort = DurationProcessor.process ( "2008-01-01 01:00 pm - 01:56 pm" );
            System.out.println ( "dShort: " + dShort );

            Duration dLong = DurationProcessor.process ( "2008-01-01 08:30 pm - 2008-01-02 09:30 am" );
            System.out.println ( "dLong: " + dLong );

            // Run with specified time zone.
            ZoneId z = ZoneId.of ( "America/Montreal" );
            Duration dShortZoned = DurationProcessor.process ( "2008-01-01 01:00 pm - 01:56 pm" , z );
            System.out.println ( "dShortZoned: " + dShortZoned );

            Duration dLongZoned = DurationProcessor.process ( "2008-01-01 08:30 pm - 2008-01-02 09:30 am" , z );
            System.out.println ( "dLongZoned: " + dLongZoned );

	}
}



    /**
     *
     * @author Basil Bourque
     */
    class DurationProcessor {

        static final int SHORT = 30;
        static final int LONG = 41;

        static final DateTimeFormatter FORMATTER_LOCALDATETIME = DateTimeFormatter.ofPattern ( "uuuu-MM-dd hh:mm a" );
        static final DateTimeFormatter FORMATTER_LOCALTIME = DateTimeFormatter.ofPattern ( "hh:mm a" );

        static public Duration process ( String input ) {
            return DurationProcessor.process ( input , ZoneOffset.UTC );
        }

        static public Duration process ( String input , ZoneId zoneId ) {
            Duration d = Duration.ZERO;  // Or maybe null. To be generated by the bottom of this code.

            if ( null == input ) {
                // …
                System.out.println ( "ERROR - Passed null argument." );
                return d;
            }
            if ( input.length () == 0 ) {
                // …
                System.out.println ( "ERROR - Passed empty string as argument." );
                return d;
            }

            String inputModified = input.toUpperCase ( Locale.ENGLISH ); // Change `am` `pm` to `AM` `PM` for parsing.

            String[] parts = inputModified.split ( " - " );
            String inputStart = parts[ 0 ]; // A date-time sting.
            String inputStop = parts[ 1 ]; // Either a date-time string or a time-only string (assume the same date).

            ZonedDateTime start = null;  // To be generated in this block of code.
            try {
                LocalDateTime ldt = LocalDateTime.parse ( inputStart , DurationProcessor.FORMATTER_LOCALDATETIME );
                start = ldt.atZone ( zoneId );
            } catch ( DateTimeParseException e ) {
                // …
                System.out.println ( "ERROR - The start failed to parse. inputStart: " + inputStart );
                return d;
            }

            ZonedDateTime stop = null; // To be generated in this block of code.
            switch ( input.length () ) {
                case DurationProcessor.SHORT:  // Example: "2008-01-01 01:00 pm - 01:56 pm"
                    try {
                        LocalTime stopTime = LocalTime.parse ( inputStop , DurationProcessor.FORMATTER_LOCALTIME );
                        stop = ZonedDateTime.of ( start.toLocalDate () , stopTime , zoneId );
                    } catch ( DateTimeParseException e ) {
                        // …
                        System.out.println ( "ERROR - The stop time failed to parse." );
                        return d;
                    }
                    break;
                case DurationProcessor.LONG:  // "2008-01-01 8:30 pm - 2008-01-02 09:30 am"
                    try {
                        LocalDateTime ldt = LocalDateTime.parse ( inputStop , DurationProcessor.FORMATTER_LOCALDATETIME );
                        stop = ldt.atZone ( zoneId );
                    } catch ( DateTimeParseException e ) {
                        // …
                        System.out.println ( "ERROR - The stop date-time failed to parse." );
                        return d;
                    }
                    break;
                default:
                    // …
                    System.out.println ( "ERROR - Input string is of unexpected length: " + input.length () );
                    break;
            }

            d = Duration.between ( start , stop );
            return d;
        }

        public static void main ( String[] args ) {
            // Run with out time zone (assumes UTC).
            Duration dShort = DurationProcessor.process ( "2008-01-01 01:00 pm - 01:56 pm" );
            System.out.println ( "dShort: " + dShort );

            Duration dLong = DurationProcessor.process ( "2008-01-01 08:30 pm - 2008-01-02 09:30 am" );
            System.out.println ( "dLong: " + dLong );

            // Run with specified time zone.
            ZoneId z = ZoneId.of ( "America/Montreal" );
            Duration dShortZoned = DurationProcessor.process ( "2008-01-01 01:00 pm - 01:56 pm" , z );
            System.out.println ( "dShortZoned: " + dShortZoned );

            Duration dLongZoned = DurationProcessor.process ( "2008-01-01 08:30 pm - 2008-01-02 09:30 am" , z );
            System.out.println ( "dLongZoned: " + dLongZoned );

        }
    }