Java Data Type How to - Use pattern character 'T' when parsing a date string to java.Date








Question

We would like to know how to use pattern character 'T' when parsing a date string to java.Date.

Answer

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/*  w ww  . jav  a 2  s  . c  om*/
public class Main {
  public static void main(final String[] args) {
    final String date = "2015-10-02T12:23:23";
    final String pattern = "yyyy-MM-dd'T'hh:mm:ss";
    final SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    try {
      Date d = sdf.parse(date);
      System.out.println(d);
    } catch (ParseException e) {
      e.printStackTrace();
    }

  }
}

The code above generates the following result.