I have used below library for calendar but it display firstdayofweek is Monday i want to start day with sunday.
https://github.com/Applandeo/Material-Calendar-View
I have changed code accordingly but it still display date accordingly monday is firstday.
ArrayList<Date> days = new ArrayList<>();
// Get Calendar object instance
Calendar calendar = (Calendar) mCalendarProperties.getCurrentDate().clone();
// Add months to Calendar (a number of months depends on ViewPager position)
calendar.add(Calendar.MONTH, position);
// Set day of month as 1
calendar.set(Calendar.DAY_OF_MONTH, 1);
// Get a number of the first day of the week
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
// Count when month is beginning
int monthBeginningCell = dayOfWeek + (dayOfWeek == 1 ? 5 : -2);
// Subtract a number of beginning days, it will let to load a part of a previous month
calendar.add(Calendar.DAY_OF_MONTH, -monthBeginningCell);
/*
Get all days of one page (42 is a number of all possible cells in one page
(a part of previous month, current month and a part of next month))
*/
while (days.size() < 42) {
days.add(calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
mPageMonth = calendar.get(Calendar.MONTH) - 1;
CalendarDayAdapter calendarDayAdapter = new CalendarDayAdapter(this, mContext,
mCalendarProperties, days, mPageMonth);
mCalendarGridView.setAdapter(calendarDayAdapter);
Try this:
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
Related
I have A string which is
String dateOfOrder = "01/06/2019";
I would like to get the number of days in Week 1 as June first week only have 1 day.
May i know how do i approach on receviing number of days in Week X ?
Like In June 2019
Week 1 : Have 1 day Week 2 - 5 : have 7 days Week 6 : Have 1 day
EDIT
I'm thinking of this approach of getting number of days in a week whereby I have a couple of String arrayList. For example on my given date above, if Week 1 Have Saturday. Week 1 Array List will add saturday in it and .size().
If Week 2 have Sunday to Saturday, it will add all the dates in the Week 2 Array List and .size() to get the number of days in the ArrayList.
Lastly, The last week of June which is Week 6 will have Sunday only and it will add into Week 6 ArrayList and have value of Sunday only and .size() to get 1.
I'm wondering if its the best approach and I'm not sure if i can get the correct days in the week.
Parse the string as date using SimpleDateFormatter. Your parsing pattern could be dd/MM/YYYY.
DateFormat df = new SimpleDateFormat("dd/MM/YYYY");
Date date = df.parse(dateOfOrder);
Once you have the date, get day of week using Calendar.
Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
Calculate number of days in the given week (and month).
int daysInMonth = c.getActualMaximum(Calendar.DAY_OF_MONTH);
int currentMonth = c.get(Calendar.MONTH);
int prevWeekMonth = c.add(Calendar.DAY_OF_MONTH, -7).get(Calendar.MONTH);
c.setTime(yourDate);
int nextWeekMonth = c.add(Calendar.DAY_OF_MONTH, 7).get(Calendar.MONTH);
c.setTime(yourDate);
if (prevWeekMonth != currentMonth) {
// first or second week
if (dayOfMonth > dayOfWeek) {
// second week
numOfDaysInWeek = 7;
} else {
// first week
int firstDay = c.set(Calendar.DAY_OF_MONTH, 1).get(Calendar.DAY_OF_WEEK);
numOfDaysInWeek = 7 - firstDay;
}
} else if (nextWeekMonth != currentMonth) {
// last or second last week
if (dayOfMonth - dayOfWeek + 7 >= daysInMonth) {
// last week
numOfDaysInWeek = c.set(Calendar.DAY_OF_MONTH, daysInMonth).get(Calendar.DAY_OF_WEEK);
} else {
// second last week
numOfDaysInWeek = 7;
}
} else {
// middle weeks
numOfDaysInWeek = 7;
}
Note: the conditions are a bit rough and you might need to add 1 depending on whether dayOfWeek, dayOfMonth have 0 or 1 index.
Actually i want to display a set of data with the start and end date of the week were that particular date falls on. In my emulator its working fine. Eg. If i give Apr 23 its giving me start date of the week as 22 Apr and end date as 28 Apr, but if i try to build the same code in my device its showing start date of the week as 27 Apr and end date as 28 Apr.
Piece of Code which i am using:
//to get first day of week
cal1.set(Calendar.DAY_OF_WEEK, 1);
int day1 = cal1.get(Calendar.DAY_OF_MONTH);
//to get last day of week
cal1.set(Calendar.DAY_OF_WEEK, 7);
int day7 = cal1.get(Calendar.DAY_OF_MONTH);
I don't know why you getting that data but this is how I get the first and last date, take look might help. (its written to give current week's first and last date, so might have to tweak it little bit.)
Calendar calendar = Calendar.getInstance();
Date date1 = calendar.getTime();
//current date to check that our week lies in same month or not
SimpleDateFormat checkformate = new SimpleDateFormat("MM/yyyy");
String currentCheckdate= checkformate.format(date1);
int weekn = calendar.get(Calendar.WEEK_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
//resat calender without date
calendar.clear();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.set(Calendar.WEEK_OF_MONTH,weekn);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.YEAR,year);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date datef = calendar.getTime();
//move date to 6 days + to get last date of week
Long timeSixDayPlus = calendar.getTimeInMillis()+518400000L;
Date dateL = new Date(timeSixDayPlus);
String firtdate = simpleDateFormat.format(datef);
String lastdate = simpleDateFormat.format(dateL);
String firtdateCheck = checkformate.format(datef);
String lastdateCheck = checkformate.format(dateL);
//if our week lies in two different months then we show only current month week part only
if (!firtdateCheck.toString().equalsIgnoreCase(currentCheckdate)) {
firtdate = "1" + "/" + calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.YEAR);
}
if (!lastdateCheck.toString().equalsIgnoreCase(currentCheckdate)) {
int ma = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
lastdate = String.valueOf(ma) + "/" + calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.YEAR);
}
Log.e("current","=>>"+firtdate+" to "+lastdate);
To get the first day of the week -
initialize the Calendar as per your need. in this case, I am getting the current date calendar.
set the current day of the week to the first day of the week.
get the corresponding date.
Calendar calendar = Calendar.getInstance();
//optional step
calendar.setFirstDayOfWeek(Calendar.SUNDAY);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
int firstDateOfWeek = calendar.get(Calendar.DATE);
To get the last date of the week -
follow the step as above to initialize.
set the day of the week as Saturday.
get the corresponding date.
Calendar calendar = Calendar.getInstance();
//optional step
calendar.setFirstDayOfWeek(Calendar.SUNDAY);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
int lastDateOfWeek = calendar.get(Calendar.DATE);
In this way, you can get eh first and the last date for the week. one thing to keep in mind that I have set the first day of the week as SUNDAY. set as per your need. although it is purely optional to set the first day of the week. this gives you a more transparency in code.
I'm trying to get the current day of the week and week of the year like this:
Declaring my variable like this:
Calendar calWeek;
Calendar calDay;
Setting the values like this:
calWeek = Calendar.getInstance();
calWeek.setFirstDayOfWeek(Calendar.MONDAY);
calWeek.set(Calendar.WEEK_OF_YEAR, Calendar.MONDAY);
calDay = Calendar.getInstance();
calDay.setFirstDayOfWeek(Calendar.MONDAY);
calDay.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
int currentWeek = calWeek.get(Calendar.WEEK_OF_YEAR);
int dayInWeek = calDay.get(Calendar.DAY_OF_WEEK);
currentWeek returns 2 and dayInWeek returns 2. The current week is 1 and it's Wednesday. (3rd day of the week)
Even if i change the date on my device, the output is the same, currentWeek returns 2 and dayInWeek returns 2.
Why does it always return the same date?
Best regards,
Dridia
As mentioned in Mike M's comment: you set both values to Calendar.MONDAY which is equals to 2.
Calendar.getInstance() returns an instance with the current date but if you call calendarInstance.get(Calendar.WEEK_OF_YEAR) you still get 2 because according to the docs:
When setting or getting the WEEK_OF_MONTH or WEEK_OF_YEAR fields, Calendar must determine the first week of the month or year as a reference point. The first week of a month or year is defined as the earliest seven day period beginning on getFirstDayOfWeek() and containing at least getMinimalDaysInFirstWeek() days of that month or year....
I just tried it and getMinimalDaysInFirstWeek()returns 1 by default. So you should set this to 7 days. So the first Week will begin on Monday as you expect.
One instance of Calendar is sufficient and your code should look like this:
Calendar calendarInstance = Calendar.getInstance();
calendarInstance.setFirstDayOfWeek(Calendar.MONDAY);
calendarInstance.setMinimalDaysInFirstWeek(7);
int currentWeek = calendarInstance.get(Calendar.WEEK_OF_YEAR);
int dayInWeek = calendarInstance.get(Calendar.DAY_OF_WEEK);
System.out.println("week = " + currentWeek);
System.out.println("day of week = " + dayInWeek);
prints:
week = 1
day of week = 4
P.S. 4 means Wednesday
I want an notification to appear each year depending on the date entered (Birthday). I have everything else working bar how to set a notification annually. As you can see below I have changed the code to say "HERE" where the intervals go. There are intervals for days and I know I could multiply that by 365. But what happens if its a leap year..
int REQUEST_CODE = 7;
Intent intent = new Intent(Activity2.this, Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Activity2.this, REQUEST_CODE, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(am.RTC_WAKEUP, System.currentTimeMillis(), HERE, pendingIntent);
You could replace 'HERE' with a method that determines if the following February from today is in a leap year, and then returns the value 365 or 366 days (in the form of milliseconds mind you) based on those checks.
private long millisUntilNextYear(){
//Set days in a year for Leap and Regular
final int daysInLeapYear = 366;
final int daysInYear = 365;
//Get calendar instance
GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
//Get this year and next year
int thisYear = cal.get(GregorianCalendar.YEAR);
int nextYear = thisYear + 1;
//Get today's month
int thisMonth = cal.get(GregorianCalendar.MONTH);
//Get today's date
int dayOfMonth = cal.get(GregorianCalendar.DAY_OF_MONTH);
//Is today before February? If so then the following February is in THIS year
if (thisMonth < GregorianCalendar.FEBRUARY){
//Check if THIS year is leapYear, and return correct days (converted to millis)
return cal.isLeapYear(thisYear) ? daysToMillis(daysInLeapYear) : daysToMillis(daysInYear);
}
//Is today after February? If so then the following February is NEXT year
else if (thisMonth > GregorianCalendar.FEBRUARY) {
//Check if NEXT year is leapYear, and return correct days (converted to millis)
return cal.isLeapYear(nextYear) ? daysToMillis(daysInLeapYear) : daysToMillis(daysInYear);
}
//Then today must be February.
else {
//Special case: today is February 29
if (dayOfMonth == 29){
return daysToMillis(daysInYear);
} else {
//Check if THIS year is leapYear, and return correct days (converted to millis)
return cal.isLeapYear(thisYear) ? daysToMillis(daysInLeapYear) : daysToMillis(daysInYear);
}
}
}
1) save dates in MM/DD/YY formats.
2) read these dates when you open your app(or at different times)
3) set alerts for single day/today it self.
Plus you can also show birthdays coming in next week/month etc.
I know the Android CalendarView is among the buggiest piece of software in human history, but I must use it so came here asking.
I need to open a CalendarView with:
minimum date set to today;
selected date set to today.
This is how I do it:
int day = 8;
int month = 2;
int year = 2015;
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
CalendarView calendarView = (CalendarView) findViewById(R.id.calendarView);
calendarView.setMinDate(calendar.getTimeInMillis()-2000);
calendarView.setDate(calendar.getTimeInMillis(), true, false);
fixIncredibleBugOfCalendarView(calendarView, calendar);
calendarView.setOnDateChangeListener(this);
When I run the previous code, the CalendarView:
shows the 9th of March as the first selectable day, instead of the 8th (DAY_OF_MONTH starts from 1);
shows the 15th of March as the selected date;
So I have geniously decided to include the two lines marked with *:
int day = 8;
int month = 2;
int year = 2015;
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
CalendarView calendarView = (CalendarView) findViewById(R.id.calendarView);
calendar.add(Calendar.DAY_OF_MONTH, -1); // * - subtract one day, i.e. March 7
calendarView.setMinDate(calendar.getTimeInMillis());
calendar.add(Calendar.DAY_OF_MONTH, 1); // * add one day, back to 8
calendarView.setDate(calendar.getTimeInMillis(), true, false);
fixIncredibleBugOfCalendarView(calendarView, calendar);
calendarView.setOnDateChangeListener(this);
In the first line I remove one day, in the second I add one.
With these two more lines, my CalendarView shows:
the 7th of March, as the first selectable date;
the 15th of March, as the selected date.
The method fixIncredibleBugOfCalendarView(...) should fix something (I found it on SO):
private void fixIncredibleBugOfCalendarView(CalendarView cal, Calendar date) {
// Workaround for CalendarView bug relating to setMinDate():
// https://code.google.com/p/android/issues/detail?id=42750
// Set then reset the date on the calendar so that it properly
// shows today's date. The choice of 24 months is arbitrary.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
date.add(Calendar.MONTH, 24);
cal.setDate(date.getTimeInMillis(), false, true);
date.add(Calendar.MONTH, -24);
cal.setDate(date.getTimeInMillis(), false, true);
}
}
This should not be so hard and I do not think it's a bug: it is more likely that it's my fault. Two questions: Could you please be so kind to tell me how to:
tell CalendarView the minimum date it should display?
have CalendarView showing as selected what I decide is the selected date?
Cheers
This is late, probably you have already solved the thing . But Here is my code to solve it
android.icu.util.Calendar c = android.icu.util.Calendar.getInstance();
Long min = c.getTime().getTime();
Long max = 2629746000L + c.getTime().getTime();
CalendarView cv = (CalendarView) findViewById(R.id.date_picker);
cv.setMinDate(min);
cv.setMaxDate(max);
You can also CalenderView just needs to have mindate in long format. You can also go for java
Data d = new Date();
d.getTime();
But Somehow it doesnt work because of current TimeZone.
Instead Calender returns a dateObject with respect to Current TimeZone set.