Parse String to Date using yyyy-MM-dd'T'HH:mm:ss format - Java java.util

Java examples for java.util:Date Format

Description

Parse String to Date using yyyy-MM-dd'T'HH:mm:ss format

Demo Code


//package com.java2s;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static void main(String[] argv) throws Exception {
        String value = "java2s.com";
        System.out.println(getDate(value));
    }//from  w ww  .j av a 2s. c o  m

    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";

    public static Date getDate(String value, Date defaultValue,
            String datePattern) {
        try {
            return new SimpleDateFormat(datePattern).parse(value);
        } catch (Exception ex) {
            return defaultValue;
        }
    }

    public static Date getDate(String value, Date defaultValue) {
        return getDate(value, defaultValue, DEFAULT_DATE_PATTERN);
    }

    public static Date getDate(String value) {
        return getDate(value, null);
    }
}

Related Tutorials