-2

For normal date Strings like "2019-04-08 08:35"

   SimpleDateFormat df = new SimpleDateFormat("yyyy-dd-mm hh:mm");

but What shall be the shortest conversion for dates like

   "2019-04-21T12:08:35"
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
  • 4
    Advice - Don't do it. `SimpleDateFormat` is long outdated, use java-8's new java.time features. Even IF you do want to do it, where is your attempt? – Nicholas K Jan 03 '19 at 14:05
  • 2
    Side note, `yyyy-dd-yy hh:mm` is not correct for your example. – achAmháin Jan 03 '19 at 14:07
  • Use this if you are still using SimpleDateFormat `yyyy-MM-dd'T'HH:mm:ss` -- thanks updated – Nikhil Jan 03 '19 at 14:08
  • 1
    LocalDateTime.parse("2019-04-21T12:08:35") – JB Nizet Jan 03 '19 at 14:09
  • 2
    /!\ Alternative answer is javascript, not java /!\ – Indent Jan 03 '19 at 14:12
  • FWIW it has been mentioned that this question is a possible duplicate of [Java string to date conversion](https://stackoverflow.com/questions/4216745/java-string-to-date-conversion) and [How to convert string to Date object?](https://stackoverflow.com/questions/7785014/how-to-convert-string-to-date-object), but it was reopened. I wanted to let the links stand here as reference. – Ole V.V. Jan 04 '19 at 12:19

2 Answers2

1

SimpleDateFormat is not a Date, it's used to

  1. Convert String to Date
  2. Format Date to String

You can parse String to java.time.LocalDateTime directly since java8:

LocalDateTime localDateTime = LocalDateTime.parse("2019-04-21T12:08:35");
System.out.println(localDateTime);
xingbin
  • 27,410
  • 9
  • 53
  • 103
0

Before Java8 :

SimpleDateFormat df = new SimpleDateFormat("yyyy-dd-yy'T'hh:mm:ss");
Indent
  • 4,675
  • 1
  • 19
  • 35