Java Parse Date parseDate(String dttm)

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

Description

Parse the date.

License

Open Source License

Parameter

Parameter Description
dttm The date string

Return

The Date or null if not able to parse it

Declaration

public static Date parseDate(String dttm) 

Method Source Code

//package com.java2s;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    /**//from   www . j av a2  s.co m
     * Parse the date. Can be of a number of forms:
     * <pre>
     *  yyyy-MM-dd HH:mm:ss z yyyy-MM-dd HH:mm:ss   yyyy-MM-dd HH:mm
     *   yyyy-MM-dd yyyyMMddHHmmss  yyyyMMddHHmm
     *   yyyyMMddHH yyyyMMdd
     * </pre>
     *
     * @param dttm The date string
     * @return The Date or null if not able to parse it
     */
    public static Date parseDate(String dttm) {
        String[] formats = { "yyyy-MM-dd HH:mm:ss z",
                "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd",
                "yyyyMMddHHmmss", "yyyyMMddHHmm", "yyyyMMddHH", "yyyyMMdd" };
        for (int i = 0; i < formats.length; i++) {
            SimpleDateFormat dateFormat = new java.text.SimpleDateFormat(
                    formats[i]);
            dateFormat.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));
            Date date = dateFormat.parse(dttm, new ParsePosition(0));
            if (date != null) {
                return date;
            }
        }
        return null;
    }
}

Related

  1. parseDate(String dateVal)
  2. parseDate(String dateValue)
  3. parseDate(String dateValue)
  4. parseDate(String dateValue, String strFormat)
  5. parseDate(String dttm)
  6. parseDate(String format, String value)
  7. parseDate(String formatPattern, String dateStr)
  8. parseDate(String formattedDate)
  9. parseDate(String gameDate)