Parse string to Date - Java java.util

Java examples for java.util:Date Parse

Description

Parse string 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 strDate = "2018-10-10";
        System.out.println(string2Date(strDate));
    }/*ww  w  .  j av  a2  s.c o  m*/

    public static Date string2Date(String strDate) {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = sdf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}

Related Tutorials