Java Parse Date parseDate(String dateString)

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

Description

parse Date

License

Open Source License

Declaration

public static Date parseDate(String dateString) throws ParseException 

Method Source Code


//package com.java2s;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    private static final ThreadLocal<DateFormat> COMMON_FORMAT = new ThreadLocal<DateFormat>() {
        @Override//  w  w  w  . ja va  2 s.co m
        protected DateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };
    private static final ThreadLocal<DateFormat> DATE_FORMAT = new ThreadLocal<DateFormat>() {
        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };

    public static Date parseDate(String dateString) throws ParseException {
        return DATE_FORMAT.get().parse(dateString);
    }

    public static Date parse(String dateString) throws ParseException {
        try {
            return COMMON_FORMAT.get().parse(dateString);
        } catch (ParseException e) {
            return DATE_FORMAT.get().parse(dateString);
        }
    }
}

Related

  1. parseDate(String dateStr, String format)
  2. parseDate(String dateStr, String... formats)
  3. parseDate(String dateString)
  4. parseDate(String dateString)
  5. parseDate(String dateString)
  6. parseDate(String dateString)
  7. parseDate(String dateString)
  8. parseDate(String dateString)
  9. parseDate(String dateString)