I'd like to get the next Friday of even week (considerning the weeks of the year numbered from 1 to 54).
For example if today is Friday and the week is even I want the next Friday. If today is Thursday of an odd week I want the date of tomorrow.
The test input / output is the following:
14/09/2012 -> 14/09/2012
15/09/2012 -> 28/09/2012
21/09/2012 -> 28/09/2012
22/09/2012 -> 28/09/2012
29/09/2012 -> 12/10/2012
25/12/2012 -> 04/01/2013
29/12/2012 -> 04/01/2013
Calendar today = Calendar.getInstance();
int dayOfWeek = today.get(Calendar.DAY_OF_WEEK);
int daysUntilNextFriday = Calendar.FRIDAY - dayOfWeek;
if(daysUntilNextFriday < 0){
daysUntilNextFriday = daysUntilNextFriday + 7;
}
Calendar nextFriday = (Calendar)today.clone();
nextFriday.add(Calendar.DAY_OF_WEEK, daysUntilNextFriday);
if(nextFriday.get(Calendar.WEEK_OF_YEAR) % 2 == 0){
nextFriday.add(Calendar.DAY_OF_WEEK, 7);
}
System.out.println(new SimpleDateFormat("dd/MM/yyyy").format(nextFriday.getTime()));
Related
I've been stuck on this issue for the past 4 hours trying different strategies which haven't prevailed much at all having checked countless posts on here and Google. Basically what I am trying to do is print out only the business working days within a week mon-fri. I have tried incrementing the day of the year each time the weekend days come up but of course this messes up the order of the days after. For example instead of having:
Iteration: 0, 1, 2, 3, 4, 5, 6, 7, 8....
Day: (0)Friday, (1)Saturday, (2)Sunday, (3)Monday, (4)Tuesday, (5)Wednesday, (6)Thursday, (7)Friday, (8)Saturday....etc
I require:
Day: (0)Friday, (1)Monday, (2)Tuesday (3)Wednesday, (4)Thursday, (5)Friday, (6)Monday, (7)Tuesday, (8)Wednesday....etc
Here's the code:
public static void date(int day) {
now = Calendar.getInstance();
//SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");//version 1
SimpleDateFormat format = new SimpleDateFormat("EEEE d MMMM yyyy"); //version 2
String[] days = new String[maxDayCount]; //limits the number of days to print out(ordinarily h)
now.add(Calendar.DAY_OF_YEAR, day + 1); //Increments from test count + 1 due to count starting at 0 (would be today). Increments the date to get from today.
int DayOfWeek = now.get(Calendar.DAY_OF_WEEK);
boolean WorkingDayCheck = ((DayOfWeek >= Calendar.MONDAY) && (DayOfWeek <= Calendar.FRIDAY));
if(WorkingDayCheck) {
days[day] = format.format(now.getTime());
} else {
//
}
now.add(Calendar.DAY_OF_MONTH, 1);
System.out.println("-----------Day " + day + ": " + String.valueOf(days[day])); //v2: print out each line
}//END OF METHOD: date
The idea is that I have an iteration elsewhere which passes the day number to this method in order for this to use that number to then print out the date. I am now back to what I was with originally before trying a load of things which at the moment with the check I am doing with the WorkingDayCheck boolean the print out is of course returning null when the weekend days come about.
Any ideas chaps?
Thanks for your time.
I think this will solve your problem.
First, why do you define the array days each time you enter the method then you populate only one value in it?
Your String[] days = new String[maxDayCount]; should be a member variable not a local variable. So initialize this array outside this method.
Also initialize another int that represent the last empty location in your array. (defaults to 0)
public class Foo {
int maxDayCount = 365;
String[] days = new String[maxDayCount];
int finalIndex = 0;
public static void date(int day) {
now = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("EEEE d MMMM yyyy");
String[] days = new String[maxDayCount]; //limits the number of days to print out(ordinarily h)
now.add(Calendar.DAY_OF_YEAR, day + 1); //Increments from test count + 1 due to count starting at 0 (would be today). Increments the date to get from today.
int DayOfWeek = now.get(Calendar.DAY_OF_WEEK);
boolean WorkingDayCheck = ((DayOfWeek >= Calendar.MONDAY) && (DayOfWeek <= Calendar.FRIDAY));
if(WorkingDayCheck) {
days[finalIndex++] = format.format(now.getTime());
System.out.print("(" + (finalIndex - 1) + ")" + String.valueOf(days[finalIndex - 1]))
} else {
//
}
}
public static void main(String[]args) {
int day = 0;
while(finalIndex != maxDaysCount) {
date(day++);
}
}
}
Now your days array will be populated with working days only in their respective indices. For example in days[0] will be Friday then days[1] will be Saturday and so on since you have a pointer finalIndex that is independent of the value of day that is input to your method from the iteration.
If I get your problem correctly you want to do somewhat like following,
Working Day #1 = 0 Week + 1 Day = 1st Day
..
Working Day #5 = 0 Week + 5 Days = 5th Day
Working Day #6 = 1 Week + 1 Day = 8th Day
..
Working Day #10 = 1 Week + 5 Days = 12th Day
And then print the 1st,2nd,3rd calendar day of the year.
If that is the case you can try finding the calendar day from working day like something,
int div = day/5;
int rem = day%5;
int calendarDay = 0;
if(day>5){
if(rem == 0)
calendarDay = (div-1)*7 + 5;
else
calendarDay = div*7 + rem;
}else
calendarDay = day;
And then find the Calendar day for that date like
Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.DAY_OF_YEAR, day);
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb, Locale.ENGLISH);
formatter.format("%tD", calendar);
System.out.println(sb);
The code is not a solution, but I guess it points to where you want to go.
Having played around with the code I have achieved what I was after now with:
public static String getDate4(int day) {
now = Calendar.getInstance();
//SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");//version 1
SimpleDateFormat format = new SimpleDateFormat("EEEE d MMMM yyyy"); //version 2
now.add(Calendar.DAY_OF_YEAR, day + 1); //Increments from test count + 1 due to count starting at 0 (would be today). Increments the date to get from today.
int DayOfWeek = now.get(Calendar.DAY_OF_WEEK);
boolean WorkingDayCheck = ((DayOfWeek >= Calendar.MONDAY) && (DayOfWeek <= Calendar.FRIDAY));
if(WorkingDayCheck) {
days[finalIndex++] = format.format(now.getTime());
return "\n(" + (finalIndex - 1) + ") " + String.valueOf(days[finalIndex - 1]);
} else {
now.add(Calendar.DATE, 2); //advance dates by two
dayCount = dayCount + 2; //Increase the day count by 2 to ensure that later dates are also incremented to avoid duplication i.e. Monday(Sat), Tuesday(Sun), Monday, Tuesday, Wed...
days[finalIndex++] = format.format(now.getTime());
return "\n(" + (finalIndex - 1) + ") " + String.valueOf(days[finalIndex - 1]);
}//END OF if else
}//END OF getDate4 method
I had to add and use an external dayCount (similar to the day int #Mohammed Osama used) separate from the count used in the mains methods iteration which I used previously to feed into this method and then when a weekend came up I incremented the date by 2 which is something I had experimented with before however the problem came when the weekend ended and it was back onto non weekend dates which ended up with:
Friday
Monday (previously Saturday)
Tuesday (previously Sunday)
Monday (weekend over, now back onto Monday) <--- Issue here and onwards
Tuesday
This is where the new dayCount came in which is now fed into this method simply as:
//Main method
//Iteration code start
getDate4(dayCount++);
//Iteration end
Incrementing this by 2 each time a weekend was encountered pushed the later dates to their appropriate position therefore now I am getting what I was always after being:
Friday
Monday (previously Saturday) <-Pushed ahead by 2 dates to Monday
Tuesday (previously Sunday) <-Pushed ahead by 2 dates to Tuesday
Wednesday (previously Monday) <-Pushed ahead 2 dates to Wednesday due to weekends incrementing the dayCount by 2
Thursday <--- etc. etc.
Thanks a lot for the help folks, can move on now finally :>
How do you find the total number of Mondays (e.g. 4 or 5) in a particular month ???
Calendar c = Calendar.getInstance();
int mon = c.getActualMaximum(Calendar.MONDAY);
is this right way ??
you can use this method
public int countMonday(int year, int month) {
Calendar calendar = Calendar.getInstance();
// Note that month is 0-based in calendar, bizarrely.
calendar.set(year, month - 1, 1);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int count = 0;
for (int day = 1; day <= daysInMonth; day++) {
calendar.set(year, month - 1, day);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.MONDAY) {
count++;
// Or do whatever you need to with the result.
}
}
return count;
}
Updated
public int countDayOccurence(int year, int month,int dayToFindCount) {
Calendar calendar = Calendar.getInstance();
// Note that month is 0-based in calendar, bizarrely.
calendar.set(year, month - 1, 1);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int count = 0;
for (int day = 1; day <= daysInMonth; day++) {
calendar.set(year, month - 1, day);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == dayToFindCount) {
count++;
// Or do whatever you need to with the result.
}
}
return count;
}
And then you can call this method for each day name
int countMonday = countDayOccurence(year,month,Calendar.MONDAY);
int countTuesday = countDayOccurence(year,month,Calendar.TUESDAY);
...............................................
Using the Calendar api, the best option I can see is to:
get the actual maximum day of month (i.e. how many days in this month)
set calendar to first day of the month and get the day of the week
calculate how many mondays could occur (i.e. if 28 days in month - 4, if 29 4 unless month started on a monday, if 30, 4 unless month started on monday or tuesday, if 31, 4 unless started on monday, tuesday, or wednesday).
Here is what you need at least for the current month :
Calendar c = Calendar.getInstance();
int maxDaysInMonth = c.getMaximum(Calendar.DAY_OF_MONTH);
int firstMonday = c.get(Calendar.MONDAY); // first monday in the month (Beware, 0 is the first day of the month)
int mondays = 0;
int i=firstMonday;
while(i<maxDaysInMonth){
mondays++;
i+=7;
};
System.out.println(mondays);
As per my question.
#Ramzan Zafar:
Answer is right and more in that i created my calender instance for current year and month for year and month.
I got my answer as per my question.
I ran into a simple problem, which I solved ( I didn't give up ). However, I think that there is some more neat and tricky solution.
The problem is as follow : Return the date of the last X day before today. For example If today is Tuesday, July 09, 2013 and I want the last Friday the answer would be Friday, July 05, 2013.
My solution is as follow :
public Date dateOfLast(int day) {
int today = calendar.get(Calendar.DAY_OF_WEEK);
int daysDifferences = today - day;
int daysToSubtract;
if (day < today) {
//last day seems to be in current week !
//for example Fr > Tu.
daysToSubtract = -(Math.abs(daysDifferences));
} else {
//7- ( difference between days )!
//last day seems to be in the previous,thus we subtract the the days differences from 7
// and subtract the result from days of month.
daysToSubtract = -(7 - Math.abs(daysDifferences));
}
//subtract from days of month.
calendar.add(Calendar.DAY_OF_MONTH, daysToSubtract);
return calendar.getTime();
}
Anyone give me a mathematical formula or a simpler solution, if any ?
int daysToSubtract = ((today - day) + 7) % 7;
should be OK, if I'm not mistaken.
For example
today = 4
day = 2
daysToSubtract = ((4 - 2) + 7) % 7 = 2 : correct
today = 2
day = 4
daysToSubtract = ((2 - 4) + 7) % 7 = 5 : correct
Your solution looks good to me. But a tip: you shouldn't need to use Math.abs here, you should know which of your variables, today or day, is bigger in each branch of your if-statement:
if (day < today)
daysToSubtract = day - today; // 'today' is bigger
else
daysToSubtract = day - today - 7; // 'day' is bigger
or just
int daysToSubtract = day - today - ((day < today) ? 0 : 7);
Notice that we don't need the daysDifferences variable anymore.
Following is the code I used to calculate the number of weeks in a Month. But actually I need number of weeks with each week's start day as MONDAY and end day as SUNDAY. For example, JANUARY, 2012 will have 5 weeks. But with the above criteria, it will have 6 weeks.
January 2012
first week - Sunday 01
second week - 2 Mon to 8 Sunday
Third week - 9 Monday to 15 Sunday
fourth week - 16 Mon to 22 Sunday
fifth week - 23 Mon to 29 Sunday
Sixth week - 30 Monday to 31 Tuesday.
The following code gives only 5 weeks.
public class Test {
public static void main(String[] args)
{
Calendar calendar = Calendar.getInstance();
int year = 2012;
int month = Calendar.JANUARY;
int date = 1;
calendar.set(year, month, date);
int numOfDaysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int numOfWeeksInMonth = calendar.getActualMaximum(Calendar.WEEK_OF_MONTH);
System.out.println("Number of Days In Month: " + numOfDaysInMonth);
System.out.println("Number of Weeks In Month: " + numOfWeeksInMonth);
}
}
The output for the above code is
Number of Days In Month: 31
Number of Weeks In Month: 5
But I need to get the "Number of Weeks In Month:" as 6
*Also I am trying to get the start date and the end date of each week in ddMMYYYY format.. *
I am still working on it.
Can anyone please help me in fixing this?
Add this to get "Number of Weeks In Month:" as 6,
calendar.setFirstDayOfWeek(Calendar.MONDAY);
try this function
public static void main(String[] args) {
System.out.println(getNumberOfWeeks(2012, Calendar.JANUARY));
}
static int getNumberOfWeeks(int year, int month) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, 1);
int numOfWeeksInMonth = 1;
while (c.get(Calendar.MONTH) == month) {
c.add(Calendar.DAY_OF_MONTH, 1);
if (c.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
numOfWeeksInMonth++;
}
}
return numOfWeeksInMonth;
}
try this:
public static int noWeeks(int year,int month)
{
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, 1);
int initDay = c.get(Calendar.DAY_OF_WEEK)-1
;
int days = c.getActualMaximum(Calendar.DAY_OF_MONTH);
int a = (initDay==0?7:initDay)+days-1;
return a/7+(a%7==0?0:1);
}
O algoritmo correto é esse:
public static int getTotalWeeksOfMonth(int month, int year){
int monthIndex = month-1;
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, monthIndex);
c.set(Calendar.DAY_OF_MONTH, 1);
int numOfWeeksInMonth = 0;
while (c.get(Calendar.MONTH) == monthIndex) {
c.add(Calendar.DAY_OF_MONTH, 1);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
if ( dayOfWeek == Calendar.SUNDAY) {
numOfWeeksInMonth++;
}
}
if (c.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
numOfWeeksInMonth++;
}
return numOfWeeksInMonth;
}
Knowing the year, week of year and day of week is it possible to obtain the month of year and the day of month. For example
// corresponding to September 15, 2012 if week starts on Monday
int weekNum = 38;
int dayNum = 6;
int year = 2012;
// set the calendar instance the a week of year and day in the future
Calendar aGMTCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
aGMTCalendar.setFirstDayOfWeek(Calendar.MONDAY);
aGMTCalendar.set(Calendar.WEEK_OF_YEAR,weekNum );
aGMTCalendar.set(Calendar.DAY_OF_WEEK,dayNum );
aGMTCalendar.set(Calendar.YEAR,year);
// get the month and day of month
int monthGMT = aGMTCalendar.get(Calendar.MONTH + 1); // returns 38 not 9
int dayOfMonthNumGMT = aGMTCalendar.get(Calendar.DAY_OF_MONTH);
// returns 14 but I wanted 15
Thank you
This should be
// +1 to the value of month returned, not to the value of MONTH constant.
int monthGMT = aGMTCalendar.get(Calendar.MONTH) + 1;
The way you obtain the monthGMT has a type. It should be:
int monthGMT = aGMTCalendar.get(Calendar.MONTH) + 1;
Put the line below after each aGMTCalendar.set() call and you will see that after calling the dayNum one, the date changes from 15 to 14. The aGMTCalendar.set(Calendar.DAY_OF_WEEK, dayNum) ignores the setFirstDayOfWeek, which is however considered when setting the WEEK_OF_YEAR.
System.out.println(aGMTCalendar.getTime());
// corresponding to September 15, 2012 if week starts on Monday
int weekNum = 38;
int dayNum = 6;
int year = 2012;
// set the calendar instance the a week of year and day in the future
Calendar aGMTCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
aGMTCalendar.setFirstDayOfWeek(Calendar.MONDAY);
aGMTCalendar.set(Calendar.WEEK_OF_YEAR,weekNum );
aGMTCalendar.set(Calendar.DAY_OF_WEEK,dayNum );
aGMTCalendar.set(Calendar.YEAR,year);
// get the month and day of month
int monthGMT = aGMTCalendar.get(Calendar.MONTH) + 1;
// should be 10
int dayOfMonthNumGMT = aGMTCalendar.get(Calendar.DAY_OF_MONTH) + 1;
// should be 15
Try Calendar.SATURDAY constant instead of 6 literal.
Calendar.SATURDAY is in fact 7 not 6.