我有这种解析日常和时间的方法,问题是我需要得到下周的那一天。
示例:
我有这一天和时间星期一,晚上10点,当前时间星期一,晚上11点,
当我解析此parseTime("M","10:00 PM")
时,由于当前时间,它会返回过去的日期。
我想要实现的是下周的周一,晚上10点
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;
}
答案 0 :(得分:6)
将日历设置为下周。这会将日历设置为提前一周。
Calendar mCalendar = Calendar.getInstance();
int i = mCalendar.get(Calendar.WEEK_OF_MONTH);
mCalendar.set(Calendar.WEEK_OF_MONTH, ++i);
然后轻松找到下周的日子
/** Now get nExt week days **/
int dayvalues=mCalendar.get(Calendar.DAY_OF_WEEK);