Java SQL Date From toDate(String dateString)

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

Description

to Date

License

Apache License

Declaration

public static Date toDate(String dateString) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static Date toDate(String dateString) {
        return parseDate(dateString);
    }//  w ww. j a  v a 2 s  .  com

    public static Date parseDate(String exifDate) {
        if (exifDate == null) {
            return null;
        }
        String patterns[];
        int i;
        patterns = (new String[] { "yyyy:MM:dd HH:mm:ss", "yyyy:MM:dd HH:mm", "yyyy-MM-dd HH:mm:ss",
                "yyyy-MM-dd HH:mm", "dd.MM.yy HH:mm", "yyyyMMdd HHmmss", "yyyyMMdd.HHmmss", "yyyyMMdd HHmm",
                "MM/dd/yy hh:mm a", "HH:mm:ss dd.MM.yyyy", "yyyy:MM:dd", "yyyy-MM-dd", "dd.MM.yy", "yyyyMMdd",
                "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "MM/dd/yy", "yyyy:MM:dd HH:mm:sss" });
        for (i = 0; i < patterns.length; i++) {
            try {
                DateFormat parser = new SimpleDateFormat(patterns[i]);
                return parser.parse(exifDate);
            } catch (ParseException ex) {
            }
        }
        return null;
    }

    public static Date parseDate(String date, String pattern) {
        if (date == null)
            return null;

        try {
            DateFormat parser = new SimpleDateFormat(pattern);
            return parser.parse(date);
        } catch (ParseException ex) {
        }

        return null;
    }

    public static String parseDate(Timestamp timestamp, String pattern) {
        if (timestamp == null)
            return null;

        DateFormat parser = new SimpleDateFormat(pattern);
        return parser.format(timestamp);
    }
}

Related

  1. toDate(java.util.Date date)
  2. toDate(Object date)
  3. toDate(Object o)
  4. toDate(Object obj)
  5. toDate(Object value)
  6. toDate(String s)
  7. toDateFromString(String str)
  8. toDateMidnight(java.util.Date d)
  9. toDateStr(java.util.Date d)