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

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

Description

to Date

License

Apache License

Parameter

Parameter Description
date a parameter
format a parameter

Declaration

public static Date toDate(String date, String format) 

Method Source Code


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

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

import java.util.Date;

public class Main {
    /**// www .j a v  a 2s. c o  m
     * 
     * @param date
     * @return
     */
    public static Date toDate(String date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (date.length() == 10)
                sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.parse(date);
        } catch (ParseException pe) {
            throw new RuntimeException(pe);
        }
    }

    /**
     * 
     * @param date
     * @param format
     * @return
     */
    public static Date toDate(String date, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(date);
        } catch (ParseException pe) {
            throw new RuntimeException(pe);
        }
    }
}

Related

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