How to find total weeks of a year in Java? - java

I am working on a project. There I should find the total weeks of a year. I tried with the following code, but I get the wrong answer: 2020 has 53 weeks, but this code gives 52 weeks.
Where have I gone wrong in this code?
package com.hib.mapping;
import java.time.LocalDate;
import java.time.temporal.WeekFields;
import java.util.Calendar;
import java.util.GregorianCalendar;
import org.joda.time.DateTime;
public class TestWeek {
public static void main(String args[]) {
System.out.println(getWeeks());
}
public static int getWeeks() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2020);
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DAY_OF_MONTH, 1);
GregorianCalendar gregorianCalendar = new GregorianCalendar();
int weekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (gregorianCalendar.isLeapYear(2020)) {
if (weekDay == Calendar.THURSDAY || weekDay == Calendar.WEDNESDAY)
return 53;
else
return 52;
} else {
if (weekDay == Calendar.THURSDAY)
return 53;
else
return 52;
}
}
}
Output:
52

tl;dr
For a standard ISO 8601 week, use the YearWeek class from ThreeTen-Extra library with a ternary statement.
YearWeek // Represents an entire week of a week-based-year.
.of( 2020 , 1 ) // Pass the number of the week-based-year (*not* calendar year), and a week number ranging from 1 to 52 or 1 to 53.
.is53WeekYear() // Every standard week-based-year has either 52 or 52 complete weeks.
? 53 // Ternary statement returns 53 if the predicate returns True, …
: 52 // … otherwise returns 52.
That is, YearWeek.of( 2020 , 1 ).is53WeekYear() ? 53 : 52
Define “week”
You need to define a week. In your code sample, the definition of week varies by the JVM’s current default Locale. So your results may vary at runtime.
Your code also uses terrible date-time classes that were supplanted years ago by the modern java.time classes. Stop using GregorianCalendar & Calendar; they were replaced for good reasons.
ISO 8601 week
The ISO 8601 standard defines a week as:
Weeks start on Monday, end on Sunday.
Week # 1 has the first Thursday of the calendar-year.
That definition means:
The first and last few days of a week-based-year may be the trailing/leading days of the previous/following calendar-year.
The week-based-year has either 52 or 53 complete weeks.
If your definition differs, see the Answer by Ole V.V..
YearWeek:is53WeekYear
If this matches your definition, then add the ThreeTen-Extra library to your project to extend the java.time functionality built into Java 8 and later. You then have access to the YearWeek class.
ZoneId z = ZoneId.of( "America/Montreal" ) ;
YearWeek yearWeekNow = YearWeek.now( z ) ;
boolean is53WeekYear = yearWeekNow.is53WeekYear() ;
int weeksLong = yearWeekNow.is53WeekYear() ? 53 : 52 ;
To ask about a particular week-based-year, just arbitrarily pick any week of the year. For example, for the week-based year 2020 we ask for week # 1.
int weeksLong = YearWeek.of( 2020 , 1 ).is53WeekYear() ? 53 : 52 ;
LocalDate weekStart = YearWeek.of( 2020 , 1 ).atDay( DayOfWeek.MONDAY ) ;
weeksLong = 53
weekStart = 2019-12-30
Notice how the first day of the week-based-year of 2020 is from the calendar-year 2019.

Using the Wikipedia definition here. A year has 53 weeks if 1st Jan is a Thursday, or 31st Dec is a Thursday, otherwise it has 52 weeks. This definition is equivalent to the one you used. I think this is a way easier condition to check for, as you don't need to check for leap years.
Using the Java 8 java.time APIs:
int year = 2020;
boolean is53weekYear = LocalDate.of(year, 1, 1).getDayOfWeek() == DayOfWeek.THURSDAY ||
LocalDate.of(year, 12, 31).getDayOfWeek() == DayOfWeek.THURSDAY;
int weekCount = is53weekYear ? 53 : 52;

The flexible solution
This should work for any week numbering scheme that can be represented in a WeekFields object.
public static int noOfWeeks(WeekFields wf, int year) {
LocalDate lastDayOfYear = YearMonth.of(year, Month.DECEMBER).atEndOfMonth();
if (lastDayOfYear.get(wf.weekBasedYear()) > year) { // belongs to following week year
return lastDayOfYear.minusWeeks(1).get(wf.weekOfWeekBasedYear());
}
else {
return lastDayOfYear.get(wf.weekOfWeekBasedYear());
}
}
The idea is to find the week number of the last week of the week based year. I try first with 31 December, but that may be in the first week of the following year. If so, I go one week back.
I have tested pretty thoroughly with WeekFields.ISO, not so much with other WeekFields objects, but as I said, I believe it works.
If you know for a fact that you will always need ISO 8601 weeks, I think you should go with one of the good answers by Sweeper and by Basil Bourque. I posted this in case you needed a more flexible solution that would work with other week numbering schemes too.
Use java.time
The code in your question is funny in that it imports classes both from Joda-Time and from java.time, yet uses the old Calendar and GregorianCalendar from Java 1.1. These classes were poorly designed and are now long outdated, you should not use them. Joda-Time is in maintenance mode, java.time has taken over after it. Which is what I use and recommend that you use.

I think this should work just fine as well:
int year = 2020;
long numOfWeeks = LocalDate.of(year, 1, 1).datesUntil(LocalDate.of(year, 12, 31), Period.ofDays(7)).count();
System.out.println("Weeks: " + numOfWeeks);

