Java Parse Date parseDateFormat(String s, String pattern, TimeZone tz)

Here you can find the source of parseDateFormat(String s, String pattern, TimeZone tz)

Description

Parses a string using SimpleDateFormat and a given pattern.

License

Apache License

Parameter

Parameter Description
s string to be parsed
pattern SimpleDateFormat pattern
tz time zone in which to interpret string. Defaults to the Java default time zone

Return

a Calendar initialized with the parsed value, or null if parsing failed. If returned, the Calendar is configured to the GMT time zone.

Declaration

public static Calendar parseDateFormat(String s, String pattern,
        TimeZone tz) 

Method Source Code

//package com.java2s;
/*//w  ww  .j ava2  s  .c  om
 // Licensed to DynamoBI Corporation (DynamoBI) under one
 // or more contributor license agreements.  See the NOTICE file
 // distributed with this work for additional information
 // regarding copyright ownership.  DynamoBI licenses this file
 // to you under the Apache License, Version 2.0 (the
 // "License"); you may not use this file except in compliance
 // with the License.  You may obtain a copy of the License at

 //   http://www.apache.org/licenses/LICENSE-2.0

 // Unless required by applicable law or agreed to in writing,
 // software distributed under the License is distributed on an
 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 // KIND, either express or implied.  See the License for the
 // specific language governing permissions and limitations
 // under the License.
 */

import java.text.*;
import java.util.*;

public class Main {
    /**
     * the GMT time zone
     */
    public static final TimeZone gmtZone = TimeZone.getTimeZone("GMT");
    /**
     * the Java default time zone
     */
    public static final TimeZone defaultZone = TimeZone.getDefault();

    /**
     * Parses a string using {@link SimpleDateFormat} and a given pattern. This
     * method parses a string at the specified parse position and if successful,
     * updates the parse position to the index after the last character used.
     * The parsing is strict and requires months to be less than 12, days to be
     * less than 31, etc.
     *
     * @param s string to be parsed
     * @param pattern {@link SimpleDateFormat}  pattern
     * @param tz time zone in which to interpret string. Defaults to the Java
     * default time zone
     * @param pp position to start parsing from
     *
     * @return a Calendar initialized with the parsed value, or null if parsing
     * failed. If returned, the Calendar is configured to the GMT time zone.
     *
     * @pre pattern != null
     */
    private static Calendar parseDateFormat(String s, String pattern,
            TimeZone tz, ParsePosition pp) {
        assert (pattern != null);
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        if (tz == null) {
            tz = defaultZone;
        }
        Calendar ret = Calendar.getInstance(tz);
        df.setCalendar(ret);
        df.setLenient(false);

        java.util.Date d = df.parse(s, pp);
        if (null == d) {
            return null;
        }
        ret.setTime(d);
        ret.setTimeZone(gmtZone);
        return ret;
    }

    /**
     * Parses a string using {@link SimpleDateFormat} and a given pattern. The
     * entire string must match the pattern specified.
     *
     * @param s string to be parsed
     * @param pattern {@link SimpleDateFormat}  pattern
     * @param tz time zone in which to interpret string. Defaults to the Java
     * default time zone
     *
     * @return a Calendar initialized with the parsed value, or null if parsing
     * failed. If returned, the Calendar is configured to the GMT time zone.
     *
     * @pre pattern != null
     */
    public static Calendar parseDateFormat(String s, String pattern,
            TimeZone tz) {
        assert (pattern != null);
        ParsePosition pp = new ParsePosition(0);
        Calendar ret = parseDateFormat(s, pattern, tz, pp);
        if (pp.getIndex() != s.length()) {
            // Didn't consume entire string - not good
            return null;
        }
        return ret;
    }
}

Related

  1. parseDate4UrlCreateTime(String dateStr)
  2. parseDateAllowNull(String date)
  3. parseDateAndGetLong(String date)
  4. parseDateAsString(Date value)
  5. parseDateForFilter(String date)
  6. parseDateFrom(String dateStr)
  7. parseDateFromDateStr(String date)
  8. parseDateFromDateTimeStr(String date)
  9. parseDateFromDefault(String dateString)