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);
Related
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
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 only need Today and yesterday date and when other date is selected it should give error....I am getting every thing ok but there is small issue
Here is my code:
if (flag == 0) {
if (getday(myCalendar.getTime()) == 1 || getday(myCalendar.getTime()) == 0) {
setvisitdate();
}
Toast.makeText(getApplicationContext(),
"Selected date is not valid...", Toast.LENGTH_SHORT)
.show();
flag = 0;
new DatePickerDialog(FvrActivity.this, date,
myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
here is method of getday:
private int getday(Date selected_date) {
Calendar selected_cal = Calendar.getInstance();
selected_cal .setTime(selected_date);
Calendar c = Calendar.getInstance();
c.getTime();
long diffInMillisec = c.getTimeInMillis() - selected_cal.getTimeInMillis();
long diffInDays = TimeUnit.MILLISECONDS.toDays(diffInMillisec);
return (int) diffInDays;
}
Suppose today's date is 21/11/2016...I just want to select 21 date and 20 and not 22 but here i can select 22 also..Thank you in advance for solving.
Use this logic:
Calendar selected_cal = Calendar.getInstance();
selected_cal.setTime(selected_date);
//For next day
Calendar nextDay = Calendar.getInstance();
nextDay.add(Calendar.DATE, 1);//Adding one day
nextDay.set(Calendar.HOUR_OF_DAY, 0);//Setting calendar for start hour of the day
nextDay.set(Calendar.MINUTE, 0);//Setting calendar for start minute of the day
nextDay.set(Calendar.SECOND, 0);//Setting calendar for start second of the day
//For previous days
Calendar previousDay = Calendar.getInstance();
previousDay.add(Calendar.DATE, -2);//Adding one day
previousDay.set(Calendar.HOUR_OF_DAY, 23);//Setting calendar for start hour of the day
previousDay.set(Calendar.MINUTE, 59);//Setting calendar for start minute of the day
previousDay.set(Calendar.SECOND, 59);//Setting calendar for start second of the day
if(selected_cal.getTimeInMillis()>=nextDay.getTimeInMillis()
|| selected_cal.getTimeInMillis()<previousDay.getTimeInMillis()){
//Show error
}else{
//Do what you want to do
}
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 do I get 11:59 PM of the previous night and 12:00 AM of tonight expressed as long values???
So many different date options I'm confused
you can use this calculation to do so.
//11:59 PM of the previous night and 12:00 AM of tonight
Calendar prevDate = Calendar.getInstance();
if(prevDate.get(Calendar.DATE)==1)
{
if(prevDate.get(Calendar.MONTH)==0)
{
prevDate.set(Calendar.YEAR, prevDate.get(Calendar.YEAR)-1);
prevDate.set(Calendar.MONTH,Calendar.DECEMBER);
prevDate.set(Calendar.DAY_OF_MONTH,31);
}
else
{
prevDate.set(Calendar.MONTH,prevDate.get(Calendar.MONTH)-1);
prevDate.set(Calendar.DAY_OF_MONTH, prevDate.getMaximum(Calendar.DAY_OF_MONTH));
}
}
else
{
prevDate.set(Calendar.DAY_OF_MONTH,prevDate.get(Calendar.DAY_OF_MONTH)-1);
}
prevDate.set(Calendar.HOUR_OF_DAY, 11);
prevDate.set(Calendar.MINUTE, 59);
Calendar currDate = Calendar.getInstance();
currDate.set(Calendar.HOUR_OF_DAY, 0);
currDate.set(Calendar.MINUTE, 0);
long prevDateLong = prevDate.getTimeInMillis();
long currDateLong = currDate.getTimeInMillis();
Log.i("", prevDate.toString()+" >>>>>>>> "+currDate.toString());
Log.i("", prevDateLong+" >>>>>>>>> "+currDateLong);
All Java dates are longs underneath. It's the number of milliseconds since midnight on 1-Jan-1970.
Date today = new SimpleDateFormat("yyyy-MMM-dd").parse("2012-Oct-28"); // today at midnight
long millis = today.getTime();
I'd recommend that you look at the java.util.Calendar class. It's not hard, just tedious.
http://www.exampledepot.com/search/luceneapi_node/Calendar