Given:
A Year: 2019
A Calender Week Number: 1 (== 1st Week of a year)
A DayOfWeek: SUNDAY
Needed:
A transformation f of those Infos into a LocalDate Object so that
assertEquals(LocalDate.of(2019,1,6), f(2019,1,SUNDAY))
What I tried
I did not find a way with java.time.* to create a Date from an Info like "The Sunday of the first calender week in 2019". I found that the old java.util.Calendar class had a setWeekDate() function that could be usefull. But the following code caused an Exception:
...
Calendar c = Calendar.getInstance();
c.setWeekDate(2019, 1, Calendar.MONDAY);
c.setTimeZone(TimeZone.getDefault());
return LocalDate.from(c.toInstant());
java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: 2018-01-08T20:03:55.602Z of type java.time.Instant
at java.time.LocalDate.from(LocalDate.java:379)
at ...
Check ChronoField or IsoFields for a suitable way to get week for the calendar system that you want to use. LocalDate object that is used as "base" value should have a week value of the expected year.
int year = 2019;
int weekNumber = 1;
LocalDate localDate = LocalDate.of(year, 2, 1)
.with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, weekNumber)
.with(ChronoField.DAY_OF_WEEK, DayOfWeek.SUNDAY.getValue());
Selecting first date is meaningful. Using LocalDate.of(year, 1, 1) would give a day that can belong to last week of the previous year, same goes for LocalDate.now()but only for given period of time.
LocalDate.of(2017, 1, 1)
.with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 26)
.with(ChronoField.DAY_OF_WEEK, DayOfWeek.SUNDAY.getValue());
// 2016-07-03
LocalDate.of(2017, 1, 1).get(IsoFields.WEEK_OF_WEEK_BASED_YEAR);
// 52
So jumping from week 52 to week 26 would give date of 2016-07-03 because date is calculated by subtracking time between weeks 52 and 26.
Also see answer here for more discussion about withYear: Unexpected date calculation result
Related
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calender = Calendar.getInstance();
calender.set(Calendar.DAY_OF_MONTH, calender.getActualMaximum(Calendar.DATE));
int months = 1;
calender.add(Calendar.MONTH, months );
String time = sdf .format(calender .getTime());
System.out.println(time);
Since current month is April and last date is 2020-04-30
Next month last date I should get 2020-05-31
but I am getting last date as 2020-05-30
Any thing am i doing wrong ?
java.time
I recommend that you use java.time, the modern Java date and time API, for your date work. It’s much nicer to work with than the old classes Calendar and SimpleDateFormat.
LocalDate endOfNextMonth =
YearMonth // Represent an entire month in a particular year.
.now(ZoneId.of("Europe/Volgograd")) // Capture the current year-month as seen in a particular time zone. Returns a `YearMonth` object.
.plusMonths(1) // Move to the next month. Returns another `YearMonth` object.
.atEndOfMonth(); // Determine the last day of that year-month. Returns a `LocalDate` object.
String time = endOfNextMonth.toString(); // Represent the content of the `LocalDate` object by generating text in standard ISO 8601 format.
System.out.println("Last day of next month: " + time);
Output when running today:
Last day of next month: 2020-05-31
A YearMonth, as the name maybe says, is a year and month without day of month. It has an atEndOfMonth method that conveniently gives us the last day of the month as a LocalDate. A LocalDate is a date without time of day, so what we need here. And its toString method conveniently gives the format that you wanted (it’s ISO 8601).
Depending on the reason why you want the last day of another month there are a couple of other approaches you may consider. If you need to handle date ranges that always start and end on month boundaries, you may either:
Represent your range as a range of YearMonth objects. Would this free you from knowing the last day of the month altogether?
Represent the end of your range as the first of the following month exclusive. Doing math on the 1st of each month is simpler since it is always day 1 regardless of the length of the month.
What went wrong in your code?
No matter if using Calendar, LocalDate or some other class you need to do things in the opposite order: first add one month, then find the end of the month. As you know, months have different lengths, so the important part is getting the end of that month where you want to get the last day. Putting it the other way: setting either a LocalDate or a Calendar to the last day of the month correctly sets it to the last day of the month in qustion but does not instruct it to stay at the last day of the month after subsequent changes to its value, such as adding a month. If you add a month to April 29, you get May 29. If you add a month to April 30, you get May 30. Here it doesn’t matter that 30 is the last day of April while 30 is not the last day of May.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Wikipedia article: ISO 8601
You'd better use LocalDate like this:
LocalDate now = LocalDate.now();
LocalDate lastDay = now.withDayOfMonth(now.lengthOfMonth());
LocalDate nextMonth = lastDay.plusMonths(1);
Don't use deprecated classes from java.util.*.
Use classes from java.time.*.
Example with LocalDate :
public class Testing {
public static void main(String args[]) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date = LocalDate.now();
int months = 1;
date = date.plusMonths(months);
date = date.withDayOfMonth(date.lengthOfMonth());
System.out.println(date.format(dateTimeFormatter));
}
}
Output :
2020-05-31
Example with Calendar :
public class Testing {
public static void main(String args[]) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calender = Calendar.getInstance();
int months = 1;
calender.add(Calendar.MONTH, months);
calender.set(Calendar.DAY_OF_MONTH, calender.getActualMaximum(Calendar.DAY_OF_MONTH));
String time = sdf.format(calender.getTime());
System.out.println(time);
}
}
Output :
2020-05-31
I need to get Instant time from week number of year. Now I'm using old Calendar API to calculate time:
int week = 1; // week of year
final Calendar cal = new GregorianCalendar();
cal.set(0, 1, 0, 0, 0, 0); // reset calendar
cal.set(Calendar.YEAR, Year.now().getValue());
cal.set(Calendar.WEEK_OF_YEAR, week);
cal.setTimeZone(TimeZone.getTimeZone(ZoneOffset.UTC));
final Instant start = cal.getTime().toInstant();
final Instant end = start.plus(Period.ofWeeks(1));
Is it possible to get Instant from week of year using new Java time API (java.time package)?
WeekFields wf = WeekFields.of(Locale.getDefault());
int week = 1; // week of year
LocalDate startDate = LocalDate.now(ZoneOffset.UTC)
.with(wf.weekOfWeekBasedYear(), week)
.with(wf.dayOfWeek(), 1);
Instant startInstant = startDate.atStartOfDay(ZoneOffset.UTC).toInstant();
LocalDate endDate = startDate.plusWeeks(1);
Instant endInstant = endDate.atStartOfDay(ZoneOffset.UTC).toInstant();
System.out.println("" + startInstant + " - " + endInstant);
My locale uses ISO week numbers. The output here was:
2019-12-29T00:00:00Z - 2020-01-05T00:00:00Z
If you want ISO weeks independently of the locale, set wf to WeekFields.ISO. If you want some other week numbering scheme, set wf accordingly.
In case any other readers were wondering, Kirill is defining the end of the week as the first moment of the following week. This is recommended. It’s known as using half-open intervals for time intervals.
I also agree with the question that one should clearly prefer using java.time for this task rather than Calendar. Calendar is poorly designed and long outdated, and I believe that the code using java.time is clearer to read.
Furthermore, the code in the question using Calendar doesn’t set the day to the first day of the week, so will not give you that day. And while I haven’t tested, I suspect that the code will sometimes produce unexpected results around New Year.
My code using WeekFields from java.time will stay within the current week-based year, which is not the same as the calendar year. For example, if I had run it on December 30, 2019, it would still have given med week 1 of 2020 because we were already in that week.
You could adjust the week using the following methods:
LocalDateTime localDateTime = LocalDateTime.of(2020, 1, 1, 0, 0);
System.out.println(localDateTime); //2020-01-01T00:00
localDateTime = localDateTime.with(ChronoField.ALIGNED_WEEK_OF_YEAR, 2);
System.out.println(localDateTime); //2020-01-08T00:00
localDateTime = localDateTime.plusWeeks(10);
System.out.println(localDateTime); //2020-03-18T00:00
and to parse it to Instant depends on what class from the API you are using:
LocalDateTime.now().toInstant(ZoneOffset.of("+04:00"));
ZonedDateTime.now().toInstant();
The LocalDateTime doest not have a Zone, so you must a provide a ZoneOffset to parse it to a Instant.
In order to set the month you can use this lambda expression:
LocalDate.now().with(temporal
-> temporal.with(ChronoField.ALIGNED_WEEK_OF_YEAR, 1l)
);
Now get instant is easy:
LocalDate localDate = LocalDate.now().with(temporal
-> temporal.with(ChronoField.ALIGNED_WEEK_OF_YEAR, 1l)
);
Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
If you don't want cto change time, then you can use this instead of last line:
Instant instant = localDate.atTime(LocalTime.now()).atZone(ZoneId.systemDefault()).toInstant();
I want to get this current week on the day of 9/10/2018 as the 42 week of the year after setting the First Day of the week to Sunday. Still I get the output as 41 from the below snippet
Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.SUNDAY);
System.out.println( c.get(Calendar.WEEK_OF_YEAR) );
Am I missing something here?
Background: Our week start can be variable. Our configuration has the ability to let users decide the first day of the week.
Sorry if I’m too persistent. I still think you should leave the calculation to a custom WeekFields object. If I understood your comment correctly, you want:
final int daysPerWeek = DayOfWeek.values().length; // A wordy way of writing 7 :-)
WeekFields customWeekFields = WeekFields.of(firstDayOfWeek, daysPerWeek);
int customWeekNumber = date.get(customWeekFields.weekOfWeekBasedYear());
To test whether this agrees with what you are already doing I wrote the following method:
static void printWeekNumber(DayOfWeek firstDayOfWeek, LocalDate date) {
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("EEE uuuu-MM-dd", Locale.ENGLISH);
final int daysPerWeek = DayOfWeek.values().length;
// Week number according to your comment: “using the Temporal adjusters
// I am getting the date on the last day of the week.
// After that dividing the Day of the Year from the API by 7”
DayOfWeek lastDayOfWeek = firstDayOfWeek.minus(1);
int askersCommentWeekNumber = date
.with(TemporalAdjusters.nextOrSame(lastDayOfWeek))
.getDayOfYear()
/ daysPerWeek;
// My suggested way of calculating the same week number
WeekFields customWeekFields = WeekFields.of(firstDayOfWeek, daysPerWeek);
int customWeekNumber = date.get(customWeekFields.weekOfWeekBasedYear());
System.out.format(Locale.ENGLISH, "Week begins on %-8s Date is %s. Week %2d or %2d, agree? %s%n",
firstDayOfWeek, date.format(dateFormatter),
askersCommentWeekNumber, customWeekNumber,
askersCommentWeekNumber == customWeekNumber);
}
To make it easier to check the calculations by hand, I have picked a date in January in different years:
printWeekNumber(DayOfWeek.SUNDAY, LocalDate.of(2017, Month.JANUARY, 9));
printWeekNumber(DayOfWeek.MONDAY, LocalDate.of(2017, Month.JANUARY, 9));
printWeekNumber(DayOfWeek.THURSDAY, LocalDate.of(2017, Month.JANUARY, 9));
printWeekNumber(DayOfWeek.SUNDAY, LocalDate.of(2018, Month.JANUARY, 9));
printWeekNumber(DayOfWeek.MONDAY, LocalDate.of(2018, Month.JANUARY, 9));
printWeekNumber(DayOfWeek.SUNDAY, LocalDate.of(2019, Month.JANUARY, 9));
printWeekNumber(DayOfWeek.MONDAY, LocalDate.of(2019, Month.JANUARY, 9));
Output:
Week begins on SUNDAY Date is Mon 2017-01-09. Week 2 or 2, agree? true
Week begins on MONDAY Date is Mon 2017-01-09. Week 2 or 2, agree? true
Week begins on THURSDAY Date is Mon 2017-01-09. Week 1 or 1, agree? true
Week begins on SUNDAY Date is Tue 2018-01-09. Week 1 or 1, agree? true
Week begins on MONDAY Date is Tue 2018-01-09. Week 2 or 2, agree? true
Week begins on SUNDAY Date is Wed 2019-01-09. Week 1 or 1, agree? true
Week begins on MONDAY Date is Wed 2019-01-09. Week 1 or 1, agree? true
Please check whether the results are as you want them, though. For the example in your question, the result is neither 41 nor 42:
printWeekNumber(DayOfWeek.SUNDAY, LocalDate.of(2018, Month.OCTOBER, 9));
Week begins on SUNDAY Date is Tue 2018-10-09. Week 40 or 40, agree? true
Edit: If you want ISO 8601 week numbers, use date.get(WeekFields.ISO.dayOfWeek()). If you want your custom first day of week and 4 days in the first week as in ISO 8601, use:
WeekFields customWeekFields = WeekFields.of(firstDayOfWeek, 4);
So I need to make a basic calendar which displays the calendar for a specific year and month (the user should be able to select
any month and any year based on text input).
So far, I have managed to create a calender obj, use Scanner to get the desired month and year from the user but my question is that how do I find out what the first day of the month is? In the example above, it's a Saturday. My logic to building it is that if I know the first day, I can make a String[][] array and start displaying the day on the relevant date by looping through the month from the first day. I've used a scanner to get the required month and year. I then created a calendar object and set the Calendar variables; Calendar.YEAR and Calendar.MONTH, as per required by the user:
Calendar cal= Calendar.getInstance();
cal.set(Calendar.YEAR, year); //my year variable is set 2015
cal.set(Calendar.MONTH, chosenMonth); //my chosenMonth is set to 5 since January starts from 0.
I tried using the following code to test out my calender to see if it will execute the code if Monday was the first day of the month. On june 2015, it was.
if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY){
System.out.println("This will print is my calendar secessfully gathers that monday was the first day of the month on June 2015.")
}
It doesn't execute.
Try this:
cal.set(Calendar.DATE, 1);
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("EEEE");
System.out.println(sdf.format(cal.getTime()));
Output:
Saturday
Demo
Java SE 8 has a whole new API for date and time, java.time. See Tutorial.
You can use the class LocalDate to get the day of a given week. For example, using your day from above do,
LocalDate d = LocalDate.of(2011, 10, 1);
Then LocalDate::getDayOfWeek() method will return a DayOfWeek enum instance, such as SATURDAY.
DayOfWeek dayOfWeek = localDate.getDayOfWeek ();
That enum can render a localized string for your sentence output.
String output = dayOfWeek.getDisplayName ( TextStyle.FULL , Locale.CANADA_FRENCH); // Or Locale.US or Locale.ENGLISH, and so on.
samedi
java.util.Date gives incorrect day as output.
java.util.Date date = new Date(2014,03,01);
System.out.println("day is" +date.getDay());
output : day is 3
actually it should be 7
Update : Thanks a lot I was able to get the output Here is my code
java.util.Calendar c = java.util.Calendar.getInstance();
c.clear();
c.set(year,month,dd);
System.out.println("day of week "+c.get(java.util.Calendar.DAY_OF_WEEK));
I guess you wanted to create the date March 1st 2014. But with your constructor call you create the date April 1st 3914!
The API tells you why:
A year y is represented by the integer y - 1900.
A month is represented by an integer from 0 to 11; 0 is January, 1 is February, and so forth; thus 11 is December.
A date (day of month) is represented by an integer from 1 to 31 in the usual manner.
EDIT: Also this constructor is deprecated. Use Calendar instead.
As per the javadoc of getDay()
Returns the day of the week represented by this date. The returned
value (0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 =
Thursday, 5 = Friday, 6 = Saturday) represents the day of the week
that contains or begins with the instant in time represented by this
Date object, as interpreted in the local time zone.
so its not day of month. Also note that you are using deprecated Date constructor and methods. You can achieve the same using java.util.Calendar
Also month 03 is not March, its April
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, 2014);
c.set(Calendar.MONTH, 3);
c.set(Calendar.DAY_OF_MONTH, 1);
System.out.println(c.get(Calendar.DAY_OF_WEEK));
Here also the output is 3 because, the date represents April 1st 2014, which is Tuesday and as per Calendar.DAY_OF_WEEK doc
Field number for get and set indicating the day of the week. This
field takes values SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY,
FRIDAY, and SATURDAY.
And Calendar.TUESDAY is 3