Java 8 Date equivalent to Joda's DateTimeFormatterBuilder with multiple parser formats?

Todd

I currently have a Joda date parser that uses the DateTimeFormatterBuilder with half a dozen different date formats that I may receive.

I'm migrating to Java 8's Date routines and don't see an equivalent.

How can I do something like this using Java 8 Dates?

DateTimeParser[] parsers = { 
    DateTimeFormat.forPattern( "yyyy/MM/dd HH:mm:ss.SSSSSS" ).getParser() ,
    DateTimeFormat.forPattern( "yyyy-MM-dd HH:mm:ss" ).getParser() ,
    DateTimeFormat.forPattern( "ddMMMyyyy:HH:mm:ss.SSS Z" ).getParser() ,
    DateTimeFormat.forPattern( "ddMMMyyyy:HH:mm:ss.SSS" ).getParser() ,
    DateTimeFormat.forPattern( "ddMMMyyyy:HH:mm:ss.SSSSSS" ).getParser() ,
    DateTimeFormat.forPattern( "yyyy-MM-dd HH:mm:ss.SSS" ).getParser() 
};

DateTimeFormatter dateTimeFormatterInput = new DateTimeFormatterBuilder()
     .append( null, parsers ).toFormatter();
Tunaki

There is no direct facility to do this, but you can use optional sections. Optional sections are enclosed inside squared brackets []. This allows for the whole section of the String to parse to be missing.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern(""
    + "[yyyy/MM/dd HH:mm:ss.SSSSSS]"
    + "[yyyy-MM-dd HH:mm:ss[.SSS]]"
    + "[ddMMMyyyy:HH:mm:ss.SSS[ Z]]"
);

This formatter defines 3 grand optional sections for the three main patterns you have. Each of them is inside its own optional section.

Working demo code:

public static void main(String[] args) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(""
        + "[yyyy/MM/dd HH:mm:ss.SSSSSS]"
        + "[yyyy-MM-dd HH:mm:ss[.SSS]]"
        + "[ddMMMyyyy:HH:mm:ss.SSS[ Z]]"
    , Locale.ENGLISH);
    System.out.println(LocalDateTime.parse("2016/03/23 22:00:00.256145", formatter));
    System.out.println(LocalDateTime.parse("2016-03-23 22:00:00", formatter));
    System.out.println(LocalDateTime.parse("2016-03-23 22:00:00.123", formatter));
    System.out.println(LocalDateTime.parse("23Mar2016:22:00:00.123", formatter));
    System.out.println(LocalDateTime.parse("23Mar2016:22:00:00.123 -0800", formatter));
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java8 equivalent of Joda's ISODateTimeFormat.dateTimeParser()?

From Dev

joda date DateTimeFormatterBuilder append does not work

From Dev

joda date DateTimeFormatterBuilder append does not work

From Dev

Catering for multiple date formats when converting date formats using Joda time

From Dev

Catering for multiple date formats when converting date formats using Joda time

From Dev

A Java date parser which reads all British date formats

From Dev

DateTimeFormatterBuilder usages in Java 8, specifically optionals

From Dev

Multiple date formats in MATLAB

From Dev

Multiple date formats in MATLAB

From Dev

Equivalent of new Date().getTime() in Java 8

From Dev

multiple java's TimeZone's are directed to single joda's DateTimeZone

From Dev

Easy way to convert Java 8's LocalDateTime to Joda's LocalDateTime

From Dev

Java date with different formats

From Dev

Is there an equivalent of Scala's Either in Java 8?

From Dev

What is the java 8 equivalent to Guava's transformAndConcat?

From Dev

Equivalent of Scala's foldLeft in Java 8

From Dev

Equivalent of joda.ISODateTimeFormat in java.time?

From Dev

Equivalent of joda.ISODateTimeFormat in java.time?

From Dev

Java date validation joda time

From Java

Differences between Java 8 Date Time API (java.time) and Joda-Time

From Dev

Is Joda Time deprecated with java 8 Date and Time API? (java.time)

From Dev

Equivalent to jodatime Interval in Java 8 Date and Time API

From Dev

Build a simple parser that is able to parse different date formats using PyParse

From Dev

Build a simple parser that is able to parse different date formats using PyParse

From Dev

Converting multiple date formats in a column to a single form

From Dev

Splitting a column that contains multiple date formats

From Dev

Multiple date formats in single column in excel

From Dev

Splitting a column that contains multiple date formats

From Dev

Is there a way to convert multiple date formats into datetime python

Related Related

  1. 1

    Java8 equivalent of Joda's ISODateTimeFormat.dateTimeParser()?

  2. 2

    joda date DateTimeFormatterBuilder append does not work

  3. 3

    joda date DateTimeFormatterBuilder append does not work

  4. 4

    Catering for multiple date formats when converting date formats using Joda time

  5. 5

    Catering for multiple date formats when converting date formats using Joda time

  6. 6

    A Java date parser which reads all British date formats

  7. 7

    DateTimeFormatterBuilder usages in Java 8, specifically optionals

  8. 8

    Multiple date formats in MATLAB

  9. 9

    Multiple date formats in MATLAB

  10. 10

    Equivalent of new Date().getTime() in Java 8

  11. 11

    multiple java's TimeZone's are directed to single joda's DateTimeZone

  12. 12

    Easy way to convert Java 8's LocalDateTime to Joda's LocalDateTime

  13. 13

    Java date with different formats

  14. 14

    Is there an equivalent of Scala's Either in Java 8?

  15. 15

    What is the java 8 equivalent to Guava's transformAndConcat?

  16. 16

    Equivalent of Scala's foldLeft in Java 8

  17. 17

    Equivalent of joda.ISODateTimeFormat in java.time?

  18. 18

    Equivalent of joda.ISODateTimeFormat in java.time?

  19. 19

    Java date validation joda time

  20. 20

    Differences between Java 8 Date Time API (java.time) and Joda-Time

  21. 21

    Is Joda Time deprecated with java 8 Date and Time API? (java.time)

  22. 22

    Equivalent to jodatime Interval in Java 8 Date and Time API

  23. 23

    Build a simple parser that is able to parse different date formats using PyParse

  24. 24

    Build a simple parser that is able to parse different date formats using PyParse

  25. 25

    Converting multiple date formats in a column to a single form

  26. 26

    Splitting a column that contains multiple date formats

  27. 27

    Multiple date formats in single column in excel

  28. 28

    Splitting a column that contains multiple date formats

  29. 29

    Is there a way to convert multiple date formats into datetime python

HotTag

Archive