How do I set the date for a date picker? - android

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

Related

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

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.

xamarin android DatePickerDialog set mini date

I'm using DatePickerDialog and i want to hide every date before today.
I can hide previous month and year using this code:
dialog.DatePicker.MinDate = new Java.Util.Date().Time - 1000;
But it's not working like i want. Days between 1st of june and today are disabled (gray) but we still can click on them and dismiss the dialog by clicking on the positive button.
Does someone has a solution to realy disabled them ? or at least avoid dismiss the dialog?
You need to give MinDate the number of milliseconds between your minimum date (in your case today) and January 1st 1970, so:
dialog.DatePicker.MinDate = (long)(DateTime.Today.Date - new DateTime(1970, 1, 1)).TotalMilliseconds;
This should prevent users from selecting any earlier dates.
Try changing the mindate to date now?
dateTimePicker1.MinDate = DateTime.Now;
Something like that should work. :)
//for lesser than today
dialog.DatePicker.MinDate = Java.Lang.JavaSystem.CurrentTimeMillis();
//for greater than today
dialog.DatePicker.MaxDate = Java.Lang.JavaSystem.CurrentTimeMillis();
This solved mine :)

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.

Compare current date with stored one

For an easteregg in my Android app, I have to compare the current date with a stored date - and I only need to know if it's the right month.
I know that System.currentTimeMillis() is the fastest way to get the current time but now I need to get the current month from that. I avoided String comparison for it's known flaws.
My awful implementation works but it really doesn't look correct and efficient:
if (Integer.parseInt((String) DateFormat.format("MM",System.currentTimeMillis()))==12) //xmas "easteregg"
xmasBool=true;
Is there any more elegant solution for this?
Here's a better solution:
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date()); // Date's default constructor gives you current time
xmasBool = calendar.get(Calendar.MONTH) == Calendar.DECEMBER;
Calendar c = Calendar.getInstance();
int month = c.get(Calendar.MONTH);
And now you can compare the variable month with your stored month.
You can compare a day or a month or both, the whole date by formatting java.Util.Date using SimpleDateFormat.
Eg.
new SimpleDateFormat("dd").format(new java.util.Date())
gives you the "day" value. Similarly "MM" will give you the month. Use combination of those as per your requirement.
Store it in the same format and you have a common standard for comparison.

calendar.set(Calendar.HOUR_OF_DAY, alarmHour); is not working. What am I doing wrong?

I am setting an alarm for which I take the Hour and Minutes from a TextView and the AM/PM through a Spinner. Here is how I initialize the Calendar object:
Calendar calen = Calendar.getInstance();
calen.set(Calendar.HOUR_OF_DAY, alarmHour); //alarmHour from TextView
calen.set(Calendar.MINUTE, alarmMinute); //alarmMinute from TextView
calen.set(Calendar.SECOND, 0);
calen.set(Calendar.MILLISECOND, 0);
if(amorpm.equals("PM") //amorpm from Spinner
{
calen.set(Calendar.AM_PM, Calendar.PM);
}
else
{
calen.set(Calendar.AM_PM, Calendar.AM);
}
The problem is the Hour value of this Calendar object is sometimes correct i.e. the value which the user enters in the TextView (and it is always 1 to 12). But sometimes, the value is equal to the current Hour. For example, if the current time is 11:30 pm, and I set the alarm for 9:30 am, then the Hour field has the value 11. One strange thing is that when I change the name of the Calendar object to something else, say cal, it works. But wont work later. What could be wrong ?
Thanks for your help!
I think the solution is to call calen.set(Calendar.HOUR, alarmHour); instead of calen.set(Calendar.HOUR_OF_DAY, alarmHour);
The trouble is you are setting the Calendar.HOUR_OF_DAY field and then setting the AM/PM field. "Hour of day" is hour in the full 24-hour day, and therefore using it in conjuction with AM/PM doesn't make sense. According to the Java documentation (http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html), when setting the time of day, if conflicting information is presented, then the most recently set information will be used according to the following combinations:
HOUR_OF_DAY
AM_PM + HOUR
Because you are setting the AM/PM field after the hour, the hour is taken from the Calendar.HOUR field, which is of course initialised to the current hour when you create the instance.
The naming of the variable is, of course, a red herring. That can't possibly affect it.

Categories

Resources