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)); }
Related
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.
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!
I am trying to retrieve the number of week in a month.. actually today is the second week in november but I am getting it as 3
The code I am using is
Log.e("dfhkdjfk", Calendar.getInstance().get(Calendar.WEEK_OF_MONTH)+"");
please help me to figure it out.
Reference Time:
3:45 AM
Monday, November 9, 2015
Coordinated Universal Time (UTC)
Wrong.
For some country like France where Monday is the first day of the week, today is really the first day of the third week of November, as the first of november was a Sunday ( so the first week of november had one day... ).
You might use DAY_OF_WEEK_IN_MONTH which give you the number of time a day already happened starting from the beginning of the current month.
Otherwise check which day is considered as the first of the week and implement some logic to adapt it to your need.
You need to try below code with TimeZone which give correct Calendar.WEEK_OF_MONTH
Calendar calUs = Calendar.getInstance(TimeZone.getTimeZone("US/Eastern"), Locale.US);
int weekOfMonthUs = calUs.get(Calendar.WEEK_OF_MONTH);
System.out.println("Week of month is " + weekOfMonthUs);
O/P
Week of month is the week within the current month starting from Sunday to how many weeks have there been.
WEEK_OF_MONTH depends on the first day of the week. Not all calendars have Sunday has beginning of the week. For ex: France has Monday as first day of the week. So Before getting into this check the locale of the phone.
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.
I'm really stuck with a certain problem and I'm hoping someone can help me understand the problem and come to a solution. I've looked online a fair bit but can't see an answer unless it's been staring me in the face :-/
Basically, I'm creating a very basic TV Guide app. It parses data from an RSS feed which has days offset (yesterday was -1. today is 0, tomorrow is 1, etc etc) and I'm trying to implement a DatePicker that allows the user to see what is on a particular channel when they select yesterday, today, tomorrow, etc.. but if they pick a date that is out-with the range (at the moment it's a week in advance), a simple Toast message will be displayed.
My questions I guess are, firstly, how do I use maybe an IF ELSE to either parse the specific channel data for the day the user wants or display an error Toast message, and, how do I go about converting the days from what the user has put in compared to the actual date today into integers? If they select yesterday's date it will go to URL "http://example.com/-1/channel", if they select tomorrow's date it will go to URL "http://example.com/1/channel" etc etc etc.
Code is available if anyone needs to see it, but I think if someone would be kind enough to explain the logic, I'd like to see if I can come to the answer myself...
Thanks a lot folks!!
You should use a DatePicker to allow the user to choose the when.
Time in Android is stored on a long (not an int). And the long time can easily be converted back and forth between long (always milli-seconds) and a Date object.
The Date object gives you all sorts of tools to compare before and after, look at months, minutes, hours, etc.
The current time is determined by:
long nowMs = System.currentTimeMillis();
int nowSec = (int)(nowMs / 1000);
There is also a very important Calendar object. This allows you to parse textual date formats as delivered by your http functions in and out of various dates.
For example:
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss Z");
String text = sdf.format(cal.getTime();
You will have to put all these tools together with a DatePicker example such as the one here Create a DatePicker to complete your TV Guide application.
Reference:
Date
Calendar
DatePicker
EDIT : Check David's Answer its better.
First Filter the date selected with today's date. You can compare it by date.isbefore(date) or date.isafter(date) these booleans will let you tell know if a date is of past or future or present. then to further calculate the days inbetween you can make a method with switch statement that will basically convert the selected date and the current date into miliseconds(Date.getTimeinmiliseconds)
if the date is of past take the difference of present time in miliseconds and past date in miliseconds. If the date is of future do the opposite. Take the difference and convert it to days difference with appropriate sign(negative/positive).
Please refer this link for a better coding example