Java Date ISO Parse parseIsoDateAndTimeString(String aDateString, String aTimeString)

Here you can find the source of parseIsoDateAndTimeString(String aDateString, String aTimeString)

Description

parse Iso Date And Time String

License

Open Source License

Declaration

private static Calendar parseIsoDateAndTimeString(String aDateString, String aTimeString)
            throws ParseException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 Rene Schneider, GEBIT Solutions GmbH and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import java.util.TimeZone;

public class Main {
    private static Calendar parseIsoDateAndTimeString(String aDateString, String aTimeString)
            throws ParseException {
        String tempDateValue = aDateString;
        if (tempDateValue == null) {
            // in case no date is given, use the "zero" date
            tempDateValue = "1970-01-01";
        }/*from w  w  w .  j av a 2 s . c om*/

        String tempTimeValue;

        if (aTimeString == null) {
            // in case no time is given, use the "zero" time
            tempTimeValue = "T00:00:00.000";
        } else {
            tempTimeValue = aTimeString;
            if (!tempTimeValue.startsWith("T")) {
                tempTimeValue = "T" + tempTimeValue;
            }

            // handle Zulu time
            tempTimeValue = tempTimeValue.replace("Z", "+0000");
        }

        boolean tempHasTimezone = tempTimeValue.contains("+") | tempTimeValue.contains("-");
        boolean tempHasSeconds = (!tempHasTimezone && tempTimeValue.length() > 6)
                | (tempHasTimezone && tempTimeValue.length() > 12);

        if (tempHasTimezone) {
            if (tempTimeValue.charAt(tempTimeValue.length() - 3) == ':') {
                // remove the optional colon in the timezone, if present
                tempTimeValue = tempTimeValue.substring(0, tempTimeValue.length() - 3)
                        + tempTimeValue.substring(tempTimeValue.length() - 2, tempTimeValue.length());
            }
        }

        if (tempHasSeconds) {
            if (!tempTimeValue.contains(".")) {
                // inject milliseconds, if none are present but seconds are given
                if (tempHasTimezone) {
                    tempTimeValue = tempTimeValue.substring(0, tempTimeValue.length() - 5) + ".000"
                            + tempTimeValue.substring(tempTimeValue.length() - 5);
                } else {
                    tempTimeValue += ".000";
                }
            }
        }

        Calendar tempCalendar;

        if (tempHasTimezone) {
            if (tempTimeValue.charAt(tempTimeValue.length() - 3) == ':') {
                // remove the optional colon in the timezone, if present
                tempTimeValue = tempTimeValue.substring(0, tempTimeValue.length() - 3)
                        + tempTimeValue.substring(tempTimeValue.length() - 2, tempTimeValue.length());
            }
            tempCalendar = Calendar
                    .getInstance(TimeZone.getTimeZone("GMT" + tempTimeValue.substring(tempHasSeconds ? 9 : 6)));

            if (tempHasSeconds) {
                tempCalendar.setTime(
                        getSimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse(tempDateValue + tempTimeValue));
            } else {
                tempCalendar
                        .setTime(getSimpleDateFormat("yyyy-MM-dd'T'HH:mmZ").parse(tempDateValue + tempTimeValue));
            }
        } else {
            tempCalendar = Calendar.getInstance();

            if (tempHasSeconds) {
                tempCalendar.setTime(
                        getSimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").parse(tempDateValue + tempTimeValue));
            } else {
                tempCalendar
                        .setTime(getSimpleDateFormat("yyyy-MM-dd'T'HH:mm").parse(tempDateValue + tempTimeValue));
            }
        }

        return tempCalendar;
    }

    /**
     * Creates a preconfigured {@link SimpleDateFormat} for the given format string which can be used by the class
     * internally.
     * 
     * @param aFormatString
     *            the format string to use
     * @return the date format instance
     */
    private static SimpleDateFormat getSimpleDateFormat(String aFormatString) {
        SimpleDateFormat tempFormat = new SimpleDateFormat(aFormatString);
        tempFormat.setLenient(false);
        return tempFormat;
    }
}

Related

  1. parseIsoDate(final String string)
  2. parseISODate(String date)
  3. parseISODate(String date)
  4. parseIsoDate(String str)
  5. parseIsoDate2(String s)
  6. parseIsoDateInput(String str)
  7. parseIsoDateTime(String isoDateTime)
  8. parseISODuration(String duration)
  9. parseIsoString(String time)