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

將 UTC 時間轉換為 Java 中的本地時區

converting a UTC time to a local time zone in Java(將 UTC 時間轉換為 Java 中的本地時區)
本文介紹了將 UTC 時間轉換為 Java 中的本地時區的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我知道這個主題已經被打死了,但是在搜索了幾個小時之后我不得不問這個問題.

I know this subject has been beaten to death but after searching for a few hours to this problem I had to ask.

我的問題:根據客戶端應用程序 (iphone) 的當前時區計算服務器上的日期.客戶端應用程序以秒為單位告訴服務器其時區距 GMT 有多遠.然后我想使用這些信息來計算服務器中的日期.服務器上的日期都存儲為 UTC 時間.所以我想在轉換為本地時區后獲取 UTC Date 對象的 HOUR.

My Problem: do calculations on dates on a server based on the current time zone of a client app (iphone). The client app tells the server, in seconds, how far away its time zone is away from GMT. I would like to then use this information to do computation on dates in the server. The dates on the server are all stored as UTC time. So I would like to get the HOUR of a UTC Date object after it has been converted to this local time zone.

我目前的嘗試:

int hours = (int) Math.floor(secondsFromGMT / (60.0 * 60.0));
int mins = (int) Math.floor((secondsFromGMT - (hours * 60.0 * 60.0)) / 60.0);
String sign = hours > 0 ? "+" : "-";

Calendar now = Calendar.getInstance();
TimeZone t = TimeZone.getTimeZone("GMT" + sign + hours + ":" + mins);
now.setTimeZone(t);

now.setTime(someDateTimeObject);

int hourOfDay = now.get(Calendar.HOUR_OF_DAY);

變量 hour 和 mins 表示本地時區與 GMT 相差的小時和分鐘.調試此代碼后 - 變量小時、分鐘和符號是正確的.

The variables hour and mins represent the hour and mins the local time zone is away from GMT. After debugging this code - the variables hour, mins and sign are correct.

問題是 hourOfDay 沒有返回正確的小時 - 它返回的是 UTC 時間的小時,而不是本地時間.想法?

The problem is hourOfDay does not return the correct hour - it is returning the hour as of UTC time and not local time. Ideas?

推薦答案

您的時區字符串格式不正確.試試這個,

You timezone string is formulated incorrectly. Try this,

String sign = secondsFromGMT > 0 ? "+" : "-";
secondsFromGMT = Math.abs(secondsFromGMT);      
int hours = secondsFromGMT/3600;
int mins = (secondsFromGMT%3600)/60;
String zone = String.format("GMT%s%02d:%02d", sign, hours, mins);          
TimeZone t = TimeZone.getTimeZone(zone);

這篇關于將 UTC 時間轉換為 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:獲取當前星期幾的值)
主站蜘蛛池模板: 亚洲专区在线 | 在线成人免费视频 | 午夜视频在线观看一区二区 | 欧美综合在线观看 | 欧美国产激情二区三区 | 日本免费在线 | 孰女乱色一区二区三区 | 久久久久一区 | 中文字幕日韩欧美 | 欧美综合国产精品久久丁香 | 在线不卡视频 | 欧美一区二区三区在线看 | 午夜视频在线免费观看 | 午夜视频在线播放 | av免费入口| 久久精品一级 | 欧美日韩久久精品 | 日韩国产一区二区三区 | 天天拍天天操 | 午夜影院官网 | 国产日韩精品一区 | 国产精品国产馆在线真实露脸 | 九九九久久国产免费 | 国产欧美精品区一区二区三区 | av福利网站| 成人精品鲁一区一区二区 | 九九免费在线视频 | 色视频www在线播放国产人成 | 91偷拍精品一区二区三区 | 最近最新中文字幕 | 日韩一级电影免费观看 | 亚洲精品在线视频 | 国产乱码精品1区2区3区 | 狠狠av | 中文字幕 国产 | 视频在线一区二区 | 中文字幕精品一区二区三区精品 | 91精品国产综合久久香蕉麻豆 | 人干人人 | 亚洲av毛片 | 蜜桃视频在线观看免费视频网站www |