【发布时间】:2014-10-06 10:32:10
【问题描述】:
让我描述一下我的场景:
我在位于德克萨斯州达拉斯的服务器上有一个正在运行的应用程序(我认为它在内部使用 EDT 时区)。在此服务器中,我需要获取时间服务器,将其转换为欧洲/马德里时区,然后检查获取的日期是否在日期间隔内。
奇怪的是,我收到的响应表明,当前服务器时间在较低日期间隔之前转换为欧洲/马德里时区,这很奇怪。
这是我的做法,获取服务器时间并将其转换为欧洲/马德里时区:
DateTimeZone timeZoneMadrid = DateTimeZone.forID( "Europe/Madrid" );
DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm");
DateTime nowServer = new DateTime();
log.debug("Current server time is " + nowServer.toString(formatter));
DateTime nowServerSpanishTimeZone = nowServer.withZone(timeZoneMadrid);
log.debug("Current server time converted to Madrid Zone is " + nowServerSpanishTimeZone.toString(formatter));
输出:
Current server is 2014-10-06 06:12
Current server time converted to Madrid Zone is 2014-10-06 12:12
现在,我根据转换后的 DateTime 为间隔、开始和结束创建 DateTime:
int year = serverTimeConverted.getYear();
int month = serverTimeConverted.getMonthOfYear();
int day = serverTimeConverted.getDayOfMonth();
this.setStartDate(new DateTime(year, month, day, 8, 0, 0, 0));
this.setEndDate(new DateTime(year, month, day, 21, 0, 0, 0));
如你所见,我的时间间隔是从 08:00:00 到 21:00:00
然后我检查转换的服务器时间是否在日期范围内,这非常冗长,因为我添加了很多检查和输出,因为奇怪的行为......:
private boolean withinTimeRange(DateTime now, DateTime start, DateTime end){
DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYYMMdd-HH:mm");
String currentDate = now.toString(formatter);
long nowTimeStamp = now.getMillis() / 1000;
long startTimeStamp = start.getMillis() / 1000;
long endTimeStamp = end.getMillis() / 1000;
log.debug("Checking if date " + currentDate + " is in the interval dates " + start.toString(formatter) + " and " + end.toString(formatter));
log.debug("Checking if UNIX timestamp " + nowTimeStamp + " is in the interval dates " + startTimeStamp + " and " + endTimeStamp);
if (!now.isBefore(start)){
log.debug("Current time " + currentDate + " is not before " + start.toString(formatter));
if (!now.isAfter(end)){
log.debug("Current time " + currentDate + " is not after " + end.toString(formatter));
return true;
}
else{
log.debug("Current time " + currentDate + " is after " + end.toString(formatter));
return false;
}
}
else{
log.debug("Current time " + currentDate + " is before " + start.toString(formatter));
return false;
}
}
只需使用转换的时间服务器以及开始和结束日期调用该方法,对于之前的输出,转换的服务器时间是 2014-10-06 12:12,我从之前的方法得到这个输出:
Checking if date 20141006-12:12 is in the interval dates 20141006-08:00 and 20141006-21:00
Checking if UNIX timestamp 1412590332 is in the interval dates 1412596800 and 1412643600
Current time 20141006-12:12 is before 20141006-08:00
Current timeserver converted to Madrid TimeZone is not within time range, skipping iteration
如您所见,转换后的服务器时间的时间戳在开始日期时间之前.....这怎么可能?
我认为我在创建 DateTime 开始和结束时做错了什么,我尝试使用 .withTimeZone("Europe/Madrid") 创建它们,但后来我得到了最奇怪的行为......任何线索?
谢谢!
更新:基于之前的 SO 问题 here,我修改了之前的代码,现在它可以工作了:
DateTime now = new DateTime();
LocalDate today = now.toLocalDate();
LocalDate tomorrow = today.plusDays(1);
DateTimeZone timeZoneMadrid = DateTimeZone.forID( "Europe/Madrid" );
DateTime start = today.toDateTimeAtStartOfDay(timeZoneMadrid);
DateTime end = tomorrow.toDateTimeAtStartOfDay(timeZoneMadrid);
start = start.plusHours(8);
end = end.minusHours(4);
Interval interval = new Interval(start, end);
DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm");
String currentDate = now.toString(formatter);
if (interval.contains(now)){
return true;
}
else{
return false;
}
【问题讨论】: