Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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(); 
like image 411
Todd Avatar asked Mar 23 '16 20:03

Todd


People also ask

What can I use instead of SimpleDateFormat?

DateTimeFormatter is a replacement for the old SimpleDateFormat that is thread-safe and provides additional functionality.

How do I format a date in Java 8?

Anyway, here is to JDK 8 code example to change the date format in Java String: DateTimeFormatter oldPattern = DateTimeFormatter . ofPattern("yyyy-MM-dd HH:mm:ss"); DateTimeFormatter newPattern = DateTimeFormatter. ofPattern("yyyy-MM-dd"); LocalDateTime datetime = LocalDateTime.

What is the Java time format DateTimeFormatter?

format. DateTimeFormatterBuilder Class in Java. DateTimeFormatterBuilder Class is a builder class that is used to create date-time formatters. DateTimeFormatter is used as a Formatter for printing and parsing date-time objects.

Which package contains date time JSR 310 API in Java 8?

The project has been led jointly by the author of Joda-Time (Stephen Colebourne) and Oracle, under JSR 310, and will appear in the new Java SE 8 package java. time .


2 Answers

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)); } 
like image 98
Tunaki Avatar answered Sep 21 '22 00:09

Tunaki


As an alternative answer to Tunaki, you can also use DateTimeFormatterBuilder:

DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()   .appendPattern("[yyyy]")   .appendPattern("[M/d/yyyy]")   .parseDefaulting(ChronoField.MONTH_OF_YEAR, 1)   .parseDefaulting(ChronoField.DAY_OF_MONTH, 1)   .toFormatter() 
like image 21
Rob Graeber Avatar answered Sep 23 '22 00:09

Rob Graeber