-1

I need to change String date="20160101" to "January 1,2016" by using LocalDate. Right now, my code looks like this:

DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
LocalDate date = LocalDate.parse("20120403", formatter);
System.out.println(date);

The output is: 2016-01-01

Do I need to use a DateTimeFormatterBuilder to do that, or is there an easier way? Thank you!

shmosel
  • 49,289
  • 6
  • 73
  • 138
Ahab
  • 1
  • 1

3 Answers3

2

LocalDate#toString method prints the date in ISO-8601 format uuuu-MM-dd. That's why your output is coming as 2016-01-01. If you need the output in "January 1,2016" format instead, you need to again format the date to MMM d, yyyy format.

DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
LocalDate date = LocalDate.parse("20120403", formatter);
String strDate = date.format(DateTimeFormatter.ofPattern("MMM d, yyyy"));
System.out.println(strDate);
VHS
  • 9,534
  • 3
  • 19
  • 43
1

You dont have to use a builder. DateTimeFormatter can be created directly from a string here

Fermat's Little Student
  • 5,549
  • 7
  • 49
  • 70
0

Date-time objects have no format

Your comments indicate that you are conflating date-time objects with strings representing their values.

Date-time objects have no format. The objects can generate strings to represent their values, but such strings are distinct and separate. Likewise, a date-time object can be instantiated by parsing a string, but the new date-object will be distinct and separate.

Localizing

You can generate a string as shown in the Answer by VHS where you specify a certain formatting pattern. But generally a better route is to let java.time localize automatically for you.

To localize, specify:

  • FormatStyle to determine how long or abbreviated should the string be.
  • Locale to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.

Example:

Locale l = Locale.CANADA_FRENCH ; // Or Locale.US, Locale.ITALY, etc.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL ).withLocale( l );
String output = myLocalDate.format( f );

The DateTimeFormatterBuilder class is for special needs. Generally all you need is the DateTimeFormatter class.

See this example code run live at IdeOne.com. But beware of limitation with that IdeOne.com web site’s implementation of Java: Alternate locales are ignored, hard-coded to a single English locale (perhaps Locale.US).

DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
LocalDate localDate = LocalDate.parse("20120403", formatter);

String outputStandard = localDate.toString();

DateTimeFormatter fCanadaFrench = 
    DateTimeFormatter.ofLocalizedDate( FormatStyle.LONG )
                     .withLocale( Locale.CANADA_FRENCH ) ;
String outputCanadaFrench = localDate.format( fCanadaFrench ) ;

DateTimeFormatter fUS = 
    DateTimeFormatter.ofLocalizedDate( FormatStyle.LONG )
                     .withLocale( Locale.US ) ;
String outputUS = localDate.format( fUS ) ;

localDate.toString(): 2012-04-03

outputCanadaFrench: 3 avril 2012

outputUS: April 3, 2012

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154