Below code works for me.
public static int getTotalWeeksInYear(int year){
int totalWeeks=0;
Calendar calendar=Calendar.getInstance();
for(int month=0;month<12;mmonth++){
int day=1;
do{
calendar.set(year, month, day);
if(calendar.get(Calendar.DAY_OF_WEEK)==5)
totalWeeks++;
day++;
}while (day <=calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
}
return totalWeeks;
}
Using Java.time
public static long getTotalWeekByLocalDate(LocalDate ldate) {
long weeksInYear = IsoFields.WEEK_OF_WEEK_BASED_YEAR.rangeRefinedBy(ldate).getMaximum();
return weeksInYear;
}

After trying a lot in java 8. I could not find a solution. then I prepared Joda date and time dependency. It gave me a good answer as I expected
code:
for (int i = 2020; i < 2100; i++) {
int weeks = new DateTime().withYear(i).weekOfWeekyear().getMaximumValue();
System.out.println(i + " years : " + weeks);
}
Maven Dependency:
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.5</version>
</dependency>

Related

How to get Date from Week Number, Year and dayOfWeek in java?

I have the week number, its curresponding year and dayOfWeek number(i.e. 1 for Monday, 2 for Tuesday and so on). Is there a way to find the date with this information in java?
Following is a method I found online.
int week = 51;
LocalDate wkstart = LocalDate.now().with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, week);
LocalDate mon = wks.plusDays(1);
LocalDate tue = wks.plusDays(2);
LocalDate wed = wks.plusDays(3);
LocalDate thu = wks.plusDays(4);
LocalDate fri = wks.plusDays(5);
LocalDate sat = wks.plusDays(6);
LocalDate wkend = wks.plusDays(7);
But then realised that wkstart is storing the current date instead of the start of the week.
Is there a better way of doing this?
Instead of adding a number of days to wkstart, use with again to set the day of week. For example:
LocalDate date = LocalDate.now()
.with(WeekFields.ISO.weekBasedYear(), 2018) // year
.with(WeekFields.ISO.weekOfWeekBasedYear(), 51) // week of year
.with(WeekFields.ISO.dayOfWeek(), DayOfWeek.MONDAY.getValue()); // day of week
tl;dr
LocalDate localDate =
YearWeek // Represent an entire week of a week-based year per the ISO 8601 standard definition of a week.
.of( // Instantiate a `YearWeek` object.
2019 , // Specify the week-based year number, NOT the calendar year.
51 // Specify the week number, 1-52 or 1-53.
)
.atDay(
DayOfWeek.of( 1 ) // The value 1 yields a `DayOfWeek.MONDAY` object.
)
;
org.threeten.extra.YearWeek
The Answer by Sweeper looks correct. But there is a more specialized class for this.
If doing much work with weeks of week-based years per the ISO 8601 definition of week, use the YearWeek class found in the ThreeTen-Extra library. This library adds extra functionality to the java.time classes built into Java 8 and later.
Determine the week.
YearWeek yearWeek = YearWeek.of( 2019 , 51 ) ;
Get a LocalDate for the day-of-week within that week.
LocalDate localDate = yearWeek.atDay( DayOfWeek.MONDAY ) ;
For the day-of-week, you should be using DayOfWeek enum objects in your code rather than mere integer numbers. To get a DayOfWeek from an original number 1-7 for Monday-Sunday, call DayOfWeek.of( x ).
DayOfWeek dow = DayOfWeek.of( 1 ) ; // 1 = Monday, 7 = Sunday.
Putting that all together we get this one-liner.
LocalDate localDate = YearWeek.of( 2019 , 51 ).atDay( DayOfWeek.of( 1 ) ) ;
To be clear… The ISO 8601 definition of a week is:
Week # 1 contains the first Thursday of the year.
Weeks start on a Monday, ending on a Sunday.
A year has either 52 or 53 complete 7-day weeks.
The first/last weeks of the week-based year may contain the trailing/leading days of the previous/next calendar years. Thus, the calendar year of those days differ from the week-based year.
It also depends on the Locale.
Note that the first day of the week is Locale-dependent e.g. it is Monday in the UK while Sunday in the US. As per the ISO 8601 standards, it is Monday. For comparison, check the US calendar and the UK calendar. Accordingly, the date will vary as shown in the example below:
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.WeekFields;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// Test
int weekNumber = 34;
int year = 2021;
System.out.println(getLocalDate(weekNumber, DayOfWeek.TUESDAY, year, Locale.UK));
System.out.println(getLocalDate(weekNumber, DayOfWeek.TUESDAY, year, Locale.US));
System.out.println(getLocalDate(weekNumber, DayOfWeek.SUNDAY, year, Locale.UK));
System.out.println(getLocalDate(weekNumber, DayOfWeek.SUNDAY, year, Locale.US));
}
static LocalDate getLocalDate(int weekNumber, DayOfWeek dow, int year, Locale locale) {
return LocalDate.of(year, 2, 1)
.with(dow)
.with(WeekFields.of(locale).weekOfWeekBasedYear(), weekNumber);
}
}
Output:
2021-08-24
2021-08-17
2021-08-29
2021-08-15
ONLINE DEMO
Learn more about the modern Date-Time API from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

getting Monday date from given week and year using java.time package

