I want the user to be able to pick start date and end date. The minimum of end date must be equal to start date. For example the user selects August 1 2014 as a start date. When he clicks on the return date field these values are passed to date_picker_activity as intent extras. In the date_picker_activity I check for these extras and currently it just sets the selected date as the displayed date:
if(getIntent().getExtras()!=null)
{
int year = getIntent().getExtras().getInt("SELECTED_YEAR");
int month = getIntent().getExtras().getInt("SELECTED_MONTH");
int day = getIntent().getExtras().getInt("SELECTED_DAY");
date_picker.updateDate(year, month, day);
}
but the user is still able to pick dates prior to selected start date.
I tried to do something like that inside that if statement:
Time time = new Time();
time.set(day, month, year);
date_picker.setMinDate(time.toMillis(true)-1000);
but that didn't work.
How can I set the min date with the passed values?
After discussing on chat, we managed to understand that this code:
date_picker.setMinDate(System.currentTimeMillis() - 1000);
was being called anyway inside the if statement, setting the minimum date to the current Date.
Once Igal moved that code to an else statement, all is working ok.
Related
I have some doubts about how it works the Calendar library in Android.
I have implemented a listener to get the selected date in the MaterialDatePicker. With the method addOnPositiveButtonClickListener, I get the date selected in milliseconds but I need to transform these milliseconds to a Date object, BUT! whenever I transform this millisecond to a Date object, I am not getting the right value.
For example, if I select 03/24/2021, when I create a new Date object with the correspondent millisecond I get the next Date -> 03/23/2021.
I am trying different solutions I find in different posts but no one works fine for me.
I am trying to use Calendar but is not working properly either.
fun Long.toDate() : Date {
val calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
calendar.timeInMillis = this // calendar.time.time = this
return calendar.time
}
and also try it out with different timeZone
fun Long.toDate() : Date {
val calendar = Calendar.getInstance(TimeZone.getDefault())
calendar.timeInMillis = this // calendar.time.time = this
return calendar.time
}
I have also try to save the millisecond on my Calendar instance with calendar.time.time but also is not working for me.
It is so curious because if I set the next values on my device:
Time zone in New Zealand (GMT+13:45) at 10:00 AM,
I select 03/02/2021, so should return the same date.
For this specific example is working fine (I am in Madrid, Spain GMT+01:00) and is returning the date 03/02/2021.
If it was working bad, should returns 02/02/2021, but for this timeZone is working fine.
But, if I change my device values to:
Time zone to Colombia (GMT-05:00) and I set my device time to 10:00 PM.
I select the same date, 03/02/2021, it should return the same date.
In this case, is returning me the day before, 03/01/2021.
Why MaterialDatePicker is working so bad and how can I fix that without doing so badly tricky code?
I saw it can be fixed used SimpleDateformat, but I would like to keep the timeZone and do it a good way, or at least the best way...
Thanks in advance!
https://github.com/kizitonwose/CalendarView
I have been using this library for setting up an horizontal calendar view. In this library they set Date range like this:-
val currentMonth = YearMonth.now()
binding.mainSingleRowCalendar.setup(
currentMonth,
currentMonth.plusMonths(2),
DayOfWeek.values().random()
)
Here the month is used to set the date range. i need it to be a specific date. Is there any work around?
My actual requirement is to show past 30 days from current date.
of(int year, int month) method
Obtains an instance of YearMonth from a year and month.
YearMonth.of(2021,9) // Sample
I have two DatePickerDialogs, one is for start date and one for end date. I need that every time the user open the end date Dialog the min date will be the start date dialog's date. Notice that the start date is not constant and the user can change it anytime.
If i understood correctly. You should do something like this:
Get your start date Calendar instance.
Set your end date minimum date from start date Calendar instance.
datePickerDialog.getDatePicker().setMinDate(startDateCalendar.getTimeInMillis());
i have a monthdisplayhelper to display the month, i want to select days between two date. i.e.,if i selected 2nd July and 9th July, all the dates(3rd to 8th) in between these dates must be selected or highlighted.
You can use the Calendar class. Initialise a new Calendar object for your initial date, then use [calendar instance].add(Calendar.DAY_OF_YEAR, 1) to increment the date by 1 day, it'll automatically roll to the next month if required.
Calendar reference: http://developer.android.com/reference/java/util/Calendar.html
If you know how many days you will need then you could have a loop basically add days and store in a list
When trying to get a string for the current date using
DateFormat.getDateInstance().format(calendar.getTime())
it keeps returning the wrong day. For example, it is saying today, July 25th., is July 26th. Also when I use it to sat a date picker, I get the day value by using
dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
When the date picker is set, it also shows the day ahead by 1.
To get the calendar I'm using
Calendar calendar = Calendar.getInstance();
Is there something I'm missing?
Thanks
I would imagine this is because you havent set the timezone to your timezone, and rather than the day being off randomly, the time zone you are in is diferent than GMT (Greenwich Median? Time). Try looking at this example How to handle calendar TimeZones using Java?