37
37
import java .time .temporal .ChronoField ;
38
38
import java .time .temporal .IsoFields ;
39
39
import java .time .temporal .TemporalAccessor ;
40
- import java .time .temporal .TemporalAdjusters ;
41
40
import java .time .temporal .TemporalQueries ;
42
41
import java .time .temporal .TemporalQuery ;
43
42
import java .time .temporal .WeekFields ;
53
52
import static java .time .temporal .ChronoField .SECOND_OF_MINUTE ;
54
53
55
54
public class DateFormatters {
55
+ public static final WeekFields WEEK_FIELDS = WeekFields .of (DayOfWeek .MONDAY ,4 );
56
56
57
57
private static final DateTimeFormatter TIME_ZONE_FORMATTER_NO_COLON = new DateTimeFormatterBuilder ()
58
58
.appendOffset ("+HHmm" , "Z" )
@@ -946,14 +946,14 @@ public class DateFormatters {
946
946
* Returns a formatter for a four digit weekyear
947
947
*/
948
948
private static final DateFormatter STRICT_WEEKYEAR = new JavaDateFormatter ("strict_weekyear" , new DateTimeFormatterBuilder ()
949
- .appendValue (WeekFields . ISO .weekBasedYear (), 4 , 10 , SignStyle .EXCEEDS_PAD )
949
+ .appendValue (WEEK_FIELDS .weekBasedYear (), 4 , 10 , SignStyle .EXCEEDS_PAD )
950
950
.toFormatter (Locale .ROOT )
951
951
.withResolverStyle (ResolverStyle .STRICT ));
952
952
953
953
private static final DateTimeFormatter STRICT_WEEKYEAR_WEEK_FORMATTER = new DateTimeFormatterBuilder ()
954
- .appendValue (WeekFields . ISO .weekBasedYear (), 4 , 10 , SignStyle .EXCEEDS_PAD )
954
+ .appendValue (WEEK_FIELDS .weekBasedYear (), 4 , 10 , SignStyle .EXCEEDS_PAD )
955
955
.appendLiteral ("-W" )
956
- .appendValue (WeekFields . ISO .weekOfWeekBasedYear (), 2 , 2 , SignStyle .NOT_NEGATIVE )
956
+ .appendValue (WEEK_FIELDS .weekOfWeekBasedYear (), 2 , 2 , SignStyle .NOT_NEGATIVE )
957
957
.toFormatter (Locale .ROOT )
958
958
.withResolverStyle (ResolverStyle .STRICT );
959
959
@@ -972,7 +972,7 @@ public class DateFormatters {
972
972
new DateTimeFormatterBuilder ()
973
973
.append (STRICT_WEEKYEAR_WEEK_FORMATTER )
974
974
.appendLiteral ("-" )
975
- .appendValue (WeekFields . ISO .dayOfWeek ())
975
+ .appendValue (WEEK_FIELDS .dayOfWeek ())
976
976
.toFormatter (Locale .ROOT )
977
977
.withResolverStyle (ResolverStyle .STRICT ));
978
978
@@ -1162,7 +1162,7 @@ public class DateFormatters {
1162
1162
* Returns a formatter for a four digit weekyear. (YYYY)
1163
1163
*/
1164
1164
private static final DateFormatter WEEK_YEAR = new JavaDateFormatter ("week_year" ,
1165
- new DateTimeFormatterBuilder ().appendValue (WeekFields . ISO .weekBasedYear ()).toFormatter (Locale .ROOT )
1165
+ new DateTimeFormatterBuilder ().appendValue (WEEK_FIELDS .weekBasedYear ()).toFormatter (Locale .ROOT )
1166
1166
.withResolverStyle (ResolverStyle .STRICT ));
1167
1167
1168
1168
/*
@@ -1591,9 +1591,9 @@ public class DateFormatters {
1591
1591
*/
1592
1592
private static final DateFormatter WEEKYEAR_WEEK = new JavaDateFormatter ("weekyear_week" , STRICT_WEEKYEAR_WEEK_FORMATTER ,
1593
1593
new DateTimeFormatterBuilder ()
1594
- .appendValue (WeekFields . ISO .weekBasedYear ())
1594
+ .appendValue (WEEK_FIELDS .weekBasedYear ())
1595
1595
.appendLiteral ("-W" )
1596
- .appendValue (WeekFields . ISO .weekOfWeekBasedYear ())
1596
+ .appendValue (WEEK_FIELDS .weekOfWeekBasedYear ())
1597
1597
.toFormatter (Locale .ROOT )
1598
1598
.withResolverStyle (ResolverStyle .STRICT )
1599
1599
);
@@ -1606,15 +1606,15 @@ public class DateFormatters {
1606
1606
new DateTimeFormatterBuilder ()
1607
1607
.append (STRICT_WEEKYEAR_WEEK_FORMATTER )
1608
1608
.appendLiteral ("-" )
1609
- .appendValue (WeekFields . ISO .dayOfWeek ())
1609
+ .appendValue (WEEK_FIELDS .dayOfWeek ())
1610
1610
.toFormatter (Locale .ROOT )
1611
1611
.withResolverStyle (ResolverStyle .STRICT ),
1612
1612
new DateTimeFormatterBuilder ()
1613
- .appendValue (WeekFields . ISO .weekBasedYear ())
1613
+ .appendValue (WEEK_FIELDS .weekBasedYear ())
1614
1614
.appendLiteral ("-W" )
1615
- .appendValue (WeekFields . ISO .weekOfWeekBasedYear ())
1615
+ .appendValue (WEEK_FIELDS .weekOfWeekBasedYear ())
1616
1616
.appendLiteral ("-" )
1617
- .appendValue (WeekFields . ISO .dayOfWeek ())
1617
+ .appendValue (WEEK_FIELDS .dayOfWeek ())
1618
1618
.toFormatter (Locale .ROOT )
1619
1619
.withResolverStyle (ResolverStyle .STRICT )
1620
1620
);
@@ -1858,7 +1858,7 @@ public static ZonedDateTime from(TemporalAccessor accessor) {
1858
1858
} else if (isLocalDateSet ) {
1859
1859
return localDate .atStartOfDay (zoneId );
1860
1860
} else if (isLocalTimeSet ) {
1861
- return of (getLocaldate (accessor ), localTime , zoneId );
1861
+ return of (getLocalDate (accessor ), localTime , zoneId );
1862
1862
} else if (accessor .isSupported (ChronoField .YEAR ) || accessor .isSupported (ChronoField .YEAR_OF_ERA ) ) {
1863
1863
if (accessor .isSupported (MONTH_OF_YEAR )) {
1864
1864
return getFirstOfMonth (accessor ).atStartOfDay (zoneId );
@@ -1868,26 +1868,29 @@ public static ZonedDateTime from(TemporalAccessor accessor) {
1868
1868
}
1869
1869
} else if (accessor .isSupported (MONTH_OF_YEAR )) {
1870
1870
// missing year, falling back to the epoch and then filling
1871
- return getLocaldate (accessor ).atStartOfDay (zoneId );
1872
- } else if (accessor .isSupported (WeekFields .ISO .weekBasedYear ())) {
1873
- if (accessor .isSupported (WeekFields .ISO .weekOfWeekBasedYear ())) {
1874
- return Year .of (accessor .get (WeekFields .ISO .weekBasedYear ()))
1875
- .atDay (1 )
1876
- .with (WeekFields .ISO .weekOfWeekBasedYear (), accessor .getLong (WeekFields .ISO .weekOfWeekBasedYear ()))
1877
- .atStartOfDay (zoneId );
1878
- } else {
1879
- return Year .of (accessor .get (WeekFields .ISO .weekBasedYear ()))
1880
- .atDay (1 )
1881
- .with (TemporalAdjusters .firstInMonth (DayOfWeek .MONDAY ))
1882
- .atStartOfDay (zoneId );
1883
- }
1871
+ return getLocalDate (accessor ).atStartOfDay (zoneId );
1872
+ } else if (accessor .isSupported (WEEK_FIELDS .weekBasedYear ())) {
1873
+ return localDateFromWeekBasedDate (accessor ).atStartOfDay (zoneId );
1884
1874
}
1885
1875
1886
1876
// we should not reach this piece of code, everything being parsed we should be able to
1887
1877
// convert to a zoned date time! If not, we have to extend the above methods
1888
1878
throw new IllegalArgumentException ("temporal accessor [" + accessor + "] cannot be converted to zoned date time" );
1889
1879
}
1890
1880
1881
+ private static LocalDate localDateFromWeekBasedDate (TemporalAccessor accessor ) {
1882
+ if (accessor .isSupported (WEEK_FIELDS .weekOfWeekBasedYear ())) {
1883
+ return LocalDate .ofEpochDay (0 )
1884
+ .with (WEEK_FIELDS .weekBasedYear (), accessor .get (WEEK_FIELDS .weekBasedYear ()))
1885
+ .with (WEEK_FIELDS .weekOfWeekBasedYear (), accessor .get (WEEK_FIELDS .weekOfWeekBasedYear ()))
1886
+ .with (ChronoField .DAY_OF_WEEK , WEEK_FIELDS .getFirstDayOfWeek ().getValue ());
1887
+ } else {
1888
+ return LocalDate .ofEpochDay (0 )
1889
+ .with (WEEK_FIELDS .weekBasedYear (), accessor .get (WEEK_FIELDS .weekBasedYear ()))
1890
+ .with (ChronoField .DAY_OF_WEEK , WEEK_FIELDS .getFirstDayOfWeek ().getValue ());
1891
+ }
1892
+ }
1893
+
1891
1894
/**
1892
1895
* extending the java.time.temporal.TemporalQueries.LOCAL_DATE implementation to also create local dates
1893
1896
* when YearOfEra was used instead of Year.
@@ -1915,9 +1918,11 @@ public String toString() {
1915
1918
}
1916
1919
};
1917
1920
1918
- private static LocalDate getLocaldate (TemporalAccessor accessor ) {
1919
- int year = getYear (accessor );
1920
- if (accessor .isSupported (MONTH_OF_YEAR )) {
1921
+ private static LocalDate getLocalDate (TemporalAccessor accessor ) {
1922
+ if (accessor .isSupported (WEEK_FIELDS .weekBasedYear ())) {
1923
+ return localDateFromWeekBasedDate (accessor );
1924
+ } else if (accessor .isSupported (MONTH_OF_YEAR )) {
1925
+ int year = getYear (accessor );
1921
1926
if (accessor .isSupported (DAY_OF_MONTH )) {
1922
1927
return LocalDate .of (year , accessor .get (MONTH_OF_YEAR ), accessor .get (DAY_OF_MONTH ));
1923
1928
} else {
0 commit comments