Java Parse Date Pattern YYYY parseEuropeanDateAnd12HrsTimeString(String aDateString, String aTimeString)

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

Description

parse European Date And Hrs Time String

License

Open Source License

Declaration

private static Calendar parseEuropeanDateAnd12HrsTimeString(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;

public class Main {
    private static Calendar parseEuropeanDateAnd12HrsTimeString(String aDateString, String aTimeString)
            throws ParseException {
        String tempStringToParse;
        if (aDateString != null) {
            tempStringToParse = aDateString;
        } else {//from   ww  w .  j av  a2 s.  co m
            // use the common "zero" date if no date was given.
            tempStringToParse = "01.01.1970";
        }

        // append a divider
        tempStringToParse += "T";

        if (aTimeString.length() < 8) {
            // inject seconds if they're not given; they're optional, but if not present :00 is assumed
            tempStringToParse += aTimeString.substring(0, aTimeString.length() - 2) + ":00.000"
                    + aTimeString.substring(aTimeString.length() - 2);
        } else {
            if (aTimeString.length() < 11) {
                // inject just milliseconds
                tempStringToParse += aTimeString.substring(0, aTimeString.length() - 2) + ".000"
                        + aTimeString.substring(aTimeString.length() - 2);
            } else {
                tempStringToParse += aTimeString;
            }
        }

        return parseDateOrTimeString(tempStringToParse, "dd.MM.yyyy'T'hh:mm:ss.SSSaa");
    }

    private static Calendar parseDateOrTimeString(String aDateString, String aFormatString) throws ParseException {
        Calendar tempCalendar = Calendar.getInstance();
        tempCalendar.setTime(getSimpleDateFormat(aFormatString).parse(aDateString));
        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. parseDIGTime(String timestr)
  2. parseDisplay4Key(Object date)
  3. parseDT(String fmt, String timeline)
  4. parseDueDate(String date)
  5. parseEapoDate(String date)
  6. parseEXIFFormat(String dateString)
  7. parseExpireField(String timestring)
  8. parseFormattedTime(String formattedTime)
  9. parseFTPDate(String dateStr)