Java Parse Date parseDate(String dateValue)

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

Description

Loops over all the possible date formats and tries to find the right one.

License

Apache License

Parameter

Parameter Description
dateValue a parameter

Declaration

public static Date parseDate(String dateValue) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import java.util.Locale;

public class Main {
    /**/*  www.  j  a  va 2 s.  c o m*/
     * Date formats using for Date parsing.
     */
    static final SimpleDateFormat formats[] = {
            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US),
            new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US),
            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sss'Z'", Locale.US),
            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US),
            new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US),
            new SimpleDateFormat("EEEEEE, dd-MMM-yy HH:mm:ss zzz",
                    Locale.US),
            new SimpleDateFormat("EEE MMMM d HH:mm:ss yyyy", Locale.US) };

    /**
     * Loops over all the possible date formats and tries to find the right one.
     * @param dateValue
     */
    public static Date parseDate(String dateValue) {
        if (dateValue == null)
            return null;

        Date date = null;
        for (int i = 0; (date == null) && (i < formats.length); i++) {
            try {
                synchronized (formats[i]) {
                    date = formats[i].parse(dateValue);
                }
            } catch (ParseException e) {
            }
        }

        return date;
    }
}

Related

  1. parseDate(String dateString, String format, TimeZone zone)
  2. parseDate(String dateString, String pattern)
  3. parseDate(String dateText)
  4. parseDate(String datetime)
  5. parseDate(String dateVal)
  6. parseDate(String dateValue)
  7. parseDate(String dateValue, String strFormat)
  8. parseDate(String dttm)
  9. parseDate(String dttm)