10

From document SimpleDateTimePattern, yy should be the same with YY.

Today is Dec 30, 2019, now we get YY for today is 20, yy for today is 19. What's the difference between yy and YY in Java Time Pattern?

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
user2992029
  • 161
  • 1
  • 2
  • 6
  • 1
    I would suggest to use `java.time` API instead of legacy library – Youcef LAIDANI Dec 30 '19 at 08:50
  • @YCF_L The difference between `y` and `Y` also applies to the [`java.time.format.DateTimeFormatter`](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html) – Mark Rotteveel Dec 30 '19 at 09:05
  • 1
    @MarkRotteveel yes I know that, I put that comment to the OP to avoid using the legacy library. – Youcef LAIDANI Dec 30 '19 at 09:09
  • I recommend you don’t use `SimpleDateFormat`. That class is notoriously troublesome and long outdated. Instead use `DateTimeFormatter` and other classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jan 02 '20 at 19:44

3 Answers3

16

yy is the calendar year, while YY is a week year. A week year can be different from the calendar year depending on which day the first of January falls. For example see ISO-8601 week year.

Today (30 December 2019) is a good example, the calendar year is 2019, but the week year is 2020, because this week is week 1 of 2020. So yy will result in 19, while YY results in 20.

The definition of the first week of a year from the wikipedia page:

The ISO 8601 definition for week 01 is the week with the Gregorian year's first Thursday in it. The following definitions based on properties of this week are mutually equivalent, since the ISO week starts with Monday:

  • It is the first week with a majority (4 or more) of its days in January.
  • Its first day is the Monday nearest to 1 January.
  • It has 4 January in it. Hence the earliest possible first week extends from Monday 29 December (previous Gregorian year) to Sunday 4 January, the latest possible first week extends from Monday 4 January to Sunday 10 January.
  • It has the year's first working day in it, if Saturdays, Sundays and 1 January are not working days.

If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in week 01. If 1 January is on a Friday, it is part of week 53 of the previous year. If it is on a Saturday, it is part of the last week of the previous year which is numbered 52 in a common year and 53 in a leap year. If it is on a Sunday, it is part of week 52 of the previous year.

Some locales, like for example the US, don't follow ISO-8601, because there they use Sunday as the first day of the week, but they have similar rules for week years.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
4

You have it in you link:

y Year Year 1996; 96

Y Week year Year 2009; 09

Week year can be different, for example this new year week, than current year

Week year defines as year's first Thursday:

The first week of the year is the week that contains that year's first Thursday

Community
  • 1
  • 1
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
1

They both represent a year but yyyy represents the calendar year while YYYY represents the year of the week.

An example illustrates this much better than words ever could.

package datetest;    

import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {

    public static void main(String[] args) {
        try {
            String[] dates = {"2018-12-01", "2018-12-31", "2019-01-01"};
            for (String date: dates) {
                SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd");
                Date d = dt.parse(date);

                SimpleDateFormat dtYYYY = new SimpleDateFormat("YYYY");
                SimpleDateFormat dtyyyy = new SimpleDateFormat("yyyy");

                System.out.println("For date :" + date + " the YYYY year is : " + dtYYYY.format(d) + " while for yyyy it's " + dtyyyy.format(d));
            }
        } catch (Exception e) {
            System.out.println("Failed with exception: " + e);
        }
    }
}

Output

For date : 2018-12-01 the YYYY year is : 2018 while for yyyy it's 2018
For date : 2018-12-31 the YYYY year is : 2019 while for yyyy it's 2018
For date : 2019-01-01 the YYYY year is : 2019 while for yyyy it's 2019
Naresh Kumar
  • 794
  • 8
  • 25