I have next code:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, mYear); /2011
cal.add(Calendar.MONTH, mMonth); /04
cal.add(Calendar.DAY_OF_MONTH, mDay);/13
cal.add(Calendar.HOUR, mHour); /11
cal.add(Calendar.MINUTE, mMinute); /53
System.out.println("Cal time "+ cal.getTimeInMillis());
System.out.println("System time " +System.currentTimeMillis());
cal time 64775494376227
System time 1302724616231
What's wrong in my code?
The problem is Calendar.getInstance() is already initialized to the current date/time. You're returning the millis time for for sometime in 4022...not 2011. See the JavaDoc for more info.
Related
I want to pick a time from time picker and set an alarm using AlarmManager for that time.
When I use when.getTimeInMillis the value returned is different from the time I´ve set.
For example , if I set the time at 20:40 and I print the value of when.getTime I get 17:12 from next day.
Why is this happening?
Getting time from time picker
MedicationReminder mr=new MedicationReminder(getApplicationContext());
int hour = tp.getCurrentHour();
int min = tp.getCurrentMinute();
Calendar c=Calendar.getInstance();
c.add(Calendar.HOUR, hour);
c.add(Calendar.MINUTE, min);
mr.setReminder(new Medication(name,quant_aux,time),c);
Setting alarm
Intent i = new Intent(mContext, MedicationReceiver.class);
i.putExtra("medName",medication.getName());
i.putExtra("medQuant",medication.getQuantity());
PendingIntent pi=PendingIntent.getBroadcast(mContext,0,i,0);
mAlarmManager.set(AlarmManager.RTC_WAKEUP,when.getTimeInMillis(),pi);
Instead of
Calendar c=Calendar.getInstance();
c.add(Calendar.HOUR, hour);
c.add(Calendar.MINUTE, min);
Try doing
Calendar c=Calendar.getInstance();
c.set(Calendar.HOUR, hour);
c.set(Calendar.MINUTE, min);
If you want to use 24h hour format, then use
c.set(Calendar.HOUR_OF_DAY, hour);
I'm not 100% that's the problem since you didn't include all the code, but I think you're adding 20 hours to the current time instead of setting the calendar to hour 20.
Another note: I believe getCurrentHour() and getCurrentMinute() have been deprecated since API 23, so I suggest you use getHour() and getMinute() instead.
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);
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'm using a datepicker and want to set the min date to today and the max date to today one year ahead.
I do this like:
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
cal.add(Calendar.YEAR, 1);
datePickerDialog.getDatePicker().setMaxDate(cal.getTimeInMillis());
When I don't do - 1000 then I get another exception:
java.lang.IllegalArgumentException: fromDate: Sat Apr 11 23:59:59 CEST 2015 does not precede toDate: Sat Apr 11 08:24:19 CEST 2015
thats because the date may not be equal to today. So I extract 1000 ms.
I don't know how to solve the new exception. I tried to count + 1000 ms on the maxDate but that didn't solve it.
EDIT:
I create my cal like this:
cal = Calendar.getInstance();
datePickerDialog = new DatePickerDialog(getActivity(), this, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE));
I solved the problem as follows:
cal.set(Calendar.HOUR_OF_DAY, cal.getMinimum(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE));
cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND));
datePickerDialog.getDatePicker().setMinDate(cal.getTimeInMillis());
cal.add(Calendar.YEAR, 1);
cal.set(Calendar.HOUR_OF_DAY, cal.getMaximum(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));
cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND));
datePickerDialog.getDatePicker().setMaxDate(cal.getTimeInMillis());
I just set the start date to the minimum and the end date to the maximum off that day.
Looks like MinDate is higher than MaxDate. As per the exception.
Try this:
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
datePickerDialog.getDatePicker().setMinDate(cal.getTimeInMillis());
cal.add(Calendar.YEAR, 1);
datePickerDialog.getDatePicker().setMaxDate(cal.getTimeInMillis());
Thanks for sharing your solutions. I got a similar problem in one of my android apps. Also I want to limit the datepicker calendar range to show a minimum date 100 years ago and a maximum date of 5 years ago. Based on the codes that you shared, the following is my solution:
Calendar calmin = Calendar.getInstance();
Calendar calmax = Calendar.getInstance();
calmin.add(Calendar.YEAR, -100);
calmin.set(Calendar.HOUR_OF_DAY, calmin .getMinimum(Calendar.HOUR_OF_DAY));
calmin.set(Calendar.MINUTE, calmin .getMinimum(Calendar.MINUTE));
calmin.set(Calendar.SECOND, calmin .getMinimum(Calendar.SECOND));
calmin.set(Calendar.MILLISECOND, calmin.getMinimum(Calendar.MILLISECOND));
calmax.add(Calendar.YEAR, -5);
calmax.set(Calendar.HOUR_OF_DAY, calmax.getMaximum(Calendar.HOUR_OF_DAY));
calmax.set(Calendar.MINUTE, calmax.getMaximum(Calendar.MINUTE));
calmax.set(Calendar.SECOND, calmax.getMaximum(Calendar.SECOND));
calmax.set(Calendar.MILLISECOND, calmax.getMaximum(Calendar.MILLISECOND));
DatePickerDialog datePickerDialog = null;
datePickerDialog = new DatePickerDialog(ageGateActivity.this,
android.R.style.Theme_Holo_Light_Dialog_MinWidth, dateSetListener, year,
month, day);
datePickerDialog.getDatePicker().setMinDate(calmin.getTimeInMillis());
datePickerDialog.getDatePicker().setMaxDate(calmax.getTimeInMillis());
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();