Java String to Date stringToDate(String date, String format)

Here you can find the source of stringToDate(String date, String format)

Description

string To Date

License

Apache License

Declaration

public static Date stringToDate(String date, String format) throws Exception 

Method Source Code


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

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

public class Main {
    public static Date stringToDate(String date, String format) throws Exception {
        Date d = null;/*www  . ja v  a  2  s  .c  o m*/

        try {
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            d = formatter.parse(date);
            if (!formatter.format(d).equals(date)) {
                throw new Exception("error format date");
            }

        } catch (ParseException e) {
            throw e;
        }
        return d;
    }
}

Related

  1. stringToDate(String date)
  2. stringToDate(String date)
  3. stringToDate(String date)
  4. stringToDate(String date)
  5. stringToDate(String date)
  6. stringToDate(String date, String pattren)
  7. stringToDate(String datecontent, String format)
  8. stringToDate(String dateStr)
  9. stringToDate(String dateStr)