Android: How to set first day of week in DateRangePicker? - android

How to set first day of week to monday or saturday ?
I can't find method or variable to change it. Like picture example below, first day of week is monday. Thanks in advance.
link github DateRangePicker https://github.com/savvisingh/DateRangePicker
Thanks for the answer. I finally figure it out. You need to initialize Locale and Timezone to change first day of week. Not calendar that you have to change. Don't need to break the library code too.
new CalendarPickerView.init(date1, date1, TimeZone.getDefault(), Locale.UK, new SimpleDateFormat("MMMM, YYYY", Locale.getDefault())) //
.inMode(CalendarPickerView.SelectionMode.MULTIPLE)
.withSelectedDates(listDate);

The first day of week is determined by your locale.
Set it to something like English (UK) or German and you will have Monday as first day of week.
Then if it dosen't work you can change by code, like :
datePickerDialog.setFirstDayOfWeek(int weekStart);
If you want monday weekStart = 2
I hope it will help you!

As per you using the DateRangePicker library :
For Example:
Use this code in your project.
// create a calendar
Calendar cal = Calendar.getInstance();
// set first day of the week as something else
cal.setFirstDayOfWeek(Calendar.WEDNESDAY);
or it will not run then required for you to change in library code with create method and put above code then used it in your project.

I have looked at that repository. The library creates days from 0 to 7 in a loop and gets days. See this.
The calendar here in use is created in init method in CalendarPickerView. Look at CalendarPickerView.
today = Calendar.getInstance(timeZone, locale);
I think if you change first day of week of calendar or default locale/timezone, you can do what you want.

Related

How get the first day of a week index in Flutter

I try to get the first day of the week index on a flutter app by using :
var firstDayOfWeekIndex =
MaterialLocalizations.of(context).firstDayOfWeekIndex;
It's work done to get the first day of the week based on Language and region settings but not with the calendar phone settings.
For exemple, on locale "en_US" first day of week is sunday, but my phone settings calendar is monday. The previous code get Sunday not monday.
How can achieve this ? (I would like a compatibility iOS/Android) ?
Take a look at this answer :
enter link description here
In few words, make this:
DateTime findFirstDateOfTheWeek(DateTime dateTime) {
return dateTime.subtract(Duration(days: dateTime.weekday - 1)); }

android calendar change with device language

I'm stuck with one interesting problem.
I have a Calendar object with European style(? don't know how to call it right) i.e. week starts on Monday and ends on Sunday.
My issue appears when I change device language to English(U.S.). Calendar object changes its style to American i.e. week starts on Sunday and ends on Saturday.
Unfortunately I need week to start on Monday and end on Sunday but I can't understand how to do that.
I tried
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar = Calendar.getInstance(Locale.FRANCE);
but it doesn't work.
Thanks in advance!

calendarview highlights wrong week

I have a CalendarView in my app, when the user selects a date by touching that date in the monthview, the correct date is selected (verified by adding debug statements in the code), but the week before is highlighted, so it looks as if the wrong date is selected.
I have found a work-around: if I set 'firstDayInWeek' to 1 the problem is solved, but by default the firstDayInweek is 2 (monday), and then this problem occurs.
Thank you very much!
Samsung S4 with API 21
I have had the same issue as you, using a Samsung S5 running API 21.
There are two workarounds that I have found, none of them is a good experience for our users :(
Force first day of the week to Sunday
calendarView.setFirstDayOfTheWeek(Calendar.SUNDAY);
Set a minimum and a maximum date for the calendar (be careful because not all of the dates work here). I was able to make it work properly setting a minimum date 2 months before the current date and maximum date 2 years after the current date. You can play with these values and find a good compromise between the limits and your user experience.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 2);
calendarView.setMinDate(calendar.getTimeInMillis());
calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + 2);
calendarView.setMaxDate(calendar.getTimeInMillis());
Unfortunately, this is the only way I could fix this issue, I hope it is useful for you.

How do I set the date for a date picker?

I use DatePickerDialog.OnDateSetListener that's works fine.
I want to add date for 120 days in date picker.
What I mean is if I add 120 days, the date and month will be change automatically.
How to do it?
Something like this should do the trick:
Calendar cal = Calendar.getInstance();
cal.set(datepick.getYear(), datepick.getMonth() + 1, datepick.getDayOfMonth());
cal.add(Calendar.DATE, 120);
datepick.updateDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) - 1, cal.get(Calendar.DATE));
Make sure you create a date object with 120 days added (see this topic on how to do that) and use that to populate your datepicker, either on initialization or when changed. I'm not really sure what you are trying to achieve however the latter doesn't seem right usability wise. In that case I would create an extra textfield that represents the +120 days date.
Set Date Programmatically by using UpdateDate
datePickerDialog.UpdateDate(selectedDate ?? DateTime.Now);

Get day of week when using DatePickerDialog

I'm working on a kind of tricky app where the day of the week is important.
To avoid to much hassle, here is my main problem:
I have a DatePickerDialog where the user selects a date. I want to know what day of the week this date is.
I'm thinking this should be easy, but somehow I can't figure it out.
I've tried a few things like using:
Calendar.DAY_OF_WEEK - I can only get it to return todays day
Date.parse("1.16.2012").toString("dddd") - Didn't get this to work at all
I really don't want to create a whole new calendar just for this simple problem.
Anyone good ideas guys?
Very quickly, the date picker gives you year, month and day:
dayOfWeek = DateFormat.format("EE", new Date(year, month, day)).toString();
Iron the bugs I probably left yourself, sorry =)
Best regards.

Categories

Resources