I want to get Monday date from given week and year using Java 8 package java.time.
But at some point I am facing issue as it's not returning proper date.
private LocalDate getDateFromWeekAndYear(final String week,final String year){
LocalDate date = LocalDate.now();
date = date.with(WeekFields.ISO.dayOfWeek(), 1);
date = date.with(WeekFields.ISO.weekOfWeekBasedYear(), Long.parseLong(week));
date = date.with(WeekFields.ISO.weekBasedYear(), Long.parseLong(year));
return date;
}
For example:
If I pass week=1 and year=2013 then date is : 2012-12-31.
But if I pass week=53 and year=2015 then date is : 2014-12-29. I expected 2014-12-28.
Is there any logical mistake I am making or some other issue ?
This is astonishingly more difficult than the partially invalid expectations of OP and most answers show.
First to say: It is very important to define the correct order of week-based-manipulations. The OP has first applied day-manipulation, then year-based manipulation. The correct approach is in reverse! I will show the right helper method implementation:
public static void main(String... args) {
System.out.println(
getDateFromWeekAndYear("53", "2015")); // 2015-12-28, NOT 2014-12-28
System.out.println(
getDateFromWeekAndYear("53", "2015").get(WeekFields.ISO.weekOfWeekBasedYear())); // 53
System.out.println(
getDateFromWeekAndYear("53", "2014")); // 2014-12-29
System.out.println(
getDateFromWeekAndYear("53", "2014").get(WeekFields.ISO.weekOfWeekBasedYear())); // 1
}
private static LocalDate getDateFromWeekAndYear(final String week,final String year) {
int y = Integer.parseInt(year);
LocalDate date = LocalDate.of(y, 7, 1); // safer than choosing current date
// date = date.with(WeekFields.ISO.weekBasedYear(), y); // no longer necessary
date = date.with(WeekFields.ISO.weekOfWeekBasedYear(), Long.parseLong(week));
date = date.with(WeekFields.ISO.dayOfWeek(), 1);
return date;
}
If you don't respect this specific order then you will indeed get sometimes a 2014-date for the input 2015-W53 (depending on the current date).
Second problem: I have also avoided to start with current date in order to be not near start or end of calendar year (calendar year != week-based-year) and instead chosen midth of year as starting point.
The third problem is lenient handling of week 53 in (week-based)-year 2014. It does not exist because 2014 had only 52 weeks!!! A strict algorithm should recognize and reject such an input. Therefore I advise against using YearWeek.of(2014, 53) (in the external library Threeten-Extra) resulting in the first week of 2015, see also its javadoc. Better than such lenient handling would have been
YearWeek yw = YearWeek.of(2014, 52);
if (yw.is53WeekYear()) {
yw = YearWeek.of(2014, 53);
}
or using this code from my own time library Time4J (whose class CalendarWeek has extra i18n-features and extra week arithmetic in comparison with YearWeek):
CalendarWeek.of(2014, 53); // throws an exception
System.out.println(CalendarWeek.of(2014, 1).withLastWeekOfYear()); // 2014-W52
Only using java.time-package:
Using such external libraries would at least have helped to solve the first problem in a transparent way. If you are not willing to add an extra dependency then you can do this to handle week 53 if invalid:
If the expression WeekFields.ISO.weekOfWeekBasedYear() applied on the result of your helper method yields the value 1 then you know that week 53 was invalid. And then you can decide if you want to accept lenient handling or to throw an exception. But silent adjusting such an invalid input is IMHO bad design.
First, you need to calculate the first Monday of the first week of the year,
then simply plus multi of 7 to the date.
public static LocalDate firstMonday(int week, int year) {
LocalDate firstMonOfFirstWeek = LocalDate.now()
.with(IsoFields.WEEK_BASED_YEAR, year) // year
.with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 1) // First week of the year
.with(ChronoField.DAY_OF_WEEK, 1); // Monday
// Plus multi of 7
return firstMonOfFirstWeek.plusDays( (week - 1) * 7);
}
public static void main(String[] args) {
System.out.println(firstMonday(1, 2013)); // 2012-12-31
System.out.println(firstMonday(53 ,2015 )); // 2015-12-28
}
tl;dr
YearWeek.of ( 2013 , 1 ).atDay ( DayOfWeek.MONDAY )
Incorrect expectations
Your expectations are not correct. The Monday of 2015-W53 is 2015-12-28, not 2014-12-28, year 2015 rather than 2014. There is no reason to expect 2014. Please edit your Question to explain your thoughts, if you need more explanation.
You may be confused about a calendar-year versus a week-based year. In the ISO 8601 definition of a week based year, week number one contains the first Thursday of the calendar-year. This means we have some overlap between the years. The last few days of a calendar-year may reside in the following week-based-year. And vice-versa, the first few days of a calendar-year may reside in the previous week-based-year.
As an example, you can see in the screenshot below, the last day of calendar 2012 (December 31) falls in week one of the following week-based-year of 2013 at week number 1. And in the other screen shot, we have the opposite, where the first three days of calendar 2016 (January 1, 2, & 3) land in the week-based-year of 2015 at week number 53.
The Monday of 2013-W01 is 2012-12-31.
The Monday of 2015-W53 is 2015-12-28.
YearWeek
I suggest adding the ThreeTen-Extra library to your project to make use of of the YearWeek class. Rather than pass around mere integer numbers for year and week, pass around objects of this class. Doing so makes your code more self-documenting, provides type-safety, and ensures valid values.
// Pass ( week-based-year-number, week-number ). *Not* calendar year! See the ISO 8601 standard.
YearWeek yw = YearWeek.of( 2013 , 1 );
You can pull any day from that week.
LocalDate ld = yw.atDay( DayOfWeek.MONDAY );
Let's try this kind of code.
YearWeek yw1 = YearWeek.of ( 2013 , 1 );
LocalDate ld1 = yw1.atDay ( DayOfWeek.MONDAY );
YearWeek yw2 = YearWeek.of ( 2015 , 53 );
LocalDate ld2 = yw2.atDay ( DayOfWeek.MONDAY );
System.out.println ( "yw1: " + yw1 + " Monday: " + ld1 );
System.out.println ( "yw2: " + yw2 + " Monday: " + ld2 );
yw1: 2013-W01 Monday: 2012-12-31
yw2: 2015-W53 Monday: 2015-12-28
Tip: To see those ISO 8601 standard week numbers on a Mac in Calendar.app, set System Preferences > Language & Region > Calendar > ISO 8601. Then in Calendar.app, set Preferences > Advanced > Show week numbers.
I wanted Monday of a week about 6 month ago - to be specific 26 weeks ago.. below code gave me the required date:
LocalDate.now().minusWeeks(26).with(WeekFields.ISO.dayOfWeek(), DayOfWeek.MONDAY.getValue())

Use Calendar to get the number of days in February

