久久久久久久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(設置和獲取小時、分鐘、秒的正確方法)
主站蜘蛛池模板: 日本不卡视频 | 7777奇米影视 | 国产一区二区成人 | 2020国产在线| 亚洲一区视频在线 | 超级碰在线 | 午夜视频在线观看视频 | 欧美日韩亚洲一区 | 伊人精品在线 | 中文字幕 亚洲一区 | 免费一级网站 | 欧美αv | 国产视频久久 | 亚洲精品成人av | 成人不卡一区二区 | 91看片网站| 草草视频在线免费观看 | 香蕉一区二区 | 亚洲精品中文在线观看 | 欧美在线日韩 | 中文字幕精品一区二区三区精品 | 久久久一区二区三区四区 | 一级黄色生活视频 | 国产日韩欧美精品一区二区三区 | 国产精品成人一区二区 | 人人九九精 | 欧美在线观看一区 | 亚洲v日韩v综合v精品v | 亚洲国产精品一区二区三区 | 精品欧美一区二区精品久久久 | 99只有精品 | 99久久久久国产精品免费 | 99综合| 久久久久久久久91 | 成人在线免费观看av | 一区在线视频 | 水蜜桃久久夜色精品一区 | 综合久久99 | 色视频成人在线观看免 | 亚洲黄色av| 久久欧美精品 |