Inconsistent formatting of the `w` character from joda to java.time

My team wants to go from Joda time to java.time, but we see different behavior when formatting using the same template. The problem occurs when we use the weekday wweekday symbol :

final String dateString = "2016-01-04 00:00:00";
final String inputPattern = "yyyy-MM-dd HH:mm:ss";

// parse the input string using Joda
final org.joda.time.format.DateTimeFormatter jodaInputFormatter = org.joda.time.format.DateTimeFormat.forPattern(inputPattern);
final org.joda.time.DateTime jodaDateTime = jodaInputFormatter.parseDateTime(dateString);

// parse the input string using two different java.time classes
final java.time.format.DateTimeFormatter javaTimeInputFormatter = java.time.format.DateTimeFormatter.ofPattern(inputPattern).withZone(java.time.ZoneOffset.UTC);
final java.time.LocalDateTime localDateTime = java.time.LocalDateTime.parse(dateString, javaTimeInputFormatter);
final java.time.ZonedDateTime zonedDateTime = java.time.ZonedDateTime.parse(dateString, javaTimeInputFormatter);

final String outputPattern = "'week' w - dd/MM/yyyy HH:mm:ss";
final org.joda.time.format.DateTimeFormatter jodaOutputFormatter = org.joda.time.format.DateTimeFormat.forPattern(outputPattern);
final java.time.format.DateTimeFormatter javaTimeOutputFormatter = java.time.format.DateTimeFormatter.ofPattern(outputPattern);

// output: week 1 - 04/01/2016 00:00:00
System.out.println("With joda: " + jodaOutputFormatter.print(jodaDateTime));
// output: week 2 - 04/01/2016 00:00:00
System.out.println("With LocalDateTime: " + javaTimeOutputFormatter.format(localDateTime));
// output: week 2 - 04/01/2016 00:00:00
System.out.println("With ZonedDateTime: " + javaTimeOutputFormatter.format(zonedDateTime));

For some reason, output from a character is wdisabled through two implementations.

What causes this inconsistency? Is the character winconsistently implemented through Joda's time and java.time?

+4
source share
2 answers

, , , , EST (-05: 00), , (-?). ISO-8601-. , 4 ( , ).

, 4 . . US- - 2016-01-01 2016-01-02 (2 - ). 3 , .

: java.time (JSR-310) w, . backport, . :

} else if (cur == 'w') {
    if (count > 2) {
        throw new IllegalArgumentException("Too many pattern letters: " + cur);
    }
    appendInternal(new WeekFieldsPrinterParser('w', count));

...

static final class WeekFieldsPrinterParser implements DateTimePrinterParser {
    private final char letter;
    private final int count;

    public WeekFieldsPrinterParser(char letter, int count) {
        this.letter = letter;
        this.count = count;
    }

    @Override
    public boolean print(DateTimePrintContext context, StringBuilder buf) {
        WeekFields weekFields = WeekFields.of(context.getLocale());
        DateTimePrinterParser pp = evaluate(weekFields);
        return pp.print(context, buf);
    }

WeekFields.of(context.getLocale()) "w" .

, Joda-Time ISO-8601-week-definition, , , , , 4 , , ISO-8601 . .

, Joda-Time 1 4 , java.time US-week 2.

, , ISO, , Joda-Time. , Locale.UK, , . . .

+4

: , - Java SE , - , , , Joda-Time.

Java SE IsoFields.WEEK_OF_WEEK_BASED_YEAR:

1 52 53, 53 .

- , , .

Joda-Time:

1 52-53 . 1. , .

1 2 2016 , , , Joda-Time . 4 , .

0

Source: https://habr.com/ru/post/1626822/


All Articles