The question is to get day numbers of February of any year. My code is like this:
public static void main(String[] args) {
System.out.println("2014-02 has " + getDaysOfFeb(2014) + "days");
System.out.println("2016-02 has " + getDaysOfFeb(2016) + "days");
}
public static int getDaysOfFeb(int year) {
Calendar c = Calendar.getInstance();
// set year-01-31
c.set(year, 0, 31);
long lastDayofJan = c.getTimeInMillis();
// set year-03-01
c.set(year, 2, 1);
long firstDayOfMar = c.getTimeInMillis();
int date = (int) ((firstDayOfMar - lastDayofJan) / 1000 / 60 / 60 / 24);
}
I got Jan 31st and Mar 1st, I use the difference of time to calculate the day numbers. But the result is:
2014-02 has 29days
2016-02 has 30days
I don't understand why.
When I do like this:
public static int getDaysOfFeb(int year) {
Calendar c = Calendar.getInstance();
// set year-01-31
c.set(year, 2, 1);
c.add(Calendar.DATE, -1); // last day of Feb
int date = c.get(Calendar.DATE);
return date;
}
The result is right, as follow:
2014-02 has 28days
2016-02 has 29days
Does anyone know what the difference is here?
It is exactly how it should be. The difference between March, 1 and January, 31 is one day more than the number of days in February. In general, the difference between the same days in two consecutive months is the number of days in the earliest of these two months. So, the difference between March, 1 and February, 1 is the number of days in February, as well as the difference between March, 10 and February, 10 (and any other day). The difference between April, 1 and March, 1 is always 31 (the number of days in March) and so on.
Not that this is an exact answer. But I wrote a quick Java program a while ago that will generate a calendar for pretty much whatever month/year you want. The example falls directly in line with what you're trying to do.
Here is the code running on TutorialsPoint:
Calendar in Java
Code:
import java.util.Calendar;
public class calDays {
/**
* #param args
*/
static Calendar today = Calendar.getInstance();
static int curDay = today.get(Calendar.DAY_OF_MONTH);
static int curMonth = today.get(Calendar.MONTH);
static int curYear = today.get(Calendar.YEAR);
public static void main(String[] args) {
drawCal(curMonth,curYear);
drawCal(5,1969);
drawCal(4,2001);
}
public static void drawCal(Integer monthIs, Integer yearIs){
System.out.println(""+getMonth(monthIs)+" "+yearIs);
System.out.println("S M T W T F S ");
int calDayBox =0;
Calendar monthtd = Calendar.getInstance();
monthtd.set(Calendar.DAY_OF_MONTH,1);
monthtd.set(Calendar.MONTH, monthIs);
monthtd.set(Calendar.YEAR, yearIs);
int daysInMonth = monthtd.getActualMaximum(Calendar.DAY_OF_MONTH);
int allboxes=0;
//Draw leading days
int CalDaxBoxVal=1;
for (int xx=0;xx<monthtd.get(Calendar.DAY_OF_WEEK)-1;xx++){
System.out.print("_ ");
calDayBox++;
allboxes++;
}
for (int xx=calDayBox;xx<daysInMonth+calDayBox;xx++){
System.out.print(""+CalDaxBoxVal+ " ");
if (CalDaxBoxVal<10) System.out.print(" ");
CalDaxBoxVal++;
allboxes++;
if (allboxes%7==0) System.out.println();
}
System.out.println();
System.out.println();
}
public static String getMonth(Integer whichMonth){
String monthsAre[] = new String[12];
monthsAre[0]="January";
monthsAre[1]="February";
monthsAre[2]="March";
monthsAre[3]="April";
monthsAre[4]="May";
monthsAre[5]="June";
monthsAre[6]="July";
monthsAre[7]="August";
monthsAre[8]="September";
monthsAre[9]="October";
monthsAre[10]="November";
monthsAre[11]="December";
return monthsAre[whichMonth];
}
}
Change your first method to something like this.
Calendar c = Calendar.getInstance();
c.set(year, 1, 1);
long firstDayOfFeb= c.getTimeInMillis();
c.set(year, 2, 1);
long firstDayOfMar = c.getTimeInMillis();
int date = (int) ((firstDayOfMar - firstDayOfFeb) / 1000 / 60 / 60 / 24);
return date;
In your calculation you are actually calculating from last day of january to 1 st dat of March, where as you should start from 1 st day of Feb to 1st day of march for correct answer. The problem is with calculation.
This can be easily answered with simple mathematics.
If you are subtracting 0 from 30, (30 - 0 = ?) you get 30.
If you are subtracting 1 from 30, (30 - 1 = ?) you get 29.
Pretend 0 is February 0th (which is equivalent to January 31st)
Pretend 30 is February 30th (which is equivalent to March 1st on a leap year)
So therefore, March 1st - January 31st must be 30 days.
In order to get the desired number of days, you must subtract 1 from 30, and not 0 from 30.
Pretend 1 is February 1st
Pretend 30 is February 30th (which is equivalent to March 1st on a leap year)
Therefore, you would need to subtract March 1st from February 1st in order to get the correct number of days in February.
tl;dr
YearMonth.parse( "2014-02" ).lengthOfMonth()
Avoid old date-time classes
The Question and other Answers use troublesome old legacy date-time classes bundled with the earliest versions of Java. Now supplanted by the java.time classes.
YearMonth
Among the java.time classes is YearMonth to represent, well, a year and month.
Note that in java.time the months have sane numbering, 1-12 for January-December (unlike the crazy 0-11 in old date-time classes).
YearMonth ym = YearMonth.parse( "2014-02" );
Or you can make use of the handy Month enum.
YearMonth ym = YearMonth.of( 2014 , Month.FEBRUARY );
Interrogate for the number of days in that month by calling lengthOfMonth.
int lengthOfMonth = ym.lengthOfMonth() ;
You can ask if the year of that year-month is a Leap Year by calling isLeapYear.
boolean isLeapYear = ym.isLeapYear();
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

