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.
Related
I would like to show the week range in the MPAndroidChart Graph. For example in February month I would like to show the xAxis label value like 1-4, 5-11, 12- 18, 19-25, 26-28. Here 1-4 comes from the 1st week of February where previous month dates also availble. But I need only current month days. However I am getting all the dates in the week.
public List<String> getWeeksInCurrentMonth() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
int month = cal.get(Calendar.MONTH);
List<String> weekRanges = new ArrayList<>();
while (cal.get(Calendar.MONTH) == month) {
int week = cal.get(Calendar.WEEK_OF_MONTH);
int year = cal.get(Calendar.YEAR);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.SUNDAY || cal.getActualMaximum(Calendar.DAY_OF_MONTH) == cal.get(Calendar.DAY_OF_MONTH)) {
int startDay = cal.get(Calendar.DAY_OF_MONTH) - (dayOfWeek - 1);
int endDay = cal.get(Calendar.DAY_OF_MONTH) + (7 - dayOfWeek);
if (endDay > cal.getActualMaximum(Calendar.DAY_OF_MONTH)) {
endDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}
if (startDay <= endDay && startDay <= cal.getActualMaximum(Calendar.DAY_OF_MONTH)) {
weekRanges.add(String.format("%d-%d", startDay, endDay));
}
}
cal.add(Calendar.DAY_OF_MONTH, 1);
}
System.out.println(weekRanges);
return weekRanges;
}
Someone please shed some light here, to acheive the week range with only current month date.
To get the week ranges with only current month dates, you can modify the logic in your getWeeksInCurrentMonth() method to take the current month into account and exclude any dates from previous or next months. Here's an updated implementation that should achieve the desired behavior:
public List<String> getWeeksInCurrentMonth() {
Calendar cal = Calendar.getInstance();
int currentMonth = cal.get(Calendar.MONTH);
int currentYear = cal.get(Calendar.YEAR);
cal.set(Calendar.DAY_OF_MONTH, 1);
List<String> weekRanges = new ArrayList<>();
while (cal.get(Calendar.MONTH) == currentMonth) {
int week = cal.get(Calendar.WEEK_OF_MONTH);
int year = cal.get(Calendar.YEAR);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
// Only consider days in the current month
if (cal.get(Calendar.MONTH) == currentMonth && cal.get(Calendar.YEAR) == currentYear) {
int startDay = cal.get(Calendar.DAY_OF_MONTH);
int endDay = startDay + (7 - dayOfWeek);
if (endDay > cal.getActualMaximum(Calendar.DAY_OF_MONTH)) {
endDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}
weekRanges.add(String.format("%d-%d", startDay, endDay));
}
cal.add(Calendar.DAY_OF_MONTH, 7 - dayOfWeek + 1);
}
System.out.println(weekRanges);
return weekRanges;
}
This question already has answers here:
In Java, get all weekend dates in a given month
(4 answers)
Closed 3 years ago.
I want to find number of weekend (here weekend may be one or more day) from a particular month of particular date. Like if number of weekend is 2 i.e. saturday and sunday than how to calculate total number of weekend from a date like 2019-11-15 in java.
public int weekendCount(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int cnt = 0;
for (int i= 1; i<= daysInMonth; i++) {
calendar.set(year, month - 1, i);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.SUNDAY || dayOfWeek == Calendar.SATURDAY) {
cnt++;
}
}
return cnt;
}
Try this.
year -2019, month -11
output=9
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.
You are given the following information, but you may prefer to do some research for yourself.
1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
Solution :
My following logic gives me 173 Sundays , whereas there are 171 Sundays !! Where are the extra 2 days came from ?
public static void main(String args[]) {
Date startDate = new Date(1901, Calendar.JANUARY, 01);
Date endDate = new Date(2000, Calendar.DECEMBER, 31);
checkSundays(startDate, endDate);
}
private static void checkSundays(Date start, Date end) {
int dayNum;
Calendar startDate = Calendar.getInstance();
startDate.setTime(start);
System.out.println(startDate.getTime());
Calendar endDate = Calendar.getInstance();
endDate.setTime(end);
System.out.println(endDate.getTime());
int count = 0;
while (startDate.before(endDate)) {
for (int i = 1; i < 13; i++) {
dayNum = startDate.get(Calendar.DAY_OF_WEEK);
if (dayNum == 1) {
count++;
}
System.out.println(startDate.getTime());
startDate.add(Calendar.MONTH, 1);
}
System.out.println("Count " + count);
}
}
Your following code uses deprecated constructor of Date
Date startDate = new Date(1901, Calendar.JANUARY, 01);
System.out.println(startDate);
which is not proper, it prints
Thu Jan 01 00:00:00 IST 3801
So use Calendar to construct Date,
Calendar startDateCal = createDateInstance(0,1901,1)
Calendar endDateCal = createDateInstance(11,2000,13)
and a Factory method
public static Date createDateInstance(int month, int year, int date){
Calendar cal= Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DATE, date);
return cal.getTime();
}
See your working code
You can use probability and do this with just a calculator.
There's 100 years in a century, and every year, there are 12 first days of the months. Divide that by 7 and you have your answer.
It's cheap, but it works.