久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為如何比較 Java 中不同時(shí)區(qū)的時(shí)間而苦惱?

Struggling with how to compare hours with different time zones in Java?(為如何比較 Java 中不同時(shí)區(qū)的時(shí)間而苦惱?)
本文介紹了為如何比較 Java 中不同時(shí)區(qū)的時(shí)間而苦惱?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

我在數(shù)據(jù)庫(kù)中有 2 個(gè)代表公司工作時(shí)間的日期對(duì)象.

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

我只需要時(shí)間,但因?yàn)槲冶仨毐4嫒掌?它看起來(lái)像這樣:

I only need the hours but since I have to save date. it appears like this:

Date companyWorkStartHour; 
Date companyWorkEndHour;

開(kāi)始時(shí)間:12-12-2001-13:00:00結(jié)束時(shí)間:12-12-2001-18:00:00

start hours: 12-12-2001-13:00:00 finish hours: 12-12-2001-18:00:00

我有公司和用戶(hù)的時(shí)區(qū).(我的服務(wù)器可能在另一個(gè)時(shí)區(qū)).

I have the timezone of the company and of the user. (my server may be in another timezone).

TimeZone userTimeZone;
TimeZone companyTimeZone;

我需要檢查用戶(hù)的當(dāng)前時(shí)間(考慮到他的時(shí)區(qū))是否在公司工作時(shí)間內(nèi)(考慮到公司的時(shí)區(qū)).

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

我該怎么做?我在 Java 日歷上苦苦掙扎了一個(gè)多星期,但沒(méi)有成功!

How can I do it? I am struggling for over a week with Java calendar and with no success!

推薦答案

java.util.Date 類(lèi)是一個(gè)容器,它保存了自 1970 年 1 月 1 日 00:00:00 以來(lái)的毫秒數(shù)世界標(biāo)準(zhǔn)時(shí)間.請(qǐng)注意,類(lèi) Date 對(duì)時(shí)區(qū)一無(wú)所知.如果您需要使用時(shí)區(qū),請(qǐng)使用類(lèi) Calendar.(edit 2017 年 1 月 19 日:如果您使用的是 Java 8,請(qǐng)使用包 java.time 中的新日期和時(shí)間 API).

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 并不適合保存沒(méi)有日期的小時(shí)數(shù)(例如 13:00 或 18:00).它根本不是為了那個(gè)目的而設(shè)計(jì)的,所以如果你嘗試像那樣使用它,就像你正在做的那樣,你會(huì)遇到很多問(wèn)題,你的解決方案也不會(huì)優(yōu)雅.

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.

如果您忘記使用類(lèi) Date 來(lái)存儲(chǔ)工作時(shí)間而只使用整數(shù),這會(huì)簡(jiǎn)單得多:

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

如果您還想考慮幾分鐘(而不僅僅是幾小時(shí)),您可以這樣做:

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

這篇關(guān)于為如何比較 Java 中不同時(shí)區(qū)的時(shí)間而苦惱?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時(shí)間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉(zhuǎn)換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉(zhuǎn)換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當(dāng)前星期幾的值)
主站蜘蛛池模板: 欧美亚洲视频 | 在线欧美一区二区 | 亚洲视频在线一区 | 精品日韩欧美一区二区 | 国产一区在线免费观看视频 | 国产偷录视频叫床高潮对白 | 九九九视频 | 久久国产精品视频观看 | 国产精品美女久久久久久免费 | 男女羞羞免费网站 | 中国美女av | 一区二区免费高清视频 | 欧美视频免费 | 九九热在线视频 | 国产黄a一级 | 中文字幕亚洲免费 | 中文字幕在线观 | 国产精品日产欧美久久久久 | 久久久一二三区 | 99热精品国产 | 国产精品一区视频 | 亚洲精品一区二区三区免 | 国产第一亚洲 | 日韩成人影院 | www.色综合| 国产精品1区2区 | 久久国产视频网站 | 伊伊综合网 | 日本一二三区高清 | 一区二区三区四区在线视频 | 久久一二区 | 久久久久久女 | 欧美成人影院在线 | 中文字幕日韩欧美一区二区三区 | 国产三区在线观看视频 | 青青草在线播放 | 在线欧美日韩 | 久草资源在线 | 国产欧美精品一区二区三区 | 国产精品自产av一区二区三区 | 国产精品久久久久久 |