Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of DecimalStyle

I fail to understand how DecimalStyle influences my formatting/parsing with DateTimeFormatter. If I try:

date = LocalDateTime.now();
DecimalStyle ds = DecimalStyle.of(Locale.GERMAN);
formatter = DateTimeFormatter.ofPattern("yyyy").withDecimalStyle(ds.withDecimalSeparator('?'));
text = formatter.format(date);
System.out.println("DecimalStyle: " + text);

Shouldn't I get something like: 2.016 in my Output? What I actually see is that whatever parameters I set to my ds, it will never influence the actual formatted String. This is java 8 API, so there's not much to find on the Internet, unfortunately.

like image 312
yuranos Avatar asked Jan 06 '23 10:01

yuranos


1 Answers

The DecimalStyle affects how 0 (zero), + (positive), - (negative) and . (decimal separator) characters are represented. There is no decimal character in 2016 (there could be a thousand separator but DecimalStyle does not handle that).

An example that shows a difference:

LocalDateTime date = LocalDateTime.now();
DecimalStyle ds = DecimalStyle.of(Locale.GERMAN);
DateTimeFormatter noDs = DateTimeFormatter.ISO_LOCAL_TIME;
DateTimeFormatter withDs = DateTimeFormatter.ISO_LOCAL_TIME.withDecimalStyle(ds);
System.out.println("Default: " + noDs.format(date));    // Default: 17:44:54.457
System.out.println("German:  " + withDs.format(date));  // German:  17:44:54,457
like image 90
assylias Avatar answered Jan 18 '23 01:01

assylias