Java String to Date toDate(String dateStr)

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

Description

This function is use to convert string date format to data format

License

Open Source License

Parameter

Parameter Description
dateStr a parameter

Declaration

public static Date toDate(String dateStr) 

Method Source Code

//package com.java2s;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.TimeZone;

public class Main {
    /**/*from   w  ww .j  a  v  a2s .  com*/
     * This function is use to convert string date format to data format
     * @param dateStr
     * @return 
     */
    public static Date toDate(String dateStr) {
        return toDate(dateStr, null);
    }

    /**
     * Convert string date format with time zone
     * @param dateStr
     * @param tz
     * @return 
     */
    public static Date toDate(String dateStr, TimeZone tz) {
        if (tz == null)
            tz = TimeZone.getDefault();
        if (dateStr == null)
            return null;

        SimpleDateFormat fmt = null;
        if (dateStr.length() == 19) {
            fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            fmt.setTimeZone(tz);
        } else {
            fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
        }
        try {
            return fmt.parse(dateStr);
        } catch (ParseException e) {

            return null;
        }
    }
}

Related

  1. toDate(String date, String dateFormat)
  2. toDate(String date, String format)
  3. toDate(String date, String format)
  4. toDate(String date, String format)
  5. toDate(String dateStr)
  6. toDate(String dateStr, String format)
  7. toDate(String dateStr, String pattern)
  8. toDate(String dateString)
  9. toDate(String dateString)