0

I am getting weird error which I can't get around easily. This is my code

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.[S][SS][SSS][SSSS][SSSSS][SSSSSS][SSSSSSS][SSSSSSSS][SSSSSSSSS]X");
df.setTimeZone(TimeZone.getDefault());
Date issued = df.parse(this.dateAndTimeOfIssue);

I am getting exception java.text.ParseException: Unparseable date: "2018-02-13T00:00:51.045+13:00"

Does anyone knows what is causing error?

1
  • I recommend you don’t use SimpleDateFormat and Date. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead justv use OffsetDateTime from java.time, the modern Java date and time API. Commented Apr 6, 2022 at 10:48

2 Answers 2

3

tl;dr

Date.from( 
    OffsetDateTime
    .parse( "2018-02-13T00:00:51.045+13:00" )
    .toInstant() 
) 

Details

You are using terrible date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310. Avoid Date/Calendar.

For a date with time of day as seen with a particular offset, use OffsetDateTime class.

Your input text complies with the standard ISO 8601 format used by default in the java.time classes. So no need to specify a formatting pattern.

OffsetDateTime.parse( "2018-02-13T00:00:51.045+13:00" )

If you must use the legacy classes to interoperate with old code not yet updated for java.time, you can convert to and fro. Use the new conversion methods added to the old classes.

java.util.Date d = Date.from( myOffsetDateTime.toInstant() ) ;

All this has been covered many many times already on Stack Overflow. Search to learn more. And see the tutorial provided by Oracle Corp free of cost.

Sign up to request clarification or add additional context in comments.

5 Comments

This is legacy code and there is Date class everywhere. It needs massive refactoring. Its confusing that code works for +1,+2,+3... but not for 13 -.- Java is a mess when it comes to datetime. There is like 6 different types of date :) Thank you for no. 7 date representation
@SlobodanT Just convert back and forth. See my edits.
@SlobodanT Actually, the entire information technology industry has been frightfully ignorant of proper date-time handling. The legacy classes in the early versions of Java were donated by IBM & Taligent. Despite those big names, the classes were written by people who clearly did not understand the subtleties and complexities of date-time handling. The first competent date-time library I know of was Joda-Time, whose creator went on to lead the java.time JSR 310 effort. So, the rule is simple: Write all your new code in java.time classes, converting as needed for old code.
Yeah I found the way.Not the pretty one.Convert OffsetDateTime to LocalDateTime then to Date via Instant. The main problem is that model classes and business logic involved deal with Date.And if I change it I will get a lot of errors.It works for now. Thanks!!!
Don’t use LocalDateTime for this. You will corrupt your data.
1

Your issue is that SimpleDateFormat does not support the optional sections - that is, the enclosure of parts of the format in [ ] characters. You need to either

  • abandon the use of the optional sections,
  • write your own DateFormat implementation,
  • switch to the more modern classes in the java.time package.

The third option is usually the best - the java.time package has been around for many years now, and the DateTimeFormatter class does indeed support the [ ] notation. But if you have to deal with legacy date and time classes, you might be best to write your own DateFormat.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.