2017-01-12 10 views
-2

タイムゾーンに従ってオフセット値を使って日付を修正しようとしています。そのため、タイムゾーンオフセットでタイムスタンプをフォーマットすると、SimpleDateFormatがオフセット値をその時刻に追加することが予想されました。ここ
は、私が試したものです:SimpleDateFormatがISTタイムゾーンオフセットを追加していません

package com.krishna.mytrials; 

import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.TimeZone; 

public class DateExperiments { 

    public static void main(String[] args) throws Exception { 
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
     //Date we set in UI 
     Date today = new Date(); 
     //The long value 
     String todayBrowserLocalTimeStamp = sdf.format(today); 
     System.out.println(todayBrowserLocalTimeStamp); 
     Date todayBrowserLocalTimeStampDate = sdf.parse(todayBrowserLocalTimeStamp); 
     System.out.println("Today's browser local time stamp: " + todayBrowserLocalTimeStampDate); 
     System.out.println("And its long value:" + todayBrowserLocalTimeStampDate.getTime()); 
     System.out.println("Date generate from long:"+ new Date(todayBrowserLocalTimeStampDate.getTime())); 
     //What server does to the above mid night time stamp of browser-local time zone 
     sdf.setTimeZone(TimeZone.getTimeZone("GMT")); 
     //What we get after it applied the server time zone to browser-local date 
     //### This is the wrong date 
     SimpleDateFormat sdf2 = new SimpleDateFormat(); 
     sdf2.setTimeZone(TimeZone.getTimeZone("GMT")); 
     sdf2.applyPattern("yyyy-MM-dd HH:mm"); 
     System.out.println(sdf2.format(todayBrowserLocalTimeStampDate)); 
     String utcDateString = sdf.format(todayBrowserLocalTimeStampDate); 
     System.out.println("The above mid night time stamp of browser-local time zone" 
       + "is converted to GMT.### The wrong one:"); 
     System.out.println(utcDateString); 
     //### The wrong date constructed 
     Date utcDate = sdf.parse(utcDateString); 
     System.out.println("###Wrong date:"+utcDate); 
     //### The wrong long 
     Long utcLong = utcDate.getTime(); 
     System.out.println("###Wrong long:"+utcLong); 
     // What we will do with the GMT+05:30 
     sdf.setTimeZone(TimeZone.getTimeZone("GMT+05:30")); 
     String dateToBeCorrected = sdf2.format(todayBrowserLocalTimeStampDate); 
     System.out.println("Date to be corrected:"+ dateToBeCorrected); 
     SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
     Date correctedDate = sdf3.parse(dateToBeCorrected); 
     System.out.println(correctedDate); 
     SimpleDateFormat sdf4 = new SimpleDateFormat(); 
     sdf4.setTimeZone(TimeZone.getTimeZone("IST")); 
     String correctedString = sdf4.format(correctedDate); 
     System.out.println("Corrected date:" + formatDateToString(correctedDate,"dd MMM yyyy hh:mm:ss a", "IST")); 


    } 

    public static String formatDateToString(Date date, String format, 
      String timeZone) { 
     // null check 
     if (date == null) return null; 
     // create SimpleDateFormat object with input format 
     SimpleDateFormat sdf = new SimpleDateFormat(format); 
     // default system timezone if passed null or empty 
     if (timeZone == null || "".equalsIgnoreCase(timeZone.trim())) { 
      timeZone = Calendar.getInstance().getTimeZone().getID(); 
     } 
     // set timezone to SimpleDateFormat 
     sdf.setTimeZone(TimeZone.getTimeZone(timeZone)); 
     // return Date in required format with timezone as String 
     return sdf.format(date); 
    } 


} 
Here is the output: 
2017-01-12 
Today's browser local time stamp: Thu Jan 12 00:00:00 IST 2017 
And its long value:1484159400000 
Date generate from long:Thu Jan 12 00:00:00 IST 2017 
2017-01-11 18:30 
The above mid night time stamp of browser-local time zoneis converted to GMT.### The wrong one: 
2017-01-11 
###Wrong date:Wed Jan 11 05:30:00 IST 2017 
###Wrong long:1484092800000 
Date to be corrected:2017-01-11 18:30 
Wed Jan 11 18:30:00 IST 2017 
Corrected date:11 Jan 2017 06:30:00 PM 
It is supposed add 05:30. to the date. What am I doing wrong? 
+0

あなたのスニペットは複雑すぎます。それを本質的に整えてください。 –

+0

ええ、質問を更新し、問題を起こしているコードだけを表示してください。 –

+0

また、メソッド内のサブタスクを1つの空白行で区切ってください。それは物事をかなり読みやすくします。 – VGR

答えて

0

あなたはそれが情報を失うことができますフォーマットと解析を使用してラウンドトリップを検討する必要があります。これは、書式設定された日付が元のDate-instanceよりも少ない情報を含む可能性があるためです。このデータの損失を見てください:

オリジナルスタンプ(変数todayBrowserLocalTimeStampDate)であった:2017-01-11 18:30(UTC)、または限り:

String utcDateString = sdf.format(new Date(1484175600000L)); 
// 2017-01-11 
Date utcDate = sdf.parse(utcDateString); 
ここ

を、あなたは時間の一部を剥ぎ取ると、再び取り除いた文字列を解析します。もちろん、結果の新しいDate - インスタンスは対応する時刻部分も失わなければならず、前と同じにすることはできません。

2017-01-11(ゼロ時間付き)は、ISTゾーンで5時間30分後に表示されます。つまり、2017-01-11T05:30 + 05:30です(覚えておいてください:Date.toString()はISTゾーンを使用します)。あなたのシステムゾーン)。これは2017-01-11T00:00Zと同じ瞬間です。すべては問題ありません。プレフィックス###Wrong dateで示された行の期待値だけが間違っています。

関連する問題