This question already has answers here:
Parsing a string to date format in java defaults date to 1 and month to January
(2 answers)
Closed 4 years ago.
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, YYYY, EEE", Locale.US);
System.out.println(sdf.format(new Date()));
System.out.println(sdf.format(sdf.parse("Apr 27, 2018, Fri")));
Java does not parse the date as expected and outputs:
Apr 27, 2018, Fri
Jan 05, 2018, Fri // I can not understand why java parse the month of April as January
Your problem is that you are using YYYY, which represents Week year, whereas you wanted yyyy which represents year, see all options here.
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy, EEE", Locale.US);
As per SimpleDateFormat Javadoc
y Year
Y Week year
So you want to use yyyy instead YYYY:
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy, EEE", Locale.US);
System.out.println(sdf.format(new Date()));
System.out.println(sdf.format(sdf.parse("Apr 27, 2018, Fri")));
will result int:
Apr 27, 2018, Fri
Apr 27, 2018, Fri
Do note that you probably want to set sdf.setLenient(false) when using SimpleDateFormat.
As explained in the other answers, what you want is yyyy instead of YYYY .
Now to add further information about the resut you get :
Date String : "Apr 27, 2018, Fri"
Format : "MMM dd, YYYY, EEE" (YYYY being week year)
Quoting from How does Java “week year” really work?
On parsing, SimpleDateFormat expects a matching set of values: either
day, month, year or day of week, week in year, week-year. Since you
supplied a week-year but did not supply day of week and week in year,
those to values have been assumed as 1.
You don't have the week in year part (or your format would contain w), so it is assumed to be 1 .
You end up having friday of the first week of the year 2018, which is :
Jan 05, 2018, Fri
(The day and the month from your date get discarded once the format hits the YYYY part, and are replaced by the result of the "year week" computing. )
Y stands for week year
From Java doc
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.
For example, January 1, 1998 is a Thursday. If getFirstDayOfWeek() is
MONDAY and getMinimalDaysInFirstWeek() is 4 (ISO 8601 standard
compatible setting), then week 1 of 1998 starts on December 29, 1997,
and ends on January 4, 1998. The week year is 1998 for the last three
days of calendar year 1997. If, however, getFirstDayOfWeek() is
SUNDAY, then week 1 of 1998 starts on January 4, 1998, and ends on
January 10, 1998; the first three days of 1998 then are part of week
53 of 1997 and their week year is 1997.
Related
This question already has answers here:
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
How does Java "week year" really work?
(2 answers)
Closed 3 years ago.
I found strange behaviour when using Java's DateTimeFormatter on LocalDate for 30th and 31st of December.
LocalDate date1 = LocalDate.of(2019, Month.DECEMBER, 30);
System.out.println("date1: " + date1);
System.out.println("converted date1: " + DateTimeFormatter.ofPattern("YYYY-MM-dd").format(date1));
LocalDate date2 = LocalDate.of(2019, Month.JANUARY, 30);
System.out.println("date2: " + date2);
System.out.println("converted date2: " + DateTimeFormatter.ofPattern("YYYY-MM-dd").format(date2));
Output:
date1: 2019-12-30
converted date1: 2020-12-30
date2: 2019-01-30
converted date2: 2019-01-30
The first date (December 30) is converted with the next year, the second date (January 30) is converted with the correct year.
Am I missing something out, or is it a bug?
Y means "year of week-based year". It's not the same as y which means "year of era" as per docs.
This post explains the difference and purpose of Y:
A week year is a year where all the weeks in the year are whole weeks. This is specified by some standard (which I don't remember at the moment). Basically, this guarantees that a program working on a week's data will not transition between years. Unfortunately, this also means that the beginning of the year may not start on the first of January. What year a particular day belongs in depends on these rules, and of course, there are days where the year and the week year are different.
In your example 30st Dec '19 was a Monday, 31st Dec '19 was a Tuesday and 1st Jan '20 was Wednesday so "year of week-based year" for this three days is 2020.
Change 'Y' to 'y' (Capital case to lower case) 'Y' is week based year and since most of the week of Dec-30, 2019 falls into 2020 the year is 2020. while lower case 'y' is your regular year. See JavaDoc for DateTimeFormatter for details
This question already has answers here:
Cant parse recently formatted ZonedDateTime Java 8
(1 answer)
Java LocalDate Formatting of 2000-1-2 error [duplicate]
(2 answers)
Difference between year-of-era and week-based-year?
(7 answers)
Closed 4 years ago.
I am using DateTimeFormatter to format my LocalDate which has the value 31 Jan 2019.
When I execute the code below, the correct date is returned:
// 31, Dec, 2018
System.out.println(LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("dd, MMM, yyyy")));
But if I execute the code below, the date is returned with the correct date, month but the wrong year:
// 31, Dec, 2019
System.out.println(LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("dd, MMM, YYYY")));
What I am confused about is that, if I also use the same pattern above and I just minusDays(30) which makes my expected to be 01 Dec 2018, the correct result is returned:
// 01, Dec, 2018
System.out.println(LocalDate.now().minusMonths(1).minusDays(30).format(DateTimeFormatter.ofPattern("dd, MMM, YYYY")));
Could using YYYY in the DateTimeFormatter.ofPattern("dd, MMM, YYYY")) pattern because this?
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
class Example {
public static void main(String[] args) {
/*Today's date is 31st of January 2019*/
// 2018-12-31
System.out.println(LocalDate.now().minusMonths(1));
// 31, Dec, 2018
System.out.println(LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("dd, MMM, yyyy")));
// 31, Dec, 2019
System.out.println(LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("dd, MMM, YYYY")));
// 01, Dec, 2018
System.out.println(LocalDate.now().minusMonths(1).minusDays(30).format(DateTimeFormatter.ofPattern("dd, MMM, YYYY")));
// 31, Dec, 2018
System.out.println(LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("dd, MMM, uuuu")));
}
}
As far as I understood reading the documentation is because of this:
"YY" specifies the week-based year numbering and for normal year numbering, you should use "yy" instead.
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
EDIT:
The week and year numbering in an ISO-8601 calendar is different from a standard Gregorian calendar. Here’s how January 2nd, 2011 would be represented:
Calendar System Week Number Year Number
Standard Gregorian 1 2011
ISO-8601 52 2010
The Gregorian leap cycle, which has 97 leap days spread across 400 years, contains a whole number of weeks (20871). In every cycle there are 71 years with an additional 53rd week (corresponding to the Gregorian years that contain 53 Thursdays). An average year is exactly 52.1775 weeks long; months (1⁄12 year) average at exactly 4.348125 weeks.
An ISO week-numbering year (also called ISO year informally) has 52 or 53 full weeks. That is 364 or 371 days instead of the usual 365 or 366 days. The extra week is sometimes referred to as a leap week, although ISO 8601 does not use this term.
For more info: https://en.wikipedia.org/wiki/ISO_week_date
This question already has answers here:
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Closed 7 years ago.
I'm getting very weird results, which I can not understand.
public class Test {
private static DateFormat df = new SimpleDateFormat("dd.MM.YYYY HH:mm");
public static void main(String[] args) {
Date d = new Date(1356912000000L);
System.out.println(d);
System.out.println(df.format(d));
}
}
Gives the output:
Mon Dec 31 01:00:00 CET 2012
31.12.2013 01:00
I assume that this might be some issues with locales, but that's a shift by a whole year ! May anyone explain why it performs this way ?
YYYY is the week-year, not the calendar year. You want yyyy instead. Here's Java's relevant details:
Week Of Year and Week Year
Values calculated for the WEEK_OF_YEAR field range from 1 to 53. The
first week of a calendar year is the earliest seven day period
starting on getFirstDayOfWeek() that contains at least
getMinimalDaysInFirstWeek() days from that year. It thus depends on
the values of getMinimalDaysInFirstWeek(), getFirstDayOfWeek(), and
the day of the week of January 1. Weeks between week 1 of one year and
week 1 of the following year (exclusive) are numbered sequentially
from 2 to 52 or 53 (except for year(s) involved in the
Julian-Gregorian transition).
The getFirstDayOfWeek() and getMinimalDaysInFirstWeek() values are
initialized using locale-dependent resources when constructing a
GregorianCalendar. The week determination is compatible with the ISO
8601 standard when getFirstDayOfWeek() is MONDAY and
getMinimalDaysInFirstWeek() is 4, which values are used in locales
where the standard is preferred. These values can explicitly be set by
calling setFirstDayOfWeek() and setMinimalDaysInFirstWeek().
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.
For example, January 1, 1998 is a Thursday. If getFirstDayOfWeek() is
MONDAY and getMinimalDaysInFirstWeek() is 4 (ISO 8601 standard
compatible setting), then week 1 of 1998 starts on December 29, 1997,
and ends on January 4, 1998. The week year is 1998 for the last three
days of calendar year 1997. If, however, getFirstDayOfWeek() is
SUNDAY, then week 1 of 1998 starts on January 4, 1998, and ends on
January 10, 1998; the first three days of 1998 then are part of week
53 of 1997 and their week year is 1997.
Instead of:
"dd.MM.YYYY HH:mm"
Use:
"dd.MM.yyyy HH:mm"
This question already has answers here:
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Closed 8 years ago.
I am using SimpleDateFormat("dd-MMM-YYYY") in my code, which is giving wrong output.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-YYYY");
System.out.println("Actual date : "+new Date()+" after Formatting : "+ dateFormat.format(new Date()));
above code is giving :
Actual date : Tue Dec 30 13:51:06 IST 2014 after Formatting : 30-Dec-2015
Above code is print date having Year as 1 year ahead.
and this issue is replicable for 28-31 december 2014 dates only.
Thanks in advance.
--Ajay
You're using YYYY, which is the "ISO-8601 week year". That should almost always be used in conjunction with w, "week in year". You want yyyy to show the normal calendar year.
The reason they're different is that the first ISO-8601 week of a year is the first (Monday to Sunday) week that includes at least 4 days. This means that the first week of the year is the one containing the first Thursday. As January 1st 2015 falls on a Thursday, that means that the week of 2014-12-29 to 2015-01-04 is all "week year 2015, week of year 1". (I'm surprised if you see if for December 28th...)
In other years, the first few days of the year are in week 52 or 53 of the previous year. For example, January 1st 2010 was in week 53 of week-year 2009, and January 1st 2011 was in week 52 of week-year 2010.
I am trying to figure out how to make an alarm on a specific day of the week. Here is sample code that prints what I expect:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.WEEK_OF_MONTH, 1);
System.out.println("Week of month: " + calendar.get(Calendar.WEEK_OF_MONTH)); // Week of month: 1
But when I add this to the code I get a result I dont understand:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
calendar.set(Calendar.WEEK_OF_MONTH, 1);
System.out.println("Week of month: " + calendar.get(Calendar.WEEK_OF_MONTH)); // Week of month: 5
I am trying to set an alarm for a specific day in a specific week. The date today is June 15th, 2013. Could someone please explain this to me. Joda time is not an option with what I am doing, so I need to make this work with the regular Java libs. Thanks for all the help.
Editor's note
The above code will return different results based on the date it is run, because Calendar.getInstance() returns a Calendar set to the current time. For an illustration of the problem that is independent of the current time, use this:
DateFormat dateFormat = DateFormat.getDateInstance();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(1371294000000L);
System.out.println(dateFormat.format(calendar.getTime()));
calendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
System.out.println(dateFormat.format(calendar.getTime()));
calendar.set(Calendar.WEEK_OF_MONTH, 1);
System.out.println("Week of month: " + calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println(dateFormat.format(calendar.getTime()));
Which outputs something like this (at least if your default locale is using the Gregorian Calendar):
Jun 15, 2013
Jun 13, 2013
Week of month: 5
May 30, 2013
Java Calendar is lenient by default. If you set it to January 32, it will translate that to Feb 1. Furthermore, the definition of "week" is Locale-specific and a bit confusing. This is explained in great detail in the JavaDocs linked to above.
In your particular case (or at least in my Locale), a week is defined as starting on Sunday and the "first week of June" is defined as the Sunday through Saturday period that includes June 1. June 1, 2013, is a Saturday, the last day in week 22 of the year 2013. Sunday, June 2, 2013, is the first day of week 2 of June, not the second day of week 1.
Since there is no Thursday in week 1 of June, the lenient calendar interprets DAY_OF_WEEK, THURSDAY to be the Thursday of week 22 of year 2013, which is May 30, 2013, which is week 5 of May, so you get 5 and not 1 in your output.
To set the first Thursday in June, you want:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, Calendar.JUNE);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
DAY_OF_WEEK_IN_MONTH can be a bit confusing. You use it with DAY_OF_WEEK to specify which occurrence of that day in that month you want. So when DAY_OF_WEEK is set to Thursday, DAY_OF_WEEK_IN_MONTH, 1 is the first Thursday of the month, DAY_OF_WEEK_IN_MONTH, 2 is the second Thursday of the month, etc. This is much less Locale-specific or open to (mistaken) interpretation then what days constitute the first week of the month.