fiveandten
fiveandten

Reputation: 61

Joda DateTimeFormat without - separator?

I am trying to make a joda LocalDate in the format of yyyyMMdd, but every time I parse it to create the object, it inserts hyphens like yyyy-MM-dd for no apparent reason.

DateTimeFormatter format = DateTimeFormat.forPattern("yyyyMMdd");
LocalDate d = format.parseLocalDate("20150101");

Is there a way to get this to end up looking like 20150101 or will it forever be 2015-01-01?

Upvotes: 0

Views: 2525

Answers (1)

Meno Hochschild
Meno Hochschild

Reputation: 44061

Solution is not to use the toString()-method:

DateTimeFormatter format = DateTimeFormat.forPattern("yyyyMMdd");
LocalDate d = format.parseLocalDate("20150101");
System.out.println(format.print(d)); // 20150101
System.out.println(d); // using toString(): 2015-01-01

Quite simple, isn't it? For me, the form using hyphens is more readable, so this is a natural choice for the standard output of the method toString().

By the way: Both variants are valid ISO-8601-strings. The form without hyphens is called BASIC DATE while the form with hyphens is called EXTENDED DATE.

Upvotes: 1

Related Questions