对不起,我想我在这个问题上花了太多时间,把问题搞糊涂了。
我有以下代码:
System.out.println("Time Now: " + new Date());
Calendar beginningOfDay = new GregorianCalendar();
beginningOfDay.set(Calendar.HOUR_OF_DAY, 0);
beginningOfDay.set(Calendar.MINUTE, 0);
beginningOfDay.set(Calendar.SECOND, 0);
beginningOfDay.set(Calendar.MILLISECOND, 0);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Date specifiedTime = null;
try{
specifiedTime = sdf.parse("20:55"); // this time is extracted from interface
} catch(ParseException pe){} // complete this later
System.out.println("specified time is: " + specifiedTime);
Calendar runResults = new GregorianCalendar(TimeZone.getDefault());
if (beginningOfDay.getTimeInMillis() + specifiedTime.getTime() > System.currentTimeMillis())
{ System.out.println("specified time has not occured yet");
runResults.setTimeInMillis(beginningOfDay.getTimeInMillis() + specifiedTime.getTime());
System.out.println("Time to run tests is: " + runResults.getTime());
}
else
{ System.out.println("need to run tests tomorrow");
beginningOfDay.add(Calendar.DATE, 1); // add a day
runResults.setTimeInMillis(beginningOfDay.getTimeInMillis() + specifiedTime.getTime());
System.out.println("Time to run tests is: " + runResults.getTime());
}
输出如下:
Time Now: Thu Aug 25 20:17:52 BST 2011
specified time is: Thu Jan 01 20:55:00 GMT 1970
need to run tests tomorrow
Time to run tests is: Fri Aug 26 19:55:00 BST 2011
那么为什么指定的时间解析返回19:55 BST?
我试图设置sdf (simpledateformatter)时区与日历相同,但似乎不起作用。
理想情况下,我想要的是得到任何时间被解析(例如:"20.55")被转换为当地时区的时间,如果那个时间还没有发生,那么计时器被设置为在那个时候响起,否则它应该等到第二天。这是我试图计算定时器启动TimerTask的时间的代码。如果有人能提出更好的解决办法,我将不胜感激。当本地时间在指定时间范围内时,Timer应该启动TimerTask。
再次感谢您的帮助。
编辑:使用这个来使它工作。虽然不是很整洁
Calendar timeNow = new GregorianCalendar();
String timeNowString = timeNow.getTime().toString();
String timeNowFirstBit = timeNowString.substring(0, 10); // e.g. "Sat Aug 27"
String timeNowLastBit = timeNowString.substring(24); // e.g. "2011"
String userSpecifiedTime = "14:32";
SimpleDateFormat specTimeFormat = new SimpleDateFormat("EEE MMM dd HH:mm yyyy");
String allNewSpecifiedTime = timeNowFirstBit + " " + userSpecifiedTime + " " + timeNowLastBit;
Date specifiedTime = null;
try{
specifiedTime = specTimeFormat.parse(allNewSpecifiedTime);
} catch (ParseException pe){}
Calendar runResults = new GregorianCalendar();
runResults.setTimeInMillis(specifiedTime.getTime());
if (specifiedTime.getTime() > System.currentTimeMillis())
{ System.out.println("specified time has not occured yet");
}
else
{ System.out.println("need to run tests tomorrow");
runResults.add(Calendar.DATE, 1); // add a day
}
System.out.println("Time to run tests is: " + runResults.getTime());
原因是在给定时间内没有BST。英国夏令时从3月的最后一个星期日开始,到10月的最后一个星期日结束。所以在一月份是格林尼治时间——把你的时间移到夏天,一切都会好起来的:)。
虽然有点混乱,但这是一个好问题,谢谢。
Try
specifiedTime = new GregorianCalendar();
specifiedTime.set(Calendar.HOUR_OF_DAY, 20 );
specifiedTime.set(Calendar.MINUTE, 55 );
specifiedTime.set(Calendar.SECOND, 0);
specifiedTime.set(Calendar.MILLISECOND, 0);
System.out.println("specified time is: " + specifiedTime.getTime());
或
try{
specifiedTime = sdf.parse("20:55"); // this time is extracted from interface
beginningOfDay.add(Calendar.MILLISECOND, specifiedTime.getTime());
} catch(ParseException pe){} // complete this later