Java Parse Date parseDate(String dateString, String format, TimeZone zone)

Here you can find the source of parseDate(String dateString, String format, TimeZone zone)

Description

Transferred in accordance with the specified format string date into a Date type.

License

Open Source License

Parameter

Parameter Description
dateString The date of the string types
format format pattern
zone timeZone

Return

Date

Declaration

public static Date parseDate(String dateString, String format, TimeZone zone) 

Method Source Code


//package com.java2s;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.Date;

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

public class Main {
    /**// ww  w.  j  ava 2s  .  com
     * Transferred in accordance with the specified format string date into a Date type.
     * 
     * <pre>
     * <code>
     * DateUtils.parseDate(20120202, "yyyyMMdd", TimeZone.getDefault()) = 20120202;
     * </code>
     * </pre>
     * 
     * @param dateString The date of the string types
     * @param format format pattern
     * @param zone timeZone
     * @return Date
     */
    public static Date parseDate(String dateString, String format, TimeZone zone) {
        Date date = null;
        try {
            DateFormat df = getDateFormat(format);
            df.setTimeZone(zone);
            date = df.parse(dateString);
        } catch (Exception e) {
            return null;
        }
        return date;
    }

    /**
     * According to the specified format, String type Date object to parse the return value is a
     * Date object.
     * 
     * <pre>
     * <code>
     * DataUtils.parseDate("2012-12-01", "yyyy-MM-dd") = 2012-12-01
     * </code>
     * </pre>
     * 
     * @param dateString dateValue of string type
     * @param pattern formate pattern
     * @return Date
     */
    public static Date parseDate(String dateString, String pattern) {
        Date result = null;
        DateFormat sdf = getDateFormat(pattern);
        sdf.setLenient(false);
        try {
            result = sdf.parse(dateString);
        } catch (Exception e) {
            result = null;
        }
        return result;
    }

    /**
     * return date format with Locale US.
     * 
     * @param pattern date format pattern.
     * @return DateFormat
     */
    public static DateFormat getDateFormat(String pattern) {
        return new SimpleDateFormat(pattern, Locale.US);
    }
}

Related

  1. parseDate(String dateString)
  2. parseDate(String dateString)
  3. parseDate(String dateString, Locale locale)
  4. parseDate(String dateString, SimpleDateFormat simpleDateFormat)
  5. parseDate(String dateString, String dateFormat)
  6. parseDate(String dateString, String pattern)
  7. parseDate(String dateText)
  8. parseDate(String datetime)
  9. parseDate(String dateVal)