How to extract the data basis on it is Monday or Thursday in YYYYMMDD format?

I am trying to get the date of Monday or Thurday in this format YYYYMMDD
For Monday it should give me this - 20130224 (as an example)
For Thursday it should give me this - 20130227 (as an example)
Now, if I am running my program after Thursday or on Thursday, it should print date for Thursday in this format YYYYMMDD which can be 20130227 (coming thursday in this week).
And If I am running my program after Monday or on Monday, then it should print date for Monday in the same format YYYYMMMDD which can be 20130224 (yesterday Monday date as an example)
How would I do this in Java?
Below is what I have tried -
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("EEE");
String text = formatter.format(cal.getTime());
System.out.println(text);
// but how do I check if it is Tuesday but less than Thursday
if(text.equalsIgnoreCase("Tue")) {
// get previous Monday date in YYYYMMDD
}
// and how do I check if it is thursday or greater than Thursday?
else if(text.equalsIgnoreCase("Thur")) {
// get previous Thursday date in YYYYMMDD
}
Update:-
In a particular week, if I am running my program on Thursday or after Thursday then it should return me date for Thursday in the same week in YYYYMMDD format, but if I am running my program on Monday or after Monday, then it should return me date for Monday in the same week in YYYYMMDD format.
For example, In this week, if I am running my program on Thursday or after Thursday, then it should return date for Thursday. But if I am running my program on Monday or Tuesday or Wednesday in this same week, then it should return me date for Monday.
Code:-
Below is my code -
public static void main(String[] args) {
try {
Calendar cal = Calendar.getInstance();
SimpleDateFormat toDateFormat = new SimpleDateFormat("yyyyMMdd");
int dow = cal.get(Calendar.DAY_OF_WEEK);
switch (dow) {
case Calendar.THURSDAY:
case Calendar.FRIDAY:
case Calendar.SATURDAY:
case Calendar.SUNDAY:
while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.THURSDAY) {
cal.add(Calendar.DATE, -1);
}
break;
case Calendar.MONDAY:
case Calendar.TUESDAY:
case Calendar.WEDNESDAY:
while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
cal.add(Calendar.DATE, -1);
}
break;
}
System.out.println(date);
System.out.println(cal.getTime());
System.out.println(toDateFormat.format(cal.getTime()));
} catch (ParseException exp) {
exp.printStackTrace();
}
}
Start by parsing the text value to a Date value...
String dateText = "20130224";
SimpleDateFormat toDateFormat = new SimpleDateFormat("yyyyMMdd");
Date date = toDateFormat.parse(dateText);
This gives you the starting point. Next you need to use a Calendar which will allow you move backwards and forwards, automatically rolling the date internally for you (so if you roll over week, month or year boundaries)
For example...
try {
String dateText = "20130227";
SimpleDateFormat toDateFormat = new SimpleDateFormat("yyyyMMdd");
Date date = toDateFormat.parse(dateText);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dow = cal.get(Calendar.DAY_OF_WEEK);
switch (dow) {
case Calendar.THURSDAY:
case Calendar.FRIDAY:
case Calendar.SATURDAY:
case Calendar.SUNDAY:
while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.THURSDAY) {
cal.add(Calendar.DATE, -1);
}
break;
case Calendar.MONDAY:
case Calendar.TUESDAY:
case Calendar.WEDNESDAY:
while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
cal.add(Calendar.DATE, -1);
}
break;
}
System.out.println(date);
System.out.println(cal.getTime());
} catch (ParseException exp) {
exp.printStackTrace();
}
So, based on this example, it would output...
Wed Feb 27 00:00:00 EST 2013
Mon Feb 25 00:00:00 EST 2013
For 20130224 (which is a Sunday) it will give
Sun Feb 24 00:00:00 EST 2013
Thu Feb 21 00:00:00 EST 2013
I should also add, there's probably a much easier way to do this with JodaTime, but this is what I was able to wipe up quickly. Yes, I know the case statement is little winded, but SUNDAY is equal to 0, which is a little annoying ;)
What? Moving to a new question with the same contents?
String[] weeks = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
DateFormat df = new SimpleDateFormat("yyyyMMdd");
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Now use today date.
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); // Sun=1, Mon=2, ... Sat=7
System.out.println("Today " + df.format(c.getTime()) + " is " + weeks[dayOfWeek-1]);
c.add(Calendar.DATE, 7); // Adding 7 days
System.out.println("Next " + weeks[dayOfWeek-1] + " is " + df.format(c.getTime()));
// Should display:
// Today 20140225 is Tuesday
// Next Tuesday is 20140304
I would use the calendar class day to get the day of the week. Calendar.DAY_OF_WEEK function returns 1 - 7 for Sunday - Saturday. This way you can do numeric comparison and not mess around with comparing the strings for the weekdays (which would be a mess if your app needs to support multiple languages).
See:
http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
If I understood what you're after, then this should work
private static final java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat("yyyyMMdd");
public static String calculateCorrectDate(
java.util.Date d) {
java.util.Calendar cal = java.util.Calendar
.getInstance();
cal.setTime(d);
int dayOfWeek = cal
.get(java.util.Calendar.DAY_OF_WEEK);
if (dayOfWeek >= java.util.Calendar.MONDAY
&& dayOfWeek < java.util.Calendar.THURSDAY) {
cal.set(java.util.Calendar.DAY_OF_WEEK,
java.util.Calendar.MONDAY);
} else {
cal.set(java.util.Calendar.DAY_OF_WEEK,
java.util.Calendar.THURSDAY);
}
return sdf.format(cal.getTime());
}
public static void main(String[] args) {
java.util.List<java.util.Date> dates = new java.util.ArrayList<java.util.Date>();
java.util.Calendar cal = java.util.Calendar
.getInstance();
String today = sdf.format(cal.getTime());
cal.add(java.util.Calendar.DAY_OF_WEEK, -10);
for (int i = 0; i < 20; i++) {
dates.add(cal.getTime());
cal.add(java.util.Calendar.DAY_OF_WEEK, 1);
}
for (java.util.Date d : dates) {
if (sdf.format(d).equals(today)) {
System.out.println("TODAY!");
}
System.out.println(calculateCorrectDate(d));
}
}
Which gives the output
20140213
20140220
20140217
20140217
20140217
20140220
20140220
20140220
20140227
20140224
TODAY!
20140224
20140224
20140227
20140227
20140227
20140306
20140303
20140303
20140303
20140306
or with a few import(s),
// Import Static
// This simplifies accessing the Calendar fields. Use sparingly.
import static java.util.Calendar.DAY_OF_WEEK;
import static java.util.Calendar.MONDAY;
import static java.util.Calendar.THURSDAY;
// The other imports.
import java.util.Calendar;
import java.util.Date;
Then you can use,
public static String calculateCorrectDate(Date d) {
Calendar cal = Calendar.getInstance();
cal.setTime(d);
// By using import static this remains concise and correct.
int dayOfWeek = cal.get(DAY_OF_WEEK);
if (dayOfWeek >= MONDAY && dayOfWeek < THURSDAY) {
cal.set(DAY_OF_WEEK, MONDAY);
} else {
cal.set(DAY_OF_WEEK, THURSDAY);
}
return sdf.format(cal.getTime());
}
Joda-Time
UPDATE: The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes. This Answer is left intact as history. See my modern Answer instead.
Yes, Joda-Time is the solution.
Or the new java.time package in Java 8. Inspired by Joda-Time but re-architected. Defined by JSR 310.
Next Monday/Thursday
Search StackOverflow for getting first or last day of week. That will show you how to get next monday or thursday. I won't cover that part of your question here.
Testing Day Of Week
Testing for the day of week by the English word is prone to break if you ever happen to run where English is not the default locale. Instead, get day of week by number. Joda-Time uses ISO 8601 as its defaults, so Monday = 1, Sunday = 7. Constants are provided, so you needn't memorize the numbers.
Date Without Time
If you truly don't care about time-of-day, only date, then we can use LocalDate rather than DateTime.
Example Code
Some code to get you started, using Joda-Time 2.3.
String input = "20130224";
DateTimeFormatter formatterInput = DateTimeFormat.forPattern( "yyyyMMdd" );
LocalDate localDate = formatterInput.parseLocalDate( input );
int dayOfWeek = localDate.getDayOfWeek();
boolean isMonday = ( localDate.getDayOfWeek() == DateTimeConstants.MONDAY );
boolean isThursday = ( localDate.getDayOfWeek() == DateTimeConstants.THURSDAY );
Dump to console…
System.out.println( "localDate: " + localDate );
System.out.println( "dayOfWeek: " + dayOfWeek );
System.out.println( "isMonday: " + isMonday );
System.out.println( "isThursday: " + isThursday );
When run…
localDate: 2013-02-24
dayOfWeek: 7
isMonday: false
isThursday: false
tl;dr
If:
EnumSet.range( DayOfWeek.MONDAY , DayOfWeek.WEDNESDAY ) // Monday, Tuesday, & Wednesday.
contains( LocalDate.now().getDayOfWeek() ) // If today’s date is a day-of-week that happens to be a member of that `Set`.
…then, apply:
TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ) // Adjust into an earlier `LocalDate` that is a Monday, unless today already is Monday.
…else apply:
TemporalAdjusters.previousOrSame( DayOfWeek.THURSDAY ) // Otherwise move to a Thursday.
java.time
The modern approach uses the java.time classes. So much easier to solve this Question.
LocalDate
The LocalDate class represents a date-only value without time-of-day and without time zone.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.
Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z ) ;
If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the JVM’s current default is applied implicitly. Better to be explicit.
ZoneId z = ZoneId.systemDefault() ; // Get JVM’s current default time zone.
Or specify a date. You may set the month by a number, with sane numbering 1-12 for January-December.
LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ; // Years use sane direct numbering (1986 means year 1986). Months use sane numbering, 1-12 for January-December.
Or, better, use the Month enum objects pre-defined, one for each month of the year. Tip: Use these Month objects throughout your codebase rather than a mere integer number to make your code more self-documenting, ensure valid values, and provide type-safety.
LocalDate ld = LocalDate.of( 1986 , Month.FEBRUARY , 23 ) ;
DayOfWeek enum
The DayOfWeek enum defines a set of seven objects, one for each day of the week.
An EnumSet is a highly-optimized implementation of Set for collecting enum objects. So we can make a pair of EnumSet objects to hold a collection of DayOfWeek objects to define your two conditions: (Monday & Tuesday) versus (Thursday…Sunday).
Despite mysteriously failing to implement SortedSet, an EnumSet is indeed sorted in the natural order (declared order) of the enum. For DayOfWeek that would be Monday-Sunday, numbered 1-7 though you may never need those numbers.
Set < DayOfWeek > mondayDays = EnumSet.range( DayOfWeek.MONDAY , DayOfWeek.WEDNESDAY ); // Monday, Tuesday, & Wednesday.
Set < DayOfWeek > thursdayDays = EnumSet.range( DayOfWeek.THURSDAY , DayOfWeek.SUNDAY ); // Thursday, Friday, Saturday, Sunday.
Get the day-of-week for our source date. Prepare to match that against our enum sets.
LocalDate ld = LocalDate.now( ZoneId.of( "Africa/Tunis" ) ) ; // Get today’s current date in a particular time zone.
DayOfWeek dow = ld.getDayOfWeek();
LocalDate target = LocalDate.EPOCH; // Or null. The `LocalDate.EPOCH` is first moment of 1970 in UTC, 1970-01-01T00:00:00Z.
See which Set has the day-of-week of our date. From there, adjust into a previous or same date for the desired day-of-week (Monday or Thursday).
if ( mondayDays.contains( dow ) )
{
target = ld.with( TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ) );
} else if ( thursdayDays.contains( dow ) )
{
target = ld.with( TemporalAdjusters.previousOrSame( DayOfWeek.THURSDAY ) );
} else
{
System.out.println( "ERROR - Unexpectedly reached IF-ELSE. " );
}
Generate string in your desired format. Your chosen format happens to be the “basic” version of standard ISO 8601 format where the use of delimiters is minimized.
String output = target.format( DateTimeFormatter.BASIC_ISO_DATE ) ; // YYYY-MM-DD
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, and later
Built-in.
Part of the standard Java API with a bundled implementation.
Java 9 adds some minor features and fixes.
Java SE 6 and Java SE 7
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
Later versions of Android bundle implementations of the java.time classes.
For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Java: How do I get the date of x day in a month ( e.g. Third Monday in February 2012)

