Java String to Date toDate(String dateString, String format)

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

Description

to Date

License

Apache License

Declaration

public static Date toDate(String dateString, String format) 

Method Source Code


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

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

import java.util.Date;

public class Main {
    public static Date toDate(String str) {
        if (null == str || str.trim().isEmpty()) {
            return null;
        }//  w  ww  .  j a  v  a2  s.  c o m
        try {
            DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return fmt.parse(str);
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date toDate(String dateString, String format) {
        try {
            DateFormat fmt = new SimpleDateFormat(format);
            return fmt.parse(dateString);
        } catch (ParseException e) {
            return null;
        }
    }
}

Related

  1. toDate(String dateString)
  2. toDate(String dateString)
  3. toDate(String dateString)
  4. toDate(String dateString, Locale locale)
  5. toDate(String dateString, String dateFormatPattern)
  6. toDate(String datetime)
  7. toDate(String dateToConvert)
  8. toDate(String dateToDatify)
  9. toDate(String dateValue, String format)