Java Parse Date parseDateTime(String date, String format, String locale, String timeZone)

Here you can find the source of parseDateTime(String date, String format, String locale, String timeZone)

Description

Parses a date using a format string.

License

Mozilla Public License

Parameter

Parameter Description
date the date to parse
format the parsing format
locale the locale
timeZone the timeZone

Return

the parsed date

Declaration

public static java.util.Date parseDateTime(String date, String format, String locale, String timeZone) 

Method Source Code

//package com.java2s;
/*//from w  w  w  .ja v a  2  s .  c o  m
 * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
 * and the EPL 1.0 (http://h2database.com/html/license.html).
 * Initial Developer: H2 Group
 * Iso8601:
 * Initial Developer: Robert Rathsack (firstName dot lastName at gmx dot de)
 */

import java.text.SimpleDateFormat;

import java.util.Locale;
import java.util.TimeZone;

public class Main {
    /**
     * Parses a date using a format string.
     *
     * @param date the date to parse
     * @param format the parsing format
     * @param locale the locale
     * @param timeZone the timeZone
     * @return the parsed date
     */
    public static java.util.Date parseDateTime(String date, String format, String locale, String timeZone) {
        SimpleDateFormat dateFormat = getDateFormat(format, locale, timeZone);
        try {
            synchronized (dateFormat) {
                return dateFormat.parse(date);
            }
        } catch (Exception e) {
            // ParseException
            throw new RuntimeException("PARSE_ERROR:" + date, e);
        }
    }

    private static SimpleDateFormat getDateFormat(String format, String locale, String timeZone) {
        try {
            // currently, a new instance is create for each call
            // however, could cache the last few instances
            SimpleDateFormat df;
            if (locale == null) {
                df = new SimpleDateFormat(format);
            } else {
                Locale l = new Locale(locale);
                df = new SimpleDateFormat(format, l);
            }
            if (timeZone != null) {
                df.setTimeZone(TimeZone.getTimeZone(timeZone));
            }
            return df;
        } catch (Exception e) {
            throw new RuntimeException("PARSE_ERROR:" + (format + "/" + locale + "/" + timeZone), e);
        }
    }
}

Related

  1. parseDateTime(final String dateTime)
  2. parseDateTime(String _date)
  3. parseDateTime(String currDate, String format)
  4. parseDateTime(String d, String t)
  5. parseDateTime(String date)
  6. parseDateTime(String date, String time)
  7. parseDateTime(String dateTime)
  8. parseDateTime(String dateTimeStr)
  9. parseDateTime(String dateTimeString)