I am somewhat struggling with this.
I want to setup my Calendar to let's say: Third Monday in February 2012.
And I didn't find any way of doing this using Java.
For example, if I wanted to set my calendar for Christmas 2011, I can do this easily, this way:
Calendar when = Calendar.getInstance();
when.set (Calendar.MONTH, Calendar.DECEMBER);
when.set (Calendar.DAY_OF_MONTH, 25)
when.set (Calendar.YEAR, 2011);
But I am lost as to how to set it up for let's say Memorial Day 2012, which is the last Monday of May. This is my code, but it's obviously wrong, because I simply can't assume that the last Monday of May will be in the 4th week of May that year:
Calendar when = Calendar.getInstance ();
when.set (Calendar.DAY_OF_WEEK,Calendar.MONDAY);
when.set (Calendar.MONTH, Calendar.MAY);
when.set (Calendar.WEEK_OF_MONTH, 4)
when.set (Calendar.YEAR, 2012);
Any suggestions as to how I can programatically find out, in which week of the month of May 2012 (in example above) is the last Monday? Assuming I can get that information, I should be able to get my code above to work.
I need something which would basically work for any other examples. Something which could give an exact day for the same scenarios. Examples:
Which date is:
3rd Thursday of May 2015
1st Monday of June 2050
4th Tuesday of December 2012
2nd Wednesday of July 2000
I really need this for my project and I am sure it's simple, but I am breaking my head on this without any real results to show for :) And also couldn't find anything on the net.
Added:
Ok, this is where I've got for the last Monday in a month:
when.set (GregorianCalendar.MONTH, GregorianCalendar.MAY);
when.set (GregorianCalendar.DAY_OF_WEEK, Calendar.MONDAY);
when.set (GregorianCalendar.DAY_OF_WEEK_IN_MONTH, -1);
when.set (Calendar.YEAR, 2012);
But I am not sure how would I go about doing for example seconds Monday in the same month, like this?
when.set (GregorianCalendar.DAY_OF_WEEK_IN_MONTH, 2);
Any suggestions?
To do date arithmetic in Java (and in general, to do anything with datetimes, except for the most trivial things) Joda-Time is the answer:
public static LocalDate getNDayOfMonth(int dayweek,int nthweek,int month,int year) {
LocalDate d = new LocalDate(year, month, 1).withDayOfWeek(dayweek);
if(d.getMonthOfYear() != month) d = d.plusWeeks(1);
return d.plusWeeks(nthweek-1);
}
public static LocalDate getLastWeekdayOfMonth(int dayweek,int month,int year) {
LocalDate d = new LocalDate(year, month, 1).plusMonths(1).withDayOfWeek(dayweek);
if(d.getMonthOfYear() != month) d = d.minusWeeks(1);
return d;
}
public static void main(String[] args) {
// second wednesday of oct-2011
LocalDate d = getNDayOfMonth( DateTimeConstants.WEDNESDAY, 2, 10, 2011);
System.out.println(d);
// last wednesday of oct-2011
LocalDate dlast = getLastWeekdayOfMonth( DateTimeConstants.WEDNESDAY, 10, 2011);
System.out.println(dlast);
}
Edit: Since Java 8 (2014) the new Date API (package java.time), which is inspired by/similar to Jodatime, should be preferred.
The following code was successfully tested for all holidays in 2013 and 2014. I realize that this doesn't really answer the original question, but I think it might be useful for people who come across this post in hopes of figuring out how to work with holidays using Calendar.
public static boolean isMajorHoliday(java.util.Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
// check if New Year's Day
if (cal.get(Calendar.MONTH) == Calendar.JANUARY
&& cal.get(Calendar.DAY_OF_MONTH) == 1) {
return true;
}
// check if Christmas
if (cal.get(Calendar.MONTH) == Calendar.DECEMBER
&& cal.get(Calendar.DAY_OF_MONTH) == 25) {
return true;
}
// check if 4th of July
if (cal.get(Calendar.MONTH) == Calendar.JULY
&& cal.get(Calendar.DAY_OF_MONTH) == 4) {
return true;
}
// check Thanksgiving (4th Thursday of November)
if (cal.get(Calendar.MONTH) == Calendar.NOVEMBER
&& cal.get(Calendar.DAY_OF_WEEK_IN_MONTH) == 4
&& cal.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {
return true;
}
// check Memorial Day (last Monday of May)
if (cal.get(Calendar.MONTH) == Calendar.MAY
&& cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY
&& cal.get(Calendar.DAY_OF_MONTH) > (31 - 7) ) {
return true;
}
// check Labor Day (1st Monday of September)
if (cal.get(Calendar.MONTH) == Calendar.SEPTEMBER
&& cal.get(Calendar.DAY_OF_WEEK_IN_MONTH) == 1
&& cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
return true;
}
// check President's Day (3rd Monday of February)
if (cal.get(Calendar.MONTH) == Calendar.FEBRUARY
&& cal.get(Calendar.DAY_OF_WEEK_IN_MONTH) == 3
&& cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
return true;
}
// check Veterans Day (November 11)
if (cal.get(Calendar.MONTH) == Calendar.NOVEMBER
&& cal.get(Calendar.DAY_OF_MONTH) == 11) {
return true;
}
// check MLK Day (3rd Monday of January)
if (cal.get(Calendar.MONTH) == Calendar.JANUARY
&& cal.get(Calendar.DAY_OF_WEEK_IN_MONTH) == 3
&& cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
return true;
}
return false;
}
I do not know the "easy" way but I can suggest you the following.
Set calendar to the first day of the month.
Retrieve its day of the week
Calculate the date of the first Monday of the month.
Add 14 days using calendar.add() method. You will get the third Monday.
tl;dr
LocalDate thirdMondayInFebruary2012 =
YearMonth.of( 2012 , Month.FEBRUARY )
.atDay( 1 )
.with( TemporalAdjusters.dayOfWeekInMonth( 3 , DayOfWeek.MONDAY ) );
…and…
LocalDate lastMondayInMay2012 =
YearMonth.of( 2012 , Month.MAY )
.atDay( 1 );
.with( TemporalAdjusters.lastInMonth( DayOfWeek.MONDAY ) );
java.time
Very easy to do using the java.time classes that now supplant both Joda-Time and the troublesome old date-time classes bundled with the earliest versions of Java.
First we need to specify the desired month. The YearMonth class can do that. From there we get a LocalDate, a date without a time-of-day and without a time zone.
YearMonth ym = YearMonth.of( 2012 , Month.FEBRUARY ); // Or pass '2' for 'February'.
LocalDate ld = ym.atDay( 1 );
The TemporalAdjuster interface provides for adjusting a date-time value into another date-time value. Implementations provided by the TemporalAdjusters class (notice the plural 's'). Specify a day of week using the handy DayOfWeek enum.
int ordinal = 3 ; // Use '3' for 'third occurrence in month' such as '3rd Monday'.
LocalDate thirdMondayInFebruary2012 = ld.with( TemporalAdjusters.dayOfWeekInMonth( ordinal , DayOfWeek.MONDAY ) );
Tip: Rather than pass mere integers for year and month across your code base, pass objects such as YearMonth, Year, Month, and DayOfWeek. Doing so eliminates ambiguity, makes your code more self-documenting, provides types-safety, and ensures valid values.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, and later
Built-in.
Part of the standard Java API with a bundled implementation.
Java 9 adds some minor features and fixes.
Java SE 6 and Java SE 7
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
Later versions of Android bundle implementations of the java.time classes.
For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
All you need is a loop:
public class CalculateDate {
public static void main( String ... args ) {
Calendar c = Calendar.getInstance();
c.set( Calendar.YEAR, 2012 );
c.set( Calendar.MONTH , Calendar.MAY);
c.set( Calendar.DAY_OF_MONTH, 0 );
c.add( Calendar.DAY_OF_MONTH, -1 );
System.out.println( c.getTime() );
int mondaysCount = 0;
while ( mondaysCount != 4 ) {
c.add( Calendar.DAY_OF_MONTH, 1 );
if ( c.get( Calendar.DAY_OF_WEEK ) == Calendar.MONDAY ) {
mondaysCount++;
}
}
System.out.printf( "The fourth monday of may is %s", c.getTime() );
}
}
Any chances you are using Quartz scheduler?
public Date date(String cronExpression) {
return new org.quartz.CronExpression(cronExpression).
getNextValidTimeAfter(new Date(0));
}
System.out.println(date("0 0 0 ? May Thu#3 2015"));
System.out.println(date("0 0 0 ? Jun Mon#1 2050"));
System.out.println(date("0 0 0 ? Dec Tue#4 2012"));
System.out.println(date("0 0 0 ? Jul Wed#2 2000"));
This simple code prints correct (?) results:
Thu May 21 00:00:00 CEST 2015
Mon Jun 06 00:00:00 CEST 2050
Tue Dec 25 00:00:00 CET 2012
Wed Jul 12 00:00:00 CEST 2000
The required CronExpression doesn't have any dependencies on the rest of Quartz, so you might consider copying it to your project (watch out for license!)
Side note: the internal implementation of getNextValidTimeAfter() is 400 lines of code...
public String nDow(int year, int month, int nweek, int nday)
{
Calendar cdt = Calendar.getInstance();
cdt.set(year, month -1, 1);
return year + "-" + month + "-" + (getPosOfWeekday(cdt.get(Calendar.DAY_OF_WEEK), nday) + ((nweek - 1) *7));
}
private int getPosOfWeekday(int startday, int nday)
{
nday = weekDayValue(nday);
return constructCircularArray(startday).indexOf(nday) + 1;
}
private ArrayList<Integer> constructCircularArray(int weekday)
{
ArrayList<Integer> circularArray = new ArrayList<Integer>();
for(int i = 0; i < 7; i++)
{
circularArray.add(i, weekDayValue(weekday++));
}
return circularArray;
}
private int weekDayValue(int x)
{
return ((x-1) % 7) + 1;
}
Here is an alternative. What it does is: Get which week you want (n), and the other parameters, and return the date of the day in that week. Since Calendar gives the date of the previous month (for example 29th of February instead of 7th of March, since the 1st week of March collides with last week of Feb), the function computes the 2nd week if the date goes beyond 7 or multiples of it for each week thereof. Hope that helps.
public static int getNthWeekDay (int n, int day, int month, int year) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, day);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.WEEK_OF_MONTH,n);
calendar.set(Calendar.YEAR, year);
if (calendar.get(Calendar.DATE) > n * 7) {
calendar.set(Calendar.DAY_OF_WEEK,day);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.WEEK_OF_MONTH,day+1);
}
return calendar.get(Calendar.DATE);
}

Categories