3

I am trying to create a DateTimeFormatter object with a pattern to fit this expression of time: 2016-07-22T00:00:00.000-05:00. I am trying to create a DateTime object using the DateTimeFormatter class with the above input string.

I have tried many different versions of the below expression but am currently getting stuck at the timezone piece "-05:00" where I'm getting the error on my junit test case:

java.lang.IllegalArgumentException: Invalid format: "2016-07-22T00:00:00.000-05:00" is malformed at "-05:00"

The current format pattern that I am using is:

yyyy-MM-dd'T'HH:mm:ss.SSSZ

I have also tried:

yyyy-MM-dd'T'HH:mm:ss.SSSTZD
yyyy-MM-dd'T'HH:mm:ss.SSSZZZ
yyyy-MM-dd'T'HH:mm:ss.SSSz
yyyy-MM-dd'T'HH:mm:ss.SSSzzz
yyyy-MM-dd'T'HH:mm:ss.SSS'TZD'

I am running on Java 7 so I am not sure if that is causing an issue as well.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
user007
  • 101
  • 1
  • 1
  • 5

4 Answers4

2

In order to achieve what you wish, you can utilize the static method "ofPattern" in the DateTimeFormatter class. This method returns a DateTimeFormatter object.

And as shown by tnas, you could use the following date and time format string:

"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"

DateTimeFormatter test = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");

I tested the code and it compiles.

Matt M
  • 36
  • 3
  • This compiles, but does not work because XXX is not a valid pattern component. – user007 Aug 22 '16 at 23:06
  • I believe you are confused, the code that tnas posted compiles, therefore proving that yyyy-MM-dd'T'HH:mm:ss.SSSXXX is a valid date and time format string. – Matt M Aug 22 '16 at 23:20
  • Compiling and running are two different things....That code compiles because you are simply entering a String into the .ofPattern() method that just needs a string input. You could input "Hello World" and the code will still compile. That does not mean when you use that code that it will run properly. – user007 Aug 22 '16 at 23:30
  • I totally agree; nevertheless, what I meant to say was that the code that tnas provided compiles and runs correctly while outputting the current time. This in itself proves that the date and time format string is valid. If his code didn't compile and run correctly, then it would demonstrate that the date and time format string is invalid. Since this isn't the case, the date and time format string is valid. – Matt M Aug 22 '16 at 23:43
  • The "ofPattern" method was introduced in Java 8. I am running on Java 7. – user007 Aug 23 '16 at 00:02
0

Late to the party, but you have to include some timezone information in your timestamp string. Otherwise it would be undefined from which timezone you'll want to substract your offset of five hours.

Assuming that you'll want to parse a timestamp which is 5 hours behind UTC, your string should read

2016-07-22T00:00:00.000Z-05:00

Note the 'Z' before the -05:00 part, which is short for "UTC"

Stefan Haberl
  • 9,812
  • 7
  • 72
  • 81
0

DateTimeFormatter from "yyyy-MM-dd'T'HH:mm:ssX" worked for me.

sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
-1

The API's javadoc describe the patterns: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

I've tested this code:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
Date date = new Date();
System.out.println(sdf.format(date));

The output was:

2016-08-22T18:34:26.604-03:00
tnas
  • 510
  • 5
  • 16
  • This does work, but I am trying to create a DateTime object at the end instead of ending with a String. – user007 Aug 22 '16 at 23:13