对不起,我想我已经花了很长时间,并且感到困惑。
我有以下代码:
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
那么为什么指定的Time解析会在19:55 BST回来?
我尝试将sdf(simpledateformatter)时区设置为与日历相同,但它似乎不起作用。
理想情况下我想要的是将解析的任何时间(例如“20.55”)转换为本地时区的时间,如果还没有发生时间,那么Timer会设置为当时关闭,否则应该等到第二天。这是我试图计算Timer启动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());
答案 0 :(得分:2)
尝试
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
答案 1 :(得分:2)
原因是在给定时间内没有BST。 “英国夏令时期从3月的最后一个星期日开始,到10月的最后一个星期天结束。”因此,1月是GMT - 将你的时间转移到夏天,一切都会好的。)。
虽然有点混乱,但这是一个很好的问题,谢谢。