Parse String in 2018-10-10 12:12:12 format to Date - Java java.util

Java examples for java.util:Date Parse

Description

Parse String in 2018-10-10 12:12:12 format to Date

Demo Code


//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static void main(String[] argv) {
        String sdate = "2018-10-10 12:12:12";
        System.out.println(toDate(sdate));
    }//from  w  w  w  .  jav  a2  s.  co  m

    private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    public static Date toDate(String sdate) {
        try {
            return dateFormater.get().parse(sdate);
        } catch (ParseException e) {
            return null;
        }
    }
}

Related Tutorials