I am trying to set weekly monday reminder in calendar at 9 a.m.
Following is the code
final int[] preTimings = {9, 12, 18};
Calendar calendar = Calendar.getInstance(Locale.getDefault());
ContentValues newEvent = new ContentValues();
int month = calendar.get(Calendar.MONTH);
int date = calendar.get(Calendar.DATE);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DATE, date);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
for(int i = 0; i < preTimings.length; i++{
calendar.set(Calendar.HOUR_OF_DAY, preTimings[i]);
long timeInMillis = calendar.getTimeInMillis();
newEvent.put(CalendarContract.Events.CALENDAR_ID, 1);
newEvent.put(CalendarContract.Events.TITLE, preTitle[i]);
newEvent.put(CalendarContract.Events.DTSTART, timeInMillis);
newEvent.put(CalendarContract.Events.DTEND, timeInMillis + 60000);
newEvent.put(CalendarContract.Events.RRULE, "FREQ=WEEKLY;BYDAY=MO");
newEvent.put(CalendarContract.Events.HAS_ALARM, true);
newEvent.put(CalendarContract.Events.EVENT_TIMEZONE, "GMT-05:30");
So this program is setting the 12 p.m and 6 p.m correctly on Mondays every week but for some reason it is setting the 9 a.m on Tuesdays every week.
I don't know what is wrong over here. I even tried converting the timeInMillis in an online calculator to know what is the result, but they are correct.
So the issue was on the last line, even though i have specifically declared the timezone, it was setting it incorrectly.
So instead of
newEvent.put(CalendarContract.Events.EVENT_TIMEZONE, "GMT-05:30");
It should be
newEvent.put(CalendarContract.Events.EVENT_TIMEZONE, String.valueOf(TimeZone.getTimeZone("UTC")));
Still no idea why it worked for times 11 a.m and above. Kinda weird
Related
I have a string that may contain value 07:00 or 11:00 0r 15:00
I want to set alarm for that time for today. it doesn't seem to work
One option is to create a calendar object set to midnight, and then offset it by the hours and minutes you have:
String alarm = "13:00";
Calendar c = new GregorianCalendar();
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
long millis = c.getTime().getTime();
millis += 1000*60*60*Integer.parseInt(alarm.substring(0, 2));
millis += 1000*60*Integer.parseInt(alarm.substring(3, 5));
Demo
An alternative to this would be to use two SimpleDateFormat masks to parse out the timestamp you want.
Hi I want to get list of times between two time.
Example 10 PM to 6 AM
Currently i am getting 6 AM to 10 PM but it's not working for 10 PM to 6 AM
Code :
List<java.sql.Time> intervals = new ArrayList<>();
Calendar calStart = Calendar.getInstance();
calStart.set(calStart.HOUR_OF_DAY, 22);
calStart.set(calStart.MINUTE, 00);
calStart.set(calStart.AM_PM, Calendar.PM);
java.sql.Time startTime = new java.sql.Time(calStart.getTime().getTime());
Calendar calEnd = Calendar.getInstance();
calEnd.set(calEnd.HOUR_OF_DAY, 6);
calEnd.set(calEnd.MINUTE, 00);
calEnd.set(calEnd.AM_PM, Calendar.AM);
java.sql.Time endTime = new java.sql.Time(calEnd.getTime().getTime());
intervals.add(startTime);
Calendar cal = Calendar.getInstance();
cal.setTime(startTime);
while (cal.getTime().after(endTime)) {
cal.add(Calendar.MINUTE, 15);
intervals.add(new java.sql.Time(cal.getTimeInMillis()));
}
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
for (java.sql.Time time : intervals) {
System.out.println(sdf.format(time));
}
Please give any suggestion
When going from 10PM to 6AM, make sure you add a day as well. Or else you will have 10PM 29th of March until 6AM 29th of March while you want 6AM 30th of March.
calendar.add(Calendar.DATE, 1);
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 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);
This question already has answers here:
Add 1 Week to a Date, which way is preferred?
(10 answers)
Closed 8 years ago.
I have this method that parses a day and time, the problem is I need to get the day of next week.
Example:
I have this day and time Monday, 10:00 PM and the current time is Monday, 11:00 PM,
when I parse this parseTime("M","10:00 PM") it returns me the past date because of the current time.
I want to achieve is the next week's Monday, 10:00 PM
public static Calendar parseTime(String day, String time) {
String[] sepa_time_ampm = time.split(" ");
String[] sepa_time_hr_mn = sepa_time_ampm[0].split(":");
Calendar calendar = Calendar.getInstance();
if (!isNumeric(day)) {
calendar.set(Calendar.DAY_OF_WEEK, parseDay(day));
}
if(Integer.parseInt(sepa_time_hr_mn[0]) == 12) sepa_time_hr_mn[0] = "00";
calendar.set(Calendar.HOUR, Integer.parseInt(sepa_time_hr_mn[0]));
calendar.set(Calendar.MINUTE, Integer.parseInt(sepa_time_hr_mn[1]));
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
if (sepa_time_ampm[1].equals("AM")) {
calendar.set(Calendar.AM_PM, Calendar.AM);
} else {
calendar.set(Calendar.AM_PM, Calendar.PM);
}
if((calendar.getTimeInMillis() - System.currentTimeMillis()) < 0) {
//It is now in the past
}
return calendar;
}
set Calendar to next week. This will set calendar to one week ahead.
Calendar mCalendar = Calendar.getInstance();
int i = mCalendar.get(Calendar.WEEK_OF_MONTH);
mCalendar.set(Calendar.WEEK_OF_MONTH, ++i);
Then find days of next week easily
/** Now get nExt week days **/
int dayvalues=mCalendar.get(Calendar.DAY_OF_WEEK);