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

為如何比較 Java 中不同時區的時間而苦惱?

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

問題描述

限時送ChatGPT賬號..

我在數據庫中有 2 個代表公司工作時間的日期對象.

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;

開始時間:12-12-2001-13:00:00結束時間:12-12-2001-18:00:00

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

我該怎么做?我在 Java 日歷上苦苦掙扎了一個多星期,但沒有成功!

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

推薦答案

java.util.Date 類是一個容器,它保存了自 1970 年 1 月 1 日 00:00:00 以來的毫秒數世界標準時間.請注意,類 Date 對時區一無所知.如果您需要使用時區,請使用類 Calendar.(edit 2017 年 1 月 19 日:如果您使用的是 Java 8,請使用包 java.time 中的新日期和時間 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 并不適合保存沒有日期的小時數(例如 13:00 或 18:00).它根本不是為了那個目的而設計的,所以如果你嘗試像那樣使用它,就像你正在做的那樣,你會遇到很多問題,你的解決方案也不會優雅.

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.

如果您忘記使用類 Date 來存儲工作時間而只使用整數,這會簡單得多:

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 中不同時區的時間而苦惱?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
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 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 国产成人精品一区二区三区在线 | 午夜电影网站 | 精品视频一区二区三区四区 | 天堂一区二区三区 | 亚洲欧美一区二区三区国产精品 | 亚洲天堂成人在线视频 | 国产亚洲精品美女久久久久久久久久 | 免费一级黄色电影 | 日本午夜免费福利视频 | 欧美视频| 精品久| 日韩精品视频在线 | 狠狠做深爱婷婷综合一区 | 99在线资源 | 亚洲国产精品久久久久秋霞不卡 | 成人av大全| 国外成人在线视频网站 | 国产一区二 | 男女羞羞视频在线观看 | 欧美精品v | 亚洲一区精品视频 | 国产精品99久久久久久久久 | 久久久久久久av | 欧美国产在线一区 | 九九在线 | 成人免费视频网站在线看 | 一区二区三区视频免费看 | 99精品久久久久久 | 久久国产麻豆 | 国产精品视频一二三区 | 精品91| 亚洲精品一区国语对白 | 成人a免费 | 亚洲精品视频在线 | 日韩欧美专区 | 欧美激情一区二区三级高清视频 | 日韩一区二区在线免费观看 | 青青久草 | 一区二区三区四区国产 | 日日夜夜精品免费视频 | 亚洲精品久久久9婷婷中文字幕 |