I want to show a specific month to user using android CalendarView. Please help me to implement this functionality. I wrote following code but it is not working.
CalendarView calendarView = (CalendarView) findViewById(R.id.calendarView);
Calendar calendar1 = Calendar.getInstance();
calendar1.set(2016, 6, 1);
Calendar calendar2 = Calendar.getInstance();
calendar2.set(2016, 6, 30);
calendarView.setMinDate(calendar1.DATE);
calendarView.setMaxDate(calendar2.DATE);
Following output is coming when i am trying to run the app using above code.
Hint: What is the the value of Calendar.DATE? Documentation says 5, and so when you call this method, you are saying "5 milliseconds past Jan 01, 1970."
setMinDate(long minDate)
Sets the minimal date supported by this CalendarView in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.
Basically, that is a static field, and probably not the value that you want.
Perhaps you want getTimeInMillis() which returns the long for the value that you set the date at?
calendarView.setMinDate(calendar1.getTimeInMillis());
calendarView.setMaxDate(calendar2.getTimeInMillis());
As From my experience i always used Calender.set(Calendar.YEAR, year) method to set year and Calender.set(Calendar.MONTH, month) method to set month and Calender.set(Calendar.DAY,day) method to set day . So i am suggesting you to do same ,
String date = "1/6/2016";
String parts[] = date.split("/");
int day = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]);
int year = Integer.parseInt(parts[2]);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
long milliTime = calendar.getTimeInMillis();
And now set the selected date in the calendar view by doing
mCalendarView.setDate (milliTime, true, true);
Related
I am using realm to save a date but need to use the day and month of the date for later use so am converting the type Date to Calendar using:
public static Calendar toCalendar(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal;
}
Then on onCreate of a fragment:
Date testDate = new Date();
CharSequence s = DateFormat.format("MMMM d, yyyy ", testDate.getTime());
Log.d("Date", String.valueOf(s));
// This prints May 26, 2017
Calendar testCalendar = toCalendar(testDate);
Log.d("CALENDAR DAY TEST", String.valueOf(testCalendar.DAY_OF_MONTH));
// This prints 5, MONTH prints 2. Rather the expected 26 & 5.
The DAY_OF_MONTH and MONTH methods of testCalendar are returning the day as the 5th and month as the 2nd rather than the 26th and 5th.
Those are static ints used for tellling Calendar what type of data you want.
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_MONTH);
I have code to get the start date of week with current day as Sunday. But I want to start my week from Saturday-Friday.
How can I achieve this through android?
This should return date of last saturday and set it as a start of the date.
Calendar cal = Calendar.getInstance();
int i = cal.get(Calendar.DAY_OF_WEEK) - cal.getFirstDayOfWeek();
cal.add(Calendar.DATE, -i - 7);
cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
cal.setFirstDayOfWeek(Calendar.Saturday);
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.Saturday);
i think its help you.
I know the Android CalendarView is among the buggiest piece of software in human history, but I must use it so came here asking.
I need to open a CalendarView with:
minimum date set to today;
selected date set to today.
This is how I do it:
int day = 8;
int month = 2;
int year = 2015;
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
CalendarView calendarView = (CalendarView) findViewById(R.id.calendarView);
calendarView.setMinDate(calendar.getTimeInMillis()-2000);
calendarView.setDate(calendar.getTimeInMillis(), true, false);
fixIncredibleBugOfCalendarView(calendarView, calendar);
calendarView.setOnDateChangeListener(this);
When I run the previous code, the CalendarView:
shows the 9th of March as the first selectable day, instead of the 8th (DAY_OF_MONTH starts from 1);
shows the 15th of March as the selected date;
So I have geniously decided to include the two lines marked with *:
int day = 8;
int month = 2;
int year = 2015;
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
CalendarView calendarView = (CalendarView) findViewById(R.id.calendarView);
calendar.add(Calendar.DAY_OF_MONTH, -1); // * - subtract one day, i.e. March 7
calendarView.setMinDate(calendar.getTimeInMillis());
calendar.add(Calendar.DAY_OF_MONTH, 1); // * add one day, back to 8
calendarView.setDate(calendar.getTimeInMillis(), true, false);
fixIncredibleBugOfCalendarView(calendarView, calendar);
calendarView.setOnDateChangeListener(this);
In the first line I remove one day, in the second I add one.
With these two more lines, my CalendarView shows:
the 7th of March, as the first selectable date;
the 15th of March, as the selected date.
The method fixIncredibleBugOfCalendarView(...) should fix something (I found it on SO):
private void fixIncredibleBugOfCalendarView(CalendarView cal, Calendar date) {
// Workaround for CalendarView bug relating to setMinDate():
// https://code.google.com/p/android/issues/detail?id=42750
// Set then reset the date on the calendar so that it properly
// shows today's date. The choice of 24 months is arbitrary.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
date.add(Calendar.MONTH, 24);
cal.setDate(date.getTimeInMillis(), false, true);
date.add(Calendar.MONTH, -24);
cal.setDate(date.getTimeInMillis(), false, true);
}
}
This should not be so hard and I do not think it's a bug: it is more likely that it's my fault. Two questions: Could you please be so kind to tell me how to:
tell CalendarView the minimum date it should display?
have CalendarView showing as selected what I decide is the selected date?
Cheers
This is late, probably you have already solved the thing . But Here is my code to solve it
android.icu.util.Calendar c = android.icu.util.Calendar.getInstance();
Long min = c.getTime().getTime();
Long max = 2629746000L + c.getTime().getTime();
CalendarView cv = (CalendarView) findViewById(R.id.date_picker);
cv.setMinDate(min);
cv.setMaxDate(max);
You can also CalenderView just needs to have mindate in long format. You can also go for java
Data d = new Date();
d.getTime();
But Somehow it doesnt work because of current TimeZone.
Instead Calender returns a dateObject with respect to Current TimeZone set.
I want to deal with date and time separately in my android app, by saying separately I mean setting the time without affecting the date and vice versa, So, when setting the time only:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR + 1990, Calendar.MONTH, Calendar.DAY_OF_MONTH, hours, minutes, 0);
and it works perfectly, but when setting the Date only:
Calendar calendar = Calendar.getInstance();
calendar.setTime(new_date);
I face a problem of having the time set to 00:00:00, So, I tried to save the time values before and set the date the following way:
Calendar calendar = Calendar.getInstance();
calendar.setTime(new_date);
calendar.set(Calendar.YEAR + 1990, Calendar.MONTH, Calendar.DAY_OF_MONTH, saved_hours, saved_minutes, 0);
But that makes the date saved as it's default value as (Calendar.YEAR||.MONTH||.DAY_OF_MONTH) returns the zero date values of the device which is a date in 1990's despite I'm excuting this command after setting the new Date.
Hint: Methods (mDate.getYear(), mDate.getMonth, .....etc) are deprecated.
To set date and time separately you can use this code
Calendar cal = Calendar.getInstance();
// Note: Months value is MonthNumber-1 (Jan is 0, Feb is 1 and so on).
cal.set(Calendar.MONTH, 9);
cal.set(Calendar.DATE, 24);
cal.set(Calendar.YEAR, 2013);
cal.set(Calendar.HOUR,1); // when wanting to set it as 12 Hour system
cal.set(Calendar.HOUR_OF_DAY,13); // when wanting to set it as 24 Hour system
cal.set(Calendar.MINUTE,45);
cal.set(Calendar.SECOND,52);
// set the year,month and day to something else
//set(int year,int month,int day)
cal.set(1995, 5, 25);
// set(int year,int month,int day,int hourOfDay, int minute,int second)
cal.set(1995, 5, 25, 04, 15, 20);
I'm developing a simple app that the user pick some date on DatePicker and return the current day name in some Toast message.
but when I'm trying to do that it has given me a wrong date.
my code:
final int day = datepicker.getDayOfMonth();
datepicker.getDrawableState();
int mounth = datepicker.getMonth();
mounth+=1;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, day);
String formatDay = new SimpleDateFormat("E").format(cal.getTime());
Toast.makeText(MainActivity.this, formatDay, 2000).show();
You're setting the day of month when you create the Calendar, but you aren't setting the month or year. You need to getMonth() and getYear() of the DatePicker and set those on the Calendar also. Do GregorianCalendar cal = new GregorianCalendar(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), 0, 0) instead of Calendar.getInstance(). With getInstance() you are getting a Calendar for the current time, then just changing the day.