久久久久久久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(設置和獲取小時、分鐘、秒的正確方法)
主站蜘蛛池模板: av日韩在线播放 | 久久久久九九九九 | 九九色综合 | 亚洲精品一区二三区不卡 | 中文字幕一区二区三区日韩精品 | 黑人粗黑大躁护士 | 欧美成人a| 草久久 | 啪啪免费网 | 日韩福利在线观看 | 四虎成人免费视频 | 在线观看亚洲专区 | 国产一区影院 | 婷婷久久五月天 | 午夜一级大片 | 天天干天天爱天天爽 | 国产欧美日韩综合精品一区二区 | 在线色网址 | 91久久综合 | 精品在线播放 | 久久久久九九九女人毛片 | 国产精品毛片一区二区在线看 | 日韩中文字幕在线播放 | 女女百合av大片一区二区三区九县 | 亚洲大片一区 | 亚洲精选一区二区 | 日韩av免费看 | 极品电影院 | 久久精品国产免费 | 99视频久 | 色婷婷综合成人av | 日韩精品视频在线观看一区二区三区 | 亚洲国产电影 | 欧美视频在线看 | 中文字幕国产精品视频 | 欧美成人高清视频 | 国产精品国产三级国产aⅴ中文 | 久久伊| 三级视频在线观看电影 | 能看的av| 亚洲xxxxx |