I'm trying to implement a datepicker but I need to disable some days. For example, I do not want the user to pick Friday and Saturday. I read in other posts that I need to use this library, but I still do not know how it works and how to do it in Kotlin. Someone help me plz.
In the library documentation there is mentioned that how to use select able days and how to disable certain days. For both purpose you have to pass the array of days to the respective methods.
For example if you want to select certain days only you have to pass the array of days to the method. To create the days arraylist use following code
Calendar[] days = new Calendar[13];
for (int i = -6; i < 7; i++) {
Calendar day = Calendar.getInstance();
day.add(Calendar.DAY_OF_MONTH, i * 2);
days[i + 6] = day;
}
Now after initializing the datpicker dialog call the method to select the days or disable days.
DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(MainActivity.this,
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH));
datePickerDialog.setDisabledDays(days)
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.
How to get all weeks of month with date,I am doing like this
calendar.add(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() - calendar.get(Calendar.DAY_OF_WEEK));
String[] weekly = new String[7];
Arrays.fill(weekly, "");
int today = calendar.getInstance().get(Calendar.DAY_OF_WEEK);
for (int i = 0; i < 7; i++) {
Date dt = calendar.getTime ();
// now format it using SimpleDateFormat
DateFormat df = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
String val = df.format (dt);
weekly[i] = val;
calendar.add (Calendar.DAY_OF_WEEK, 2);
Log.d("valueweek",""+weekly[i]);
}
output
26-11-2017
27-11-2017
28-11-2017
29-11-2017
30-11-2017
01-12-2017
02-12-2017
But i also want all previous weeks of this month
java.time
First, do consider to drop the long outmoded classes Calendar, Date and DateFormat. Today we have so much better in java.time, the modern Java date and time API also known as JSR-310. On Android too, I will return to that. The modern API is so much nicer to work with.
If I understand your request correctly, this should give you what you want:
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-uuuu");
WeekFields localWeekFields = WeekFields.of(Locale.getDefault());
LocalDate today = LocalDate.now(ZoneId.of("Europe/Helsinki"));
Month thisMonth = today.getMonth();
LocalDate weekStart = today.withDayOfMonth(1)
.with(TemporalAdjusters.previousOrSame(localWeekFields.getFirstDayOfWeek()));
// Iterate over weeks
do {
System.out.println("Here’s a week:");
// Iterate over days in week
LocalDate day = weekStart;
for (int i = 0; i < 7; i++) {
System.out.println(day.format(dateFormatter));
day = day.plusDays(1);
}
weekStart = weekStart.plusWeeks(1);
} while (weekStart.getMonth().equals(thisMonth));
Running this snippet on my computer today it prints the days of 5 weeks, so I will only show you the first and the last lines of output:
Here’s a week:
30-10-2017
31-10-2017
01-11-2017
02-11-2017
…
Here’s a week:
27-11-2017
28-11-2017
29-11-2017
30-11-2017
01-12-2017
02-12-2017
03-12-2017
Since in my locale the week starts on Monday, the above weeks go from Monday to Sunday. If you run the same code in a locale where Sunday is the first day of the week, they should go from Sunday to Saturday.
A little explanation: WeekFields.of(Locale.getDefault()) gives us a WeekFields object of the current locale. We use this a few lines down to determine the first day of the week. Then we query today’s date in your desired time zone — please fill the desired time zone in if you don’t want Europe/Helsinki. To iterate over the weeks, we initialize a LocalDate to the first day of the first week by first finding the first day of this month and then going back to the first day of the week (possibly going into the previous month). To determine which is the first day of the week, we query the WeekFields object that we got a few lines earlier as the one belonging to the current locale (you can fill in a different locale if desired, or just a different WeekFields object).
In the loop over weeks we first print the week and then add one week to weekStart, so we’re ready for next week. The loop condition is that we’re within the current month. To make sure the loop makes its first iteration even if we started on one of the last days of the previous month, I use a do-while loop rather than a while loop.
Will this work on Android?
You certainly can use the modern API on Android too. You need to get ThreeTenABP, the backport of the modern API to Android (that’s ThreeTen for JSR-310 and ABP for Android Backport). It’s all well and thoroughly explained in this question: How to use ThreeTenABP in Android Project.
Months including the current month and the months to come from the current year should be displayed.
Lets try to answer:
In Android there is a class named Calendar. This is probably the best way to work with dates.
this is the documentation
You can istantiate a new istance by using
Calendar calendar = Calendar.getIstance();
This gives you a calendar with the info of the exact moment you istantiated it.
now, using
calendar.MONTH
You are getting the int of the current month.
All months are static values inside the Calendar class.
Calendar.JANUARY = 0
Calendar.FEBRUARY = 1
and so on...
So an array is a static thing:
String[] months = new String[12]();
months[0] = january;
....
or
String[] months = new String[]{"january", ...};
or
int[] months = new int[]{1,...};
If you want a list of months with checkboxes, please have a look at how to use listview adapters
Hope this helps.
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 :)
I am trying to create a DatePickerDialog when a certain TextView is clicked, and then set the date picked to be that TextView. I have gotten this to work, but when the DatePickerDialog is shown, the dates for left DatePicker are correctly set to the current date, while the calendar on the right is set to November 2100. How can I access the field of calendar and set its date?
Here is a link to an image of what I am talking about, but is not from my application exactly.
http://i1023.photobucket.com/albums/af358/shaikhhamadali/typesofdialog_4_zps078711ac.png
So, I am talking about the calendar on the right that does not start on the current date. I would like to know how to access it? If I'm not mistaken, this calendar will only show on tablets, so is there a "safe" way to do this where running it on a phone would not cause any problems?
Here is the code from my DatePickerFragment innerclass onCreateDialog method
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this, year, month, day);
datePickerDialog.setTitle("Enter date");
datePickerDialog.getDatePicker().setMinDate(Calendar.getInstance().getTimeInMillis() / 86400000L * 86400000L);
return datePickerDialog;
}
Thank you for any help!
UPDATE:
Update: I have tried adding accessing the CalendarView through both, but the CalendarView still starts up at Nov 2100
datePickerDialog.getDatePicker().getCalendarView().setDate(Calendar.getInstance().getTimeInMillis() / 86400000L *86400000L);
datePickerDialog.getDatePicker().getCalendarView().setMinDate(Calendar.getInstance().getTimeInMillis() / 86400000L 86400000L);
Solution
This long answer (to a question about practically the same bug) suggests a workaround for this issue.
Part of it quoted here:
formatDateRange() does not work past 2038.
Workaround #1
class DatePickerDialog1964 extends DatePickerDialog {
DatePickerDialog1964(Context c) {
super(c, null, 2013, 4, 21);
#SuppressWarnings("deprecation")
Date min = new Date(2013-1900, 4, 21);
DatePicker p = getDatePicker();
CalendarView cv = p.getCalendarView(); // should check for null
long cur = cv.getDate();
int d = cv.getFirstDayOfWeek();
p.setMinDate(min.getTime());
cv.setDate(cur + 1000L*60*60*24*40);
cv.setFirstDayOfWeek((d + 1) % 7);
cv.setDate(cur);
cv.setFirstDayOfWeek(d);
}
}
Workaround #2 I actually used
// Calendar view is a cascade of bugs.
// Work around that by explicitly disabling it.
datePicker.setCalendarViewShown(false);
My Deduction
By elimination, we can deduce that the line datePickerDialog.getDatePicker().setMinDate(Calendar.getInstance().getTimeInMillis() / 86400000L * 86400000L); is causing the November 2100 error.
(UNIX time / 86400000L) * 86400000L --I guess it's supposed to take advantage of rounding to produce a 'full' day. Beware of it rounding to the same day if the current time is after noon, the previous day if before. It isn't chopping after the floating point.
About the updated part:
Even calling setMinDate then setDate isn't really solving anything -- The problem that caused the Nov 2100 error will persist, because the updated line's effect is executed by the original code.
Then I looked around, and I found the above linked answer.
I guess CalendarView is stuffed with bugs.
There appears to be another NumberPicker bug where the previous month and date and shown sometimes, even if not accessible. They disappear when we try to access them.
P.S: I know this is late, but since the OP didn't provide any solution, an analysis might be helpful to somebody.