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
Related
I want to show calendarView that will set the minimum date as today for booking purposes. But in my code it will show the whole month and here I can book the previous date. So, I need a solution that will show dates available from today and can not select previous days.
How can I do this?
My code:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DATE,Calendar.getInstance().getActualMinimum(Calendar.DATE));
long date = calendar.getTime().getTime();
calendar_view.setMinDate(date);
Here is a solution. You need to set current date to the calendar minimum date.
Calendar calendar = Calendar.getInstance();
mCalendarView.setMinDate(calendar.getTimeInMillis());
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.
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.
I want DatePicker shows a specific date when opened, unfortunately seems I can set only the date limits with setMinDate and setMaxDate.
Is there a way to set a date without block the selection with date limits?
DatePicker date = new DatePicker(context);
date.init(year, month, day, null);
where year, month and day are ints. Here is an explanation about init
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?