Syntax string before date: Invalid character pattern "T".

I need to parse a string to date in java. My line has the following format:

2014-09-17T12:00:44.0000000Z 

but java throws the following exception when trying to parse such a format ... java.lang.IllegalArgumentException: Illegal pattern character 'T' .

Any ideas on how to parse this?

Thanks!

+6
source share
3 answers

Given your entry 2014-09-17T12:00:44.0000000Z , this is not enough to avoid the letter T You must also handle the final Z But keep in mind that this Z NOT a literal, but has a UTC+00:00 timezone offset in accordance with ISO-8601-standard . Therefore, avoiding Z NOT correct.

SimpleDateFormat handles this special char Z pattern X character. So, the final solution looks like this:

  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSX"); Date d = sdf.parse("2014-09-17T12:00:44.0000000Z"); System.out.println(d); // output: Wed Sep 17 14:00:44 CEST 2014 

Note that for the time zone, the correct CEST clock time ( toString() uses the system time zone), and the result is equivalent to UTC-time 12:00:44 . Also, I had to insert seven S characters in order to properly process your input, which pretends to have an accuracy of up to 100ns (although Java pre 8 can only process milliseconds).

+12
source

You need to escape the "T" character:

  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US); format.setTimeZone(TimeZone.getTimeZone("UTC")); Date parse = format.parse("2014-09-17T12:00:44.0000000Z"); 

Using the answer to: What is this date format? 2011-08-12T20: 17: 46.384Z

+8
source

Try it.

 import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateClass { public static void main(String[] args) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); Date d = sdf.parse("2014-09-17T12:00:44.0000000Z"); System.out.println(d); //output Wed Sep 17 12:00:44 IST 2014 } } 
+1
source

Source: https://habr.com/ru/post/976819/


All Articles