Java Parse Date parseDate(String value)

Here you can find the source of parseDate(String value)

Description

Parse a date expression with both date and time information with full millisecond precision and create a Date .

License

Apache License

Parameter

Parameter Description
value the date expression to be parsed.

Return

The parsed date.

Declaration

public static Date parseDate(String value) 

Method Source Code


//package com.java2s;
/*// w ww.j a  va 2  s . c o m
 * Licensed to the Indoqa Software Design und Beratung GmbH (Indoqa) under
 * one or more contributor license agreements. See the NOTICE file distributed
 * with this work for additional information regarding copyright ownership.
 * Indoqa 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.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_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
    private static final TimeZone TIME_ZONE = TimeZone.getTimeZone("UTC");

    /**
     * Parse a date expression with both date and time information with full millisecond precision and create a {@link Date}. The
     * assumed time zone is always UTC.
     *
     * This method accepts date expressions generated with {@link #formatDate(Date)}.
     *
     * @param value the date expression to be parsed.
     * @return The parsed date.
     */
    public static Date parseDate(String value) {
        try {
            return createDateFormat().parse(value);
        } catch (ParseException e) {
            throw new IllegalArgumentException("Failed to parse date expression '" + value + "'", e);
        }
    }

    private static DateFormat createDateFormat() {
        DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
        dateFormat.setTimeZone(TIME_ZONE);
        return dateFormat;
    }
}

Related

  1. parseDate(String text, String[] datePattern, int index)
  2. parseDate(String time)
  3. parseDate(String time)
  4. parseDate(String timeString)
  5. parseDate(String token)
  6. parseDate(String value)
  7. parseDate(String value)
  8. parseDate(String value)
  9. parseDate(String value)