How to subtract two calendar object in Android - android

Say for example I have two calendar object with the format of "MM/dd/yyyy". How do I subtract them and should return the number of days? For example, "9/7/2014" - "9/1/2014" and returns 7.
Calendar calendar = Calendar.getInstance();
Calendar today = Calendar.getInstance();

You may try like this:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH,7);
calendar.set(Calendar.MONTH,8);
calendar.set(Calendar.YEAR, 2014);
Calendar today = Calendar.getInstance();
long difference = today.getTimeInMillis() - calendar.getTimeInMillis();
int days = (int) (difference/ (1000*60*60*24));

Related

How to show a specific month in android CalendarView?

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

Android DatePicker- GregorianCalendar - 00:00:00 UTC - getTimeInMillis()

How do I set the Time of the GregorianCalendar to 00:00:00 UTC?
Because the following returns a Date at 10:00pm:
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar cal = Calendar.getInstance(timeZone);
cal = new GregorianCalendar(year, monthOfYear, dayOfMonth ,0 ,0, 0);
editor.putString("auswahldatum", String.valueOf(cal.getTimeInMillis() / 1000))
.apply();
Log.i("Kalender", String.valueOf(cal.getTimeInMillis()));
Look at Calendar's Calendar.getInstance(timeZone) method it already return of new object of gregorian calendar. You can create it for example like this one:
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(timezone);
Or
Calendar cal = new GregorianCalendar(year, monthOfYear, dayOfMonth ,0 ,0, 0);
cal.setTime(date);
cal.setTimeZone(timezone);
Actually you must set time zone after time setting

setMaxDate to DatePickerDialog

Hi I implemented a DatePickerDialog but I want to set range, I found setMinDate and setMaxDate, but I don't know how to put values. I want to set the calendar for a user of min age 18 and max age 100; I think it's about calculations into milliseconds.
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 dpd = new DatePickerDialog(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT, this, year, month, day);
dpd.getDatePicker().setMaxDate(max);
return dpd;
I don't know what value I have to put to max to do 2016 - 18.
Use this code, to set maximum date and minimum date from current date.
Calendar minCal = Calendar.getInstance();
minCal.set(Calendar.YEAR, minCal.get(Calendar.YEAR) - 100);
Calendar maxCal = Calendar.getInstance();
maxCal.set(Calendar.YEAR, maxCal.get(Calendar.YEAR) - 18);
dialog.getDatePicker().setMinDate(minCal.getTimeInMillis());
dialog.getDatePicker().setMaxDate(maxCal.getTimeInMillis());
I guess this is what you need:
final Calendar maxc = Calendar.getInstance();
int year = maxc.get(Calendar.YEAR);
maxc.set(Calendar.YEAR,year-18);
datePickerDialog.getDatePicker().setMaxDate(maxc.getTimeInMillis());
final Calendar minc = Calendar.getInstance();
int year = minc.get(Calendar.YEAR);
minc.set(Calendar.YEAR,year-100);
datePickerDialog.getDatePicker().setMinDate(minc.getTimeInMillis());
datePickerDialog.show();

Android Calendar : Changing the start day of week to Saturday

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.

How to get date & time in 13 digit(epoch time) from day,month,year,hours and minutes?

I am working on an application in which I have got date and time from similar dialog.
I have all this variable of type integer.
Now I have to convert this all variables in to the epoch time.
It is very important for me.
Is there any way to do this?
You can use Calendar Class, you must instantiate a new Calendar Object, set MONTH,DAY,YEAR,HOUR,MINUTE and SECOND and then use getTime() method.
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.HOUR, hours);
cal.set(Calendar.MINUTE, minute);
int epoch_time = (int) cal.getTimeInMillis()/1000;
Use Calendar:
Calendar c = Calendar.getInstance();
c.set(year, month-1, date, h, min, sec);
long t = c.getTimeInMillis();

Categories

Resources