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

解析 ISO 8601 字符串本地日期時間,就像在 UTC 中

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
本文介紹了解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

在 java 中,我需要從格式的字符串中創建一個日歷對象:

In java I need to make a Calendar object from a String in the format:

yyyy-MM-dd'T'HH:mm:ss

此字符串將始終設置為 GMT 時間.所以這是我的代碼:

This string will always be set as GMT time. So here's my code:

    public static Calendar dateDecode(String dateString) throws ParseException
{
    TimeZone t = TimeZone.getTimeZone("GMT");
    Calendar cal = Calendar.getInstance(t);
    date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    Date d = date.parse(dateString);
    cal.setTime(d);
    return cal;
}

然后:

Calendar cal = Calendar.getInstance();
    try
    {
        cal = dateDecode("2002-05-30T09:30:10");
    } catch (ParseException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    int month =  cal.get(Calendar.MONTH)+1;

我得到以下輸出:

Timezone: GMT+00:00 date: 2002-5-30 time: 7:30:10

您可以看到這是錯誤的,因為提供的時間是格林威治標準時間而不是歐洲中部時間.我認為發生的情況是它認為提供的時間是 CET(這是我當前的時區),因此將時間從 CET 轉換為 GMT,因此從最終結果中減去兩個小時.

Which is as you can see wrong since the time provided is in GMT and not CET. I think what happens is that it think the time provided is in CET (which is my current timezone) and therefore converts the time from CET to GMT and therefore deducts two hours from the final result.

誰能幫我解決這個問題?

Could anyone help me with this?

謝謝

順便說一句:出于不同的原因,我不想使用 JodaTime.

Btw: I do not wish to use JodaTime for different reasons.

推薦答案

這里有一些代碼可以幫助你在解析它們之前設置時區:

Here is some code that could help you out with setting the timzones before parsing them:

// sdf contains a Calendar object with the default timezone.
Date date = new Date();
String formatPattern = ....;
SimpleDateFormat sdf = new SimpleDateFormat(formatPattern);

TimeZone T1;
TimeZone T2;
....
....
// set the Calendar of sdf to timezone T1
sdf.setTimeZone(T1);
System.out.println(sdf.format(date));

// set the Calendar of sdf to timezone T2
sdf.setTimeZone(T2);
System.out.println(sdf.format(date));

// Use the 'calOfT2' instance-methods to get specific info
// about the time-of-day for date 'date' in timezone T2.
Calendar calOfT2 = sdf.getCalendar();

我發現的另一個類似問題也可能有所幫助:如何在Java中設置默認時區并控制日期在DB上的存儲方式?

another similar question I found might help too: How to set default time zone in Java and control the way date are stored on DB?

這是一個關于 Java & 的很棒的教程.日期:http://www.tutorialspoint.com/java/java_date_time.htm

Here is a great tutorial on Java & Dates too: http://www.tutorialspoint.com/java/java_date_time.htm

這篇關于解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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:獲取當前星期幾的值)
The correct way to set and get hour, minutes, sec(設置和獲取小時、分鐘、秒的正確方法)
主站蜘蛛池模板: 精品在线播放 | 久热精品在线 | 日韩精品在线观看视频 | 日韩在线中文 | 国产黄色免费网站 | 在线a视频 | 久久精品一区二区三区四区五区 | 国产麻豆精品视频 | 国产主播一区二区 | 综合伊人久久 | 亚洲iv一区二区三区 | 国产精品视频网站 | 91一级片 | 亚洲我射av | 激情综合网站 | 亚洲激情在线播放 | 日韩国产精品视频 | 欧美日韩国产精品 | 欧美日韩精品久久 | av三级在线观看 | 伊人网在线播放 | 久久精品国产一区 | 久久依人 | 日韩av免费播放 | 欧美日韩精品 | 久久久久一区二区 | 国产精品黄色 | 依人在线 | 国产日韩综合 | 蜜桃av一区二区三区 | 正在播放国产精品 | 国产在线不卡 | 国产精品伦 | 天堂中文在线视频 | 日韩视频免费 | 色综合久久天天综合网 | 日韩精品在线一区 | 国产成人精品亚洲男人的天堂 | 欧美做受69| 99国产精品99久久久久久粉嫩 | 国精产品99永久一区一区 |