为如何在 Java 中比较不同时区的小时数而苦恼?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2911924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Struggling with how to compare hours with different time zones in Java?
提问by Riki
I have 2 date object in the database that represent the company's working hours.
我在数据库中有 2 个日期对象代表公司的工作时间。
I only need the hours but since I have to save date. it appears like this:
我只需要时间,但因为我必须保存日期。它看起来像这样:
Date companyWorkStartHour;
Date companyWorkEndHour;
start hours: 12-12-2001-13:00:00 finish hours: 12-12-2001-18:00:00
开始时间:12-12-2001-13:00:00 结束时间:12-12-2001-18:00:00
I have the timezone of the company and of the user. (my server may be in another timezone).
我有公司和用户的时区。(我的服务器可能在另一个时区)。
TimeZone userTimeZone;
TimeZone companyTimeZone;
I need to check if the user's current time (considering his timezone) is within the company working hours (considering the company's time zone).
我需要检查用户的当前时间(考虑到他的时区)是否在公司工作时间内(考虑到公司的时区)。
How can I do it? I am struggling for over a week with Java calendar and with no success!
我该怎么做?我在 Java 日历上挣扎了一个多星期,但没有成功!
采纳答案by Jesper
The java.util.Dateclass is a container that holds a number of milliseconds since 1 January 1970, 00:00:00 UTC. Note that class Datedoesn't know anyting about timezones. Use class Calendarif you need to work with timezones. (edit19-Jan-2017: if you are using Java 8, use the new date and time API in package java.time).
本java.util.Date类是持有1970年1月00:00:00,UTC以来毫秒数的容器。请注意,该课程Date对时区一无所知。Calendar如果您需要使用时区,请使用 class 。(编辑2017 年 1 月 19 日:如果您使用的是 Java 8,请使用包中的新日期和时间 API java.time)。
Class Dateis not really suited for holding an hour number (for example 13:00 or 18:00) without a date. It's simply not made for that purpose, so if you try to use it like that, as you seem to be doing, you'll run into a number of problems and your solution won't be elegant.
类Date并不真正适合保存没有日期的小时数(例如 13:00 或 18:00)。它根本不是为此目的而设计的,因此如果您尝试这样使用它,就像您似乎正在做的那样,您将遇到许多问题并且您的解决方案不会优雅。
If you forget about using class Dateto store the working hours and just use integers, this will be much simpler:
如果您忘记使用 classDate来存储工作时间而只使用整数,这将简单得多:
Date userDate = ...;
TimeZone userTimeZone = ...;
int companyWorkStartHour = 13;
int companyWorkEndHour = 18;
Calendar cal = Calendar.getInstance();
cal.setTime(userDate);
cal.setTimeZone(userTimeZone);
int hour = cal.get(Calendar.HOUR_OF_DAY);
boolean withinCompanyHours = (hour >= companyWorkStartHour && hour < companyWorkEndHour);
If you also want to take minutes (not just hours) into account, you could do something like this:
如果您还想考虑分钟(不仅仅是小时),您可以执行以下操作:
int companyWorkStart = 1300;
int companyWorkEnd = 1830;
int time = cal.get(Calendar.HOUR_OF_DAY) * 100 + cal.get(Calendar.MINUTE);
boolean withinCompanyHours = (time >= companyWorkStart && time < companyWorkEnd);
回答by Eugene Kuleshov
Try something like this:
尝试这样的事情:
Calendar companyWorkStart = new GregorianCalendar(companyTimeZone);
companyWorkStart.setTime(companyWorkStartHour);
Calendar companyWorkEnd = new GregorianCalendar(companyTimeZone);
companyWorkEnd.setTime(companyWorkEndHour);
Calendar user = new GregorianCalendar(userTimeZone);
user.setTime(userTime);
if(user.compareTo(companyWorkStart)>=0 && user.compareTo(companyWorkEnd)<=0) {
...
}
回答by pie154
Hey I am not sure how you would do this using the Java calendar but I would highly recommend using the Joda Time package. It's a much simpler system to use and it gives you direct methods to extracts all subcomponents of data and time and even just to create simple time objects without the date involved. Then I imagine it would be a matter of comparing the 2 timezone differences and subtracting the difference from the JodaTime object.
嘿,我不确定您将如何使用 Java 日历来执行此操作,但我强烈建议使用 Joda Time 包。这是一个使用起来要简单得多的系统,它为您提供了提取数据和时间的所有子组件的直接方法,甚至可以创建不涉及日期的简单时间对象。然后我想这将是比较 2 个时区差异并从 JodaTime 对象中减去差异的问题。
回答by Snehal
I haven't tried the Joda library. This code should work.
我还没有尝试过 Joda 库。这段代码应该可以工作。
public boolean checkUserTimeZoneOverLaps(TimeZone companyTimeZone,
TimeZone userTimeZone, Date companyWorkStartHour,
Date companyWorkEndHour, Date userCurrentDate) {
Calendar userCurrentTime = Calendar.getInstance(userTimeZone);
userCurrentTime.setTime(userCurrentDate);
int year = userCurrentTime.get(Calendar.YEAR);
int month = userCurrentTime.get(Calendar.MONTH);
int day = userCurrentTime.get(Calendar.DAY_OF_MONTH);
Calendar startTime = Calendar.getInstance(companyTimeZone);
startTime.setTime(companyWorkStartHour);
startTime.set(Calendar.YEAR, year);
startTime.set(Calendar.MONTH, month);
startTime.set(Calendar.DAY_OF_MONTH, day);
Calendar endTime = Calendar.getInstance(companyTimeZone);
endTime.setTime(companyWorkEndHour);
endTime.set(Calendar.YEAR, year);
endTime.set(Calendar.MONTH, month);
endTime.set(Calendar.DAY_OF_MONTH, day);
if (userCurrentTime.after(startTime) && userCurrentTime.before(endTime)) {
return true;
}
return false;
}
EDITUpdated the code to reflect Bruno's comments. Shouldn't be taking the dates of the company work timings.
编辑更新了代码以反映布鲁诺的评论。不应该占用公司工作时间的日期。

