Difference between 'yy' and 'YY' in Java Time Pattern [duplicate] - java

This question already has answers here:
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Why does sdf.format(date) converts 2018-12-30 to 2019-12-30 in java? [duplicate]
(3 answers)
Closed 3 years ago.
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?

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.

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

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

Related

Date using 'YYYY'

This is a question related to Java Date year calculation is off by year for two days
I understand the problem appeared from using 'YYYY' instead of 'yyyy', whereby 'YYYY' refers to calendar year instead of the actual year, resulting in the year being wrong if the dates fell onto the first week of January's calendar year.
I tried to read further and understand the problem in
https://docs.oracle.com/javase/9/docs/api/java/util/GregorianCalendar.html#week_year
And it says
"A week year is in sync with a WEEK_OF_YEAR cycle. All weeks between the first and last weeks (inclusive) have the same week year value. Therefore, the first and last days of a week year may have different calendar year values."
I have been trying to see if there are any time of the year where 01-Jan-XXXX is actually displayed as 01-Jan-(XXXX-1) but have not managed to find any. Is there a case where this may happen?
I did something simple to take string dates and print out using YYYYMMdd format
public static void main(String[] args) throws ParseException
{
Calendar testCalendar = Calendar.getInstance();
System.out.println("First day of the week: " + testCalendar.getFirstDayOfWeek());
System.out.println("Minimal Days in First Week: " + testCalendar.getMinimalDaysInFirstWeek());
SimpleDateFormat YYYYMMdd= new SimpleDateFormat("YYYYMMdd");
String dateString = "01/01/2016";
Date date = new Date();
date = new SimpleDateFormat("dd/MM/yyyy").parse(dateString);
testCalendar.setTime(date);
int week = testCalendar.get(Calendar.WEEK_OF_YEAR);
String date2 = YYYYMMdd.format(date);
System.out.println("Week Number: " + week);
System.out.println("Date: " + date2);
}
And the output was
First day of the week: 1
Minimal Days in First Week: 1
Week Number: 1
Date: 20161231
If I change the date to "01/01/2016"
The output was
First day of the week: 1
Minimal Days in First Week: 1
Week Number: 1
Date: 20160101
So 01/01/2016 is the first week of of 2016, and not week 53 of 2015.
For a concrete answer, the following table shows January 1 for each year from 2010 through 2020 with day-of-week, week-based year and week number in ISO (the international standard) and in the US.
Year DOW ISO US
2010 Fri 2009-53 2010-01
2011 Sat 2010-52 2011-01
2012 Sun 2011-52 2012-01
2013 Tue 2013-01 2013-01
2014 Wed 2014-01 2014-01
2015 Thu 2015-01 2015-01
2016 Fri 2015-53 2016-01
2017 Sun 2016-52 2017-01
2018 Mon 2018-01 2018-01
2019 Tue 2019-01 2019-01
2020 Wed 2020-01 2020-01
As you can see, internationally January 1 regularly falls in week 52 or 53 of the previous year, while in the US it always falls in week 1 of its own year.
International rule: Week 1 is the first one that contains at least 4 days of the new year. In other words, a week belongs in the year where most of its days are. In yet other words, week 1 is the one that holds the first Thursday of the year (since weeks begin on Mondays). This implies that when January 1 is a Friday, Saturday or Sunday, it belongs to the last week of the previous year.
US rule: Week 1 is the week that contains January 1. That January 1 always falls in week 1 of its own year follows from this definition (weeks begin on Sundays).
The table came out of this snippet:
System.out.println("Year DOW ISO US");
for (int year = 2010; year <= 2020; year++) {
LocalDate jan1 = LocalDate.of(year, Month.JANUARY , 1);
System.out.format(Locale.ROOT, "%4d %3s %4d-%02d %4d-%02d%n",
year, jan1.getDayOfWeek().getDisplayName(TextStyle.SHORT_STANDALONE, Locale.ROOT),
jan1.get(WeekFields.ISO.weekBasedYear()), jan1.get(WeekFields.ISO.weekOfWeekBasedYear()),
jan1.get(WeekFields.SUNDAY_START.weekBasedYear()), jan1.get(WeekFields.SUNDAY_START.weekOfWeekBasedYear()));
}
I am of course using java.time, the modern Java date and time API. I warmly recommend it over the outdated Calendar, SimpleDateFormat and Date.
No. The week year is only to be used in conjunction with the week. For example the 1st January 2016 (a Friday) is in the 53. week of 2015. It will never be displayed as 1. January 2015 since that would be ambiguous.
Calendar d = Calendar.getInstance();
d.set(Calendar.YEAR, 2016)
d.set(Calendar.MONTH, Calendar.JANUARY);
d.set(Calendar.DATE, 1);
SimpleDateFormat ft = new SimpleDateFormat("w-YYYY");
ft.format(d.getTime());
// => "53-2015"
In the US, the first week of the year is defined as being the week containing January 1*.
As a consequence, the week-year for January 1 will always be the same as the calendar year, in the US.
*https://en.wikipedia.org/wiki/Week#Week_numbering

get the start and end dates of the week using java [duplicate]

This question already has answers here:
Get last week date range for a date in Java
(5 answers)
Closed 5 years ago.
My requirement is to get the start and end date of the week when date is passed. I have searched and i found tons of answers but confused with which one is best to use.In one of the thread i found the below code:
Calendar c = Calendar.getInstance();
c.setTime(new Date("8/16/2017"));
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
System.out.println("day :" + dayOfWeek);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
System.out.println("start of week day :" + c.getTime());
output:
day :4
start of week day :Sun Aug 13 00:00:00 EDT 2017
I see a bug in the above code output. Start of the week should be Monday Aug14 but it shows Sun Aug13. Any suggestions to get the start date and end date of the week when date is passed as a String dynamically.
--EDITED--
I'm looking for java code which returns the first and last day date's of the week when date is passed.
import java.time.LocalDate;
import static java.time.DayOfWeek.MONDAY;
import static java.time.DayOfWeek.SUNDAY;
import static java.time.temporal.TemporalAdjusters.nextOrSame;
import static java.time.temporal.TemporalAdjusters.previousOrSame;
public class FirstAndLast
{
public static void main(String[] args)
{
LocalDate today = LocalDate.now();
LocalDate monday = today.with(previousOrSame(MONDAY));
LocalDate sunday = today.with(nextOrSame(SUNDAY));
System.out.println("Today: " + today);
System.out.println("Monday of the Week: " + monday);
System.out.println("Sunday of the Week: " + sunday);
}
}
Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.MONDAY); //Line2
c.setTime(new Date("8/16/2017"));
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
System.out.println("day :" + dayOfWeek);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
System.out.println("start of week day :" + c.getTime());
Set the first day of the week to Monday as in line 2.
Now the output will be
day :4
start of week day :Mon Aug 14 00:00:00 EDT 2017

Issue with SimpleDateFormat

I am working on date format with SimpleDateFormat. But when I format my date in provide format, year value increment by 1.The condition happen with that value. Why this happen?
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-YYYY");
System.out.println("dob - "+date); // Java date
System.out.println("formatted date - "+sdf.format(date));
Output as follows :
dob - Sun Dec 28 00:00:00 IST 1975
formatted date - 28-12-1976
Year value increment by 1. Why this happen.
As per Oracle docs
capital Y means week year,you should use yyyy in place of YYYY
change SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-YYYY"); to
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
You should use SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
as y means year while 'Y' means week Year.
Below are supported codes to be used in SimpleDateFormat.
Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
Y Week year Year 2009; 09
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day name in week Text Tuesday; Tue
u Day number of week (1 = Monday, ..., 7 = Sunday) Number 1
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
X Time zone ISO 8601 time zone -08; -0800; -08:00
Try to use "yyyy" instead of "YYYY".
See for more info:
SimpleDateFormat producing wrong date time when parsing "YYYY-MM-dd HH:mm"
Y means "Week year"
y means "Calendar year"

Weeks in month June 2014

Sorry for asking daft question but I cannot get correct number of weeks in June 2014 returned by Calendar:
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MONTH, Calendar.JUNE);
calendar.set(Calendar.YEAR, 2014);
calendar.setFirstDayOfWeek(Calendar.MONDAY);
System.out
.println("first day of week: " + calendar.getFirstDayOfWeek());
System.out.println("weeks in month: "
+ calendar.getActualMaximum(Calendar.WEEK_OF_MONTH));
System.out.println("days in month: "
+ calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
}
I am getting:
first day of week: 2
weeks in month: 5
days in month: 30
Why number of weeks in June 2014 is not 6? I am using jdk1.8.0_05 on Mac OS X 10.9.3.
The definition of a week depends on what each Locale (country, region, whatever) defines as the first day of the week. You can check that with Calendar#getFirstDayOfWeek(). It also depends on what it considers the minimal days in the first week should be. You can get that with Calendar#getMinimalDaysInFirstWeek(). Your Locale seems to show that it needs more than one day to consider that period a week.
For example, with Locale.CANADA, I get 6 weeks since the getMinimalDaysInFirstWeek() returns 1.

Why Java Calendar set(int year, int month, int date) not returning correct date? [duplicate]

This question already has answers here:
Why is January month 0 in Java Calendar?
(18 answers)
Closed 3 years ago.
According to doc, calendar set() is:
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html#set%28int,%20int,%20int%29
set(int year, int month, int date)
Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.
code:
Calendar c1 = GregorianCalendar.getInstance();
c1.set(2000, 1, 30); //January 30th 2000
Date sDate = c1.getTime();
System.out.println(sDate);
output:
Wed Mar 01 19:32:21 JST 2000
Why it's not Jan 30 ???
1 for month is February. The 30th of February is changed to 1st of March.
You should set 0 for month. The best is to use the constant defined in Calendar:
c1.set(2000, Calendar.JANUARY, 30);
Months in Calendar object start from 0
0 = January = Calendar.JANUARY
1 = february = Calendar.FEBRUARY
Selected date at the example is interesting. Example code block is:
Calendar c1 = GregorianCalendar.getInstance();
c1.set(2000, 1, 30); //January 30th 2000
Date sDate = c1.getTime();
System.out.println(sDate);
and output Wed Mar 01 19:32:21 JST 2000.
When I first read the example i think that output is wrong but it is true:)
Calendar.Month is starting from 0 so 1 means February.
February last day is 28 so output should be 2 March.
But selected year is important, it is 2000 which means February 29 so result should be 1 March.

Categories

Resources