Java SimpleDateFormat parse String like "2018-10-12 12:42:12" to date

Description

Java SimpleDateFormat parse String like "2018-10-12 12:42:12" to date

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

public class Main {
  public static void main(String[] argv) {
    String user_time = "2018-10-12 12:42:12";
    System.out.println(getTime(user_time));
  }/*w w w .j a  v  a  2  s  .  c  om*/

  public static Date getTime(String user_time) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date;
    try {
      date = simpleDateFormat.parse(user_time);
      return date;
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return null;
  }
}



PreviousNext

Related