努力与Java中的不同时区比较小时数? [英] Struggling with how to compare hours with different time zones in Java?

查看:116
本文介绍了努力与Java中的不同时区比较小时数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中有两个代表公司工作时间的日期对象。



我只需要几个小时,但是由于我必须保存日期。看起来像这样:

  Date companyWorkStartHour; 
Date companyWorkEndHour;

开始时间:12-12-2001-13:00:00
完成时间: 12-12-2001-18:00:00



我有公司和用户的时区。 (我的服务器可能位于另一个时区)。

  TimeZone userTimeZone; 
TimeZone companyTimeZone;

我需要检查用户当前的时间(考虑到他的时区)在公司工作时间内考虑到公司的时区)。



我该怎么做?我用Java日历挣扎了一个多星期,没有成功!

解决方案

java.util .Date class是从1970年1月1日00:00:00 UTC起持续数毫秒的容器。请注意,类 Date 不知道有关时区的信息。如果您需要使用时区,请使用课程 Calendar 。 (编辑:2017年1月19日:如果您使用Java 8,请在包 java.time )中使用新的日期和时间API。 / p>

日期不适合持有小时号(例如13:00或18:00),没有一个约会。这完全不是为了这个目的,所以如果你这样尝试使用它,就像你似乎正在做的那样,你会遇到一些问题,你的解决方案不会很优雅。



如果您忘记使用类 Date 来存储工作时间并使用整数,这将会简单得多:

  Date userDate = ...; 
TimeZone userTimeZone = ...;

int companyWorkStartHour = 13;
int companyWorkEndHour = 18;

日历cal = Calendar.getInstance();
cal.setTime(userDate);
cal.setTimeZone(userTimeZone);

int hour = cal.get(Calendar.HOUR_OF_DAY);
boolean withinCompanyHours =(hour> = companyWorkStartHour&&& hour< companyWorkEndHour);

如果你也想花几分钟(不只是几个小时),你可以做这样的事情:

  int companyWorkStart = 1300; 
int companyWorkEnd = 1830;

int time = cal.get(Calendar.HOUR_OF_DAY)* 100 + cal.get(Calendar.MINUTE);
boolean inCompanyHours =(time> = companyWorkStart&& time< companyWorkEnd);


I have 2 date object in the database that represent the company's working hours.

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

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!

解决方案

The java.util.Date class is a container that holds a number of milliseconds since 1 January 1970, 00:00:00 UTC. Note that class Date doesn't know anyting about timezones. Use class Calendar if you need to work with timezones. (edit 19-Jan-2017: if you are using Java 8, use the new date and time API in package java.time).

Class Date is 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.

If you forget about using class Date to store the working hours and just use integers, this will be much simpler:

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);

这篇关于努力与Java中的不同时区比较小时数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