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

        <bdo id='fPZHl'></bdo><ul id='fPZHl'></ul>
      <legend id='fPZHl'><style id='fPZHl'><dir id='fPZHl'><q id='fPZHl'></q></dir></style></legend>

      <i id='fPZHl'><tr id='fPZHl'><dt id='fPZHl'><q id='fPZHl'><span id='fPZHl'><b id='fPZHl'><form id='fPZHl'><ins id='fPZHl'></ins><ul id='fPZHl'></ul><sub id='fPZHl'></sub></form><legend id='fPZHl'></legend><bdo id='fPZHl'><pre id='fPZHl'><center id='fPZHl'></center></pre></bdo></b><th id='fPZHl'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='fPZHl'><tfoot id='fPZHl'></tfoot><dl id='fPZHl'><fieldset id='fPZHl'></fieldset></dl></div>
      <tfoot id='fPZHl'></tfoot>
    1. <small id='fPZHl'></small><noframes id='fPZHl'>

      Java MySQL Timestamp 時區問題

      Java MySQL Timestamp time zone problems(Java MySQL Timestamp 時區問題)
      <tfoot id='X2FjH'></tfoot>

        <tbody id='X2FjH'></tbody>
    2. <legend id='X2FjH'><style id='X2FjH'><dir id='X2FjH'><q id='X2FjH'></q></dir></style></legend>
      <i id='X2FjH'><tr id='X2FjH'><dt id='X2FjH'><q id='X2FjH'><span id='X2FjH'><b id='X2FjH'><form id='X2FjH'><ins id='X2FjH'></ins><ul id='X2FjH'></ul><sub id='X2FjH'></sub></form><legend id='X2FjH'></legend><bdo id='X2FjH'><pre id='X2FjH'><center id='X2FjH'></center></pre></bdo></b><th id='X2FjH'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='X2FjH'><tfoot id='X2FjH'></tfoot><dl id='X2FjH'><fieldset id='X2FjH'></fieldset></dl></div>
        <bdo id='X2FjH'></bdo><ul id='X2FjH'></ul>

        <small id='X2FjH'></small><noframes id='X2FjH'>

                本文介紹了Java MySQL Timestamp 時區問題的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我有一個 java.util.Date 對象,我需要將它以 UTC 格式插入 MySQL 中的日期時間字段.

                I have a java.util.Date object, and I need to insert it into a datetime field in MySQL in UTC format.

                java.util.Date date = myDateFromSomewhereElse;
                PreparedStatement prep = con.prepareStatement(
                    "INSERT INTO table (t1, t2) VALUES (?,?)");
                
                java.sql.Timestamp t = new Timestamp(date.getTime());
                prep.setTimestamp(1, t, Calendar.getInstance(TimeZone.getTimeZone("PST"));
                prep.setTimestamp(2, t, Calendar.getInstance(TimeZone.getTimeZone("UTC"));
                System.out.println(prep.toString());
                

                這給了我準備好的 SQL 語句字符串:

                Which gives me the prepared SQL statement string:

                INSERT INTO table (t1, t2) VALUES ('2012-05-09 11:37:08','2012-05-09 11:37:08');
                

                無論我指定的時區如何,返回的時間戳都是相同的時間戳.它忽略了我指定的帶有時區的 Calendar 對象.發生了什么事,我做錯了什么?

                The timestamp returned is the same timestamp regardless of the timezone I specify. It's ignoring the Calendar object with timezone that I specify. What is going on and what am I doing wrong?

                推薦答案

                TimeZones 只是查看日期(即固定時間點)的不同方式.我在這里寫了一個小例子(密切注意斷言):

                TimeZones are just different ways to view a date (which is a fixed point in time). I wrote a little example here (pay close attention to the assert):

                // timezone independent date (usually interpreted by the timezone of 
                // the default locale of the user machine)
                Date now = new Date();
                
                // now lets get explicit with how we wish to interpret the date
                Calendar london =  Calendar.getInstance(TimeZone.getTimeZone("Europe/London"));
                Calendar paris = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris"));
                
                // now set the same date on two different calendar instance
                london.setTime(now);
                paris.setTime(now);
                
                // the time is the same
                assert london.getTimeInMillis() == paris.getTimeInMillis();
                
                // London is interpreted one hour earlier than Paris (as of post date of 9th May 2012)
                String londonTime = london.get(Calendar.HOUR) + ":" + london.get(Calendar.MINUTE);
                String londonTZ = london.getTimeZone().getDisplayName(london.getTimeZone().inDaylightTime(london.getTime()), TimeZone.SHORT);
                System.out.println(londonTime + " " + londonTZ);
                
                // Paris is interpreted one hour later than Paris (as of post date of 9th May 2012)
                String parisTime = paris.get(Calendar.HOUR) + ":" + paris.get(Calendar.MINUTE);
                String parisTZ = paris.getTimeZone().getDisplayName(paris.getTimeZone().inDaylightTime(paris.getTime()), TimeZone.SHORT);
                System.out.println(parisTime + " " + parisTZ);
                

                此代碼段的輸出是(結果會因執行日期/時間而異):

                The output to this snippet is (the result will be different depending on execution date/time):

                8:18 BST
                9:18 CEST
                

                您在問題中的代碼段根本沒有對存儲的日期做任何事情.通常數據庫配置為本地時區.我建議存儲一個額外的字段來表示解釋日期時要使用的時區.

                Your snippet in the question is simply not doing anything with regard to the date being stored. Usually databases are configured for a native TimeZone. I advise storing an extra field representing the TimeZone to be used when interpreting the date.

                (通常)修改日期不是一個好主意(基本上是固定時間點之前/之后的幾毫秒),因為這將是一種有損修改,在一年中的不同時間點會有不同的解釋(由于到夏令時).

                It is not (generally) a good idea to modify dates (which are essentially just milliseconds before/after a fixed point in time) as this would be a lossy modification that would be interpreted differently at different points in the year (due to daylight savings time).

                或者這個:http://puretech.paawak.com/2010/11/02/how-to-handle-oracle-timestamp-with-timezone-from-java/

                這篇關于Java MySQL Timestamp 時區問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                相關文檔推薦

                quot;Char cannot be dereferencedquot; error(“Char 不能被取消引用錯誤)
                Java Switch Statement - Is quot;orquot;/quot;andquot; possible?(Java Switch 語句 - 是“或/“和可能的?)
                Java Replace Character At Specific Position Of String?(Java替換字符串特定位置的字符?)
                What is the type of a ternary expression with int and char operands?(具有 int 和 char 操作數的三元表達式的類型是什么?)
                Read a text file and store every single character occurrence(讀取文本文件并存儲出現的每個字符)
                Why do I need to explicitly cast char primitives on byte and short?(為什么我需要在 byte 和 short 上顯式轉換 char 原語?)
                <legend id='Uepik'><style id='Uepik'><dir id='Uepik'><q id='Uepik'></q></dir></style></legend>

                <tfoot id='Uepik'></tfoot>
                • <bdo id='Uepik'></bdo><ul id='Uepik'></ul>

                  1. <i id='Uepik'><tr id='Uepik'><dt id='Uepik'><q id='Uepik'><span id='Uepik'><b id='Uepik'><form id='Uepik'><ins id='Uepik'></ins><ul id='Uepik'></ul><sub id='Uepik'></sub></form><legend id='Uepik'></legend><bdo id='Uepik'><pre id='Uepik'><center id='Uepik'></center></pre></bdo></b><th id='Uepik'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Uepik'><tfoot id='Uepik'></tfoot><dl id='Uepik'><fieldset id='Uepik'></fieldset></dl></div>
                          <tbody id='Uepik'></tbody>

                          <small id='Uepik'></small><noframes id='Uepik'>

                          主站蜘蛛池模板: 99福利视频 | 请别相信他免费喜剧电影在线观看 | 另类专区成人 | 免费同性女女aaa免费网站 | 精品日韩在线 | 97超碰在线播放 | 日韩在线免费播放 | 久久91视频| 国产99视频精品免视看9 | 久久精品国产一区二区电影 | 在线观看第一页 | 国产精品精品视频 | 精久久久 | 久久99视频免费观看 | 亚洲精品一区二区三区在线 | 97起碰| 成人国产精品色哟哟 | 亚洲国产一区二区三区四区 | 在线午夜电影 | 一区二区三区免费 | 色婷婷久久久亚洲一区二区三区 | 欧美日韩不卡 | 欧美一区二区三区大片 | 91在线看片| 日本午夜免费福利视频 | 日韩毛片在线免费观看 | 久久天天 | 亚洲人成人一区二区在线观看 | 91视频在线观看 | 成人网av | 天天射色综合 | 亚洲日日夜夜 | 国产精品久久久久久婷婷天堂 | 一级黄色片在线免费观看 | 欧美亚洲一区二区三区 | 国产在线观看网站 | 狠狠综合久久av一区二区小说 | 成人国产免费观看 | 成人免费小视频 | 请别相信他免费喜剧电影在线观看 | 色综合色综合 |