久久久久久久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(設置和獲取小時、分鐘、秒的正確方法)
主站蜘蛛池模板: 黄网在线观看 | 欧美在线视频网站 | 91文字幕巨乱亚洲香蕉 | 99av成人精品国语自产拍 | 久久久影院 | www.婷婷 | 久久久久久国产精品mv | 91在线免费观看 | 一级a性色生活片久久毛片波多野 | 一道本视频 | av网站在线看 | 国产福利资源在线 | 亚洲一区二区三区在线观看免费 | 国产精品久久久久久一级毛片 | 国产高清精品在线 | 午夜国产精品视频 | 日韩成人久久 | 色黄视频在线 | 免费在线成人网 | 一级高清免费毛片 | 永久看片 | 免费观看黄a一级视频 | 一区二区三区高清不卡 | 成人免费视频在线观看 | 亚洲一区视频在线 | 国产精品久久久久久久午夜片 | 久久久91精品国产一区二区三区 | 久久成人人人人精品欧 | 日韩在线电影 | 久夜精品 | 久久天天躁狠狠躁夜夜躁2014 | 日韩成人免费视频 | 国产一区二区在线免费 | 自拍第1页 | 国产日韩精品在线 | 欧美性另类 | 在线免费亚洲视频 | 免费人成在线观看网站 | 国产亚洲成av人片在线观看桃 | 亚洲 欧美 日韩在线 | 国内久久 |