Get week range of current month in Android - java

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;
}

Related

is there any easy way to get number of weekend in a month in java [duplicate]

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

How can i calculate many week days in a month?

I'm trying to find out how many Mondays, for example, there are in a specific month of a specific year.
Is there a library to import in java of a calendar of a specific year?
java.time
Using the java.time classes built into Java 8 and later.
YearMonth month = YearMonth.of(2017, 1);
LocalDate start = month.atDay(1).with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
int count = (int) ChronoUnit.WEEKS.between(start, month.atEndOfMonth()) + 1;
System.out.println(count);
Java 7 + JodaTime
import org.joda.time.DateTimeConstants;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import java.util.Date;
public class Test {
public static int getNumberOfMondays(Date start, Date end) {
LocalDate startDate = LocalDate.fromDateFields(start);
LocalDate endDate = LocalDate.fromDateFields(end);
int days = Days.daysBetween(startDate, endDate).getDays();
int mondayCount = 0;
//Get number of full weeks: 1 week = 1 Monday
mondayCount += days / 7;
int remainder = days % 7;
if (startDate.dayOfWeek().get() == DateTimeConstants.MONDAY || startDate.dayOfWeek().get() > (startDate.dayOfWeek().get() + remainder) % 8) {
mondayCount++;
}
return mondayCount;
}
}
Even the old `Calendar´-API with all its disadvantages enables a solution:
int year = 2017;
int month = Calendar.JANUARY;
int dow = Calendar.MONDAY;
GregorianCalendar gcal = new GregorianCalendar(year, month, 1);
while (gcal.get(Calendar.DAY_OF_WEEK) != dow) {
gcal.add(Calendar.DAY_OF_MONTH, 1);
}
System.out.println(gcal.getActualMaximum(Calendar.DAY_OF_WEEK_IN_MONTH)); // 5
The new Java-8-API does not have a DAY_OF_WEEK_IN_MONTH-field (in strict sense), but you can do this:
int year = 2017;
int month = 1;
LocalDate ld1 =
LocalDate.of(year, month, 1).with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
LocalDate ld2 =
LocalDate.of(year, month, 1).with(TemporalAdjusters.lastInMonth(DayOfWeek.MONDAY));
System.out.println(ChronoUnit.WEEKS.between(ld1, ld2) + 1); // 5

Current month count monday?

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.

Get Week Dates From Monday to Friday from a Given Date [duplicate]

This question already has answers here:
get week start and end date from week number & year in android
(2 answers)
Closed 10 years ago.
I want to get the Week Dates from Monday to Friday From a Given Date.
Suppose if the Given Date is 6-2-2013
I want the result :
Monday 4-2-2013
Tuesday 5-2-2013
Wednesday 6-2-2013
Thursday 7-2-2013
Friday 8-2-2013
or Even i have the Week no as 6 ...How can i achieve the About result
I get the Week no from the Below Code
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, 6);
c.set(Calendar.MONTH, 1);
c.set(Calendar.YEAR, 2013);
int weekNo = c.get(Calendar.WEEK_OF_YEAR);
Try this...I have edited and added more changes to Achintya Jha Code...Works Fine
Thanks to Achintya Jha
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, 14);
c.set(Calendar.MONTH, 1);
c.set(Calendar.YEAR, 2013);
int weekNo = c.get(Calendar.WEEK_OF_YEAR);
//Set the Week No
c.set(Calendar.WEEK_OF_YEAR, weekNo);
c.clear();
//Set the Week Year and the Week No
c.set(Calendar.WEEK_OF_YEAR, weekNo);
c.set(Calendar.YEAR, 2013);
SimpleDateFormat formatter = new SimpleDateFormat("EEE dd/MM/yyyy"); // PST`
//Get Week Start Date
Date startDate = c.getTime();
//Now it will be sunday so add plus one so now it becomes Monday
c.add(Calendar.DATE, 1);
for (int i = 0; i < 5; i++) {
//From Monday to Friday Dates will be Printed
System.out.println(formatter.format(c.getTime()));
c.add(Calendar.DATE, 1);
}
From 6-2-2013:
GregorianCalendar c = new GregorianCalendar(2013, 1, 6);
c.add(Calendar.DAY_OF_YEAR, Calendar.MONDAY - c.get(Calendar.DAY_OF_WEEK));
DateFormat df = new SimpleDateFormat("d-M-yyyy");
while(c.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) {
if (c.get(Calendar.YEAR) == 2013) {
System.out.printf("%-10tA%10s%n", c.getTime(), df.format(c.getTime()));
}
c.add(Calendar.DAY_OF_YEAR, 1);
}
prints
Monday 4-2-2013
Tuesday 5-2-2013
Wednesday 6-2-2013
Thursday 7-2-2013
Friday 8-2-2013
from week 6 of 2013:
GregorianCalendar c = new GregorianCalendar(2013, 0, 1);
c.add(Calendar.WEEK_OF_YEAR, 6 - 1);
c.add(Calendar.DAY_OF_YEAR, Calendar.MONDAY - c.get(Calendar.DAY_OF_WEEK));
DateFormat df = new SimpleDateFormat("d-M-yyyy");
while(c.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) {
if (c.get(Calendar.YEAR) == 2013) {
System.out.printf("%-10tA%10s%n", c.getTime(), df.format(c.getTime()));
}
c.add(Calendar.DAY_OF_YEAR, 1);
}
prints
Monday 4-2-2013
Tuesday 5-2-2013
Wednesday 6-2-2013
Thursday 7-2-2013
Friday 8-2-2013
public static void main(String[] args) {
getWeekOfDates(2013, 2, 11);
}
private static void getWeekOfDates(int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
int weekOfMonth = c.get(Calendar.WEEK_OF_MONTH);
c.set(Calendar.WEEK_OF_MONTH, weekOfMonth);
DateFormat df = new SimpleDateFormat("EEE dd-MM-yyyy");
for (int i = 0; i < 5; i++) {
System.out.println(df.format(c.getTime()));
c.add(Calendar.DATE, 1);
}
}
Try this: I edited as your requirement.
Output:
Mon 11-03-2013
Tue 12-03-2013
Wed 13-03-2013
Thu 14-03-2013
Fri 15-03-2013

java - to calculate number of weeks - with start date as MONDAY and end date as SUNDAY

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;
}

Categories

Resources