Antonio Antonio - 20 days ago 8
Java Question

Java 8 DateFormatter week of month and year key symbols difference

I am new to

java.time
. Please, be patient. What is the difference between

Symbol Meaning Presentation Examples
------ ------- ------------ -------
F week-of-month number 3
W week-of-month number 4


isn't it just

Symbol Meaning Presentation Examples
------ ------- ------------ -------
F/W week-of-month number 3


and

Symbol Meaning Presentation Examples
------ ------- ------------ -------
Y week-based-year year 1996; 96
u year year 2004; 04
y year-of-era year 2004; 04


https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

Answer

The difference between u and y has already been addressed: What is the difference between year and year-of-era?

Difference between W and F

There is a bug in the javadoc for F.

W is the week of month, as represented by Weekfields::weekOfMonth:

This represents the concept of the count of weeks within the month where weeks start on a fixed day-of-week, such as Monday.

F is the aligned day of week in month, as represented by ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH:

This represents the concept of the count of days within the period of a week where the weeks are aligned to the start of the month.

F behaves similarly in DateTimeFormatter and SimpleDateFormat.

Example: if you use 2016-11-07 as an input, W will return 2 (2nd week of the month) but F will return 7 (7th day of the first aligned week, which starts on the 1st of the month).

Comments