1

The below code for date parse works fine for the date "2015-03-25T09:24:10.000+0530" :-

String time = "2015-03-25T09:24:10.000+0530";
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

LocalDateTime localDateTime = LocalDateTime.parse(time, timeFormatter);
System.out.println("localDateTime:"+localDateTime); 

Also, the below code works fine for the date "2015-03-25T09:24:10.000+05:30"

String time = "2015-03-25T09:24:10.000+05:30";
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");

LocalDateTime localDateTime = LocalDateTime.parse(time, timeFormatter);
System.out.println("localDateTime:"+localDateTime); 

But Im trying to find a pattern which match either "2015-03-25T09:24:10.000+0530" or "2015-03-25T09:24:10.000+05:30". Is that possible without doing stuff like checking whether the input date has colon or not?

I felt adding 'X' towards ending of datePattern would help according to the doc but it didnt. https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html.

Any suggestions?

Rajeev
  • 442
  • 1
  • 5
  • 18
  • 1
    These are quite simple patterns so you could write a regexp to match them. Or cut to the chase and just go through your time formatters and catch parsing errors until you encounter one formatter that matches the input. – Torben Mar 26 '21 at 07:27
  • Do keep in mind that such a value is *not* ISO 8601 compliant because it's mixing the ISO 8601 "extended format" for date and time with the ISO 8601 "basic format" for the offset. Such mixtures are explicitly prohibited by ISO 8601:2004(E)§4.3.3(d) (if one has the spec to reference). They *are* however RFC 3339 compliant, per [Appendix A in RFC 3339](https://tools.ietf.org/html/rfc3339#appendix-A) - but only because they misinterpreted the ISO 8601 spec. (such mixtures *were* also prohibited in ISO 8601:2000, and this was [already reported](https://www.rfc-editor.org/errata/eid1584).) – Matt Johnson-Pint Mar 26 '21 at 15:46
  • This is not a duplicate with other listed answer, pls reopen because it's useful. – Tristan Dec 02 '21 at 16:40
  • Actually, the real duplicate is here : https://stackoverflow.com/questions/43360852/cannot-parse-string-in-iso-8601-format-lacking-colon-in-offset-to-java-8-date – Tristan Dec 02 '21 at 17:01

2 Answers2

4

Keep the optional patterns inside the square bracket.

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS[XXX][X]", Locale.ENGLISH);
        Stream.of(
                    "2015-03-25T09:24:10.000+0530",
                    "2015-03-25T09:24:10.000+05:30"
                ).forEach(s -> System.out.println(OffsetDateTime.parse(s, dtf)));  
    }
}

Output:

2015-03-25T09:24:10+05:30
2015-03-25T09:24:10+05:30

Learn more about the modern date-time API from Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
3

Interesting question. You can use parseBest.

  String[] test =  {"2015-03-25T09:24:10.000+0530" , "2015-03-25T09:24:10.000+05:30" };
  for (String s : test) {
     DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS[Z][XXX]");
     TemporalAccessor result = formatter.parseBest(s, ZonedDateTime::from, ZonedDateTime::from);
     System.out.println(result);
  }

This outputs

2015-03-25T09:24:10+05:30
2015-03-25T09:24:10+05:30
geco17
  • 5,152
  • 3
  • 21
  • 38