Java Parse Date parseDateTime(String s)

Here you can find the source of parseDateTime(String s)

Description

Converts a date in the form of "yy-MM-dd HH:mm:ss" to a Date object using the default time zone.

License

Apache License

Parameter

Parameter Description
s date string in the form of "yy-MM-dd HH:mm:ss"

Return

the corresponding Java date object or null if it is not parsable.

Declaration

public static Date parseDateTime(String s) 

Method Source Code


//package com.java2s;
/*//from  ww  w.j  ava 2 s  .c o  m
 *  Copyright 2004-2006 Stefan Reuter
 *
 *  Licensed 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.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    private static final String DATE_TIME_PATTERN = "yy-MM-dd HH:mm:ss";

    /**
     * Converts a date in the form of "yy-MM-dd HH:mm:ss" to a Date object using
     * the default time zone.
     * 
     * @param s date string in the form of "yy-MM-dd HH:mm:ss"
     * @return the corresponding Java date object or <code>null</code> if it is not parsable.
     */
    public static Date parseDateTime(String s) {
        return parseDateTime(s, null);
    }

    /**
     * Converts a date in the form of "yy-MM-dd HH:mm:ss" to a Date object using
     * the given time zone.
     * 
     * @param s date string in the form of "yy-MM-dd HH:mm:ss"
     * @param tz the timezone to use or <code>null</code> for the default time zone.
     * @return the corresponding Java date object or <code>null</code> if it is not parsable.
     */
    public static Date parseDateTime(String s, TimeZone tz) {
        DateFormat df;

        if (s == null) {
            return null;
        }

        df = new SimpleDateFormat(DATE_TIME_PATTERN);
        if (tz != null) {
            df.setTimeZone(tz);
        }
        try {
            return df.parse(s);
        } catch (ParseException e) {
            return null;
        }
    }
}

Related

  1. parseDateTime(String dateTimeString)
  2. parseDateTime(String dateTimeString, String[] validFormats, String outputFormat)
  3. parseDateTime(String format, String dateStr)
  4. parseDateTime(String fullDateParam, String unixDateParam, Map request)
  5. parseDateTime(String s)
  6. parseDateTime(String s)
  7. parseDateTime(String s)
  8. parseDatetime(String value)
  9. parseDateTimeFromString(String datetime)