2011-11-13 8 views
0

@Jon Skeet、Date parsing/formatting with TimeZone and SimpleDateFormat give different results around DST switchでお勧めするようにJoda-Timeをダウンロードし、それをJava EE/JSFプロジェクトに追加し、DateTimeとDateTimeFormatterを使用しようとしました。現在はESTタイムゾーン(米国/東部)の夏時間節約になっているので、JDK 6(下を参照)と同じ結果を返しています。JDKとjoda-time dateformatter:昼間の節約時間を考慮する方法

ORDER#0739トリップ日2011年11月11日 2011年11月12日に顧客番号1004

トリップ日付/時刻:2011年11月11日午前5時PM レポート日付/時刻:11/2011分の11 4:45 PM 戻り日付/時刻:次のように2011年11月13日2時00分AM

コードは次のとおりです。

public String getDateFromDateTime (Date date, Boolean display) throws ParseException { 

    /* 
    * SimpleDateFormat working as designed, but pf_ordersController.selected.returnDateTime displaying incorrect date/time 
    * 
    * see below from /orders/pf_View.xhtml 
    * pf_ordersController.selected.returnDateTime (displayed on JSF page) = 11/13/2011 02:00 AM 
    * 
    * ORDER # 0739 Trip Date 11/11/2011 to 11/12/2011 Customer # 1004 
    * 
    * Trip Date/Time: 11/11/2011 05:00 PM Report Date/Time: 11/11/2011 04:45 PM Return Date/Time: 11/13/2011 02:00 AM 
    * 
    * orders.returnDateTime (stored in database) = 11/12/2011 21:00:00 (9:00 PM) 
    * SimpleDateFormat converts orders.returnDateTime to 11/12/2011 (working as designed) 
    * 
    * https://stackoverflow.com/questions/2356672/date-parsing-formating-with-timezone-and-simpledateformat-problem-around-dst-swi 
    * 
    DateFormat formatter; 
    String myDate; 

    if (display) 
     formatter = new SimpleDateFormat("MM/dd/yyyy"); 
    else 
     formatter = new SimpleDateFormat("yyyy-MM-dd"); 

    myDate = formatter.format(date); 
    * 
    */ 

    DateTimeFormatter dtFormatter; 

    if (display) 
     dtFormatter = DateTimeFormat.forPattern("MM/dd/yyyy"); 
    else 
     dtFormatter = DateTimeFormat.forPattern("yyyy-MM-dd"); 

    DateTime dt = new DateTime(date); 
    String myDate = dt.toString(dtFormatter); 

    System.out.println("OrderDisplayUtil.java:getDateFromDateTime(" + date + ", " + display + "): " + "myDate = " + myDate); 

    return myDate; 

} 

助けてください。ありがとう。

+0

に終了したことに注意してください、私たちは、あなたが得た方法を知っておく必要がありますあなたの入力。私はそれが単体テストか短いか完全なコンソールアプリケーションの形になっているかどうかは気にしませんが、どちらかの方法で実行できるものがあります:) –

+0

@JonSkeet、あなたの素早い応答に感謝します。私は、SimpleDateFormatが設計どおりに動作していることを確認しました。なぜなら問題の日時はこのメソッドに渡されるOrders.returnDateTime(getDateFromDateTime(Orders.returnDateTime、...))であるからです。 Orders.returnDateTime = 2011-11-12 21:00:00、JSF /コントローラコードに何らかの理由で2011-11-13 02:00:00が表示されています。私はこれを見続ける。ちなみに、どうやって質問してほしいのか教えてくれてありがとう。私はあなたがすでに私がここで初心者だと言うことができると確信しています。 :) – Howard

+0

良い質問をすることに関するより多くの提案については、http://tinyurl.com/so-hintsを参照してください。あなたのコードで*たくさんのコンバージョンが発生しているようです。それらを一つずつ分離し、問題がどこにあるかを正確に調べる*。 –

答えて

1

あなたの方法に伴う問題の具体的な説明をしてください。あなたの入力値はあなたの出力値として何を期待していますか?開発者は、これをテストの形で表現する必要があります。

@Test 
public void testGetDateFromDateTime() { 
    DateTimeZone timezone = DateTimeZone.forID("US/Eastern"); 
    Date date = new DateTime(2011, 11, 11, 17, 0, 0, 0, timezone).toDate(); 

    String formattedDate = unitUnderTest.getDateFromDateTime(date, true); 

    Assert.assertEquals(formattedDate, "11/11/2011"); 
} 

あなたの問題は、日付を書式設定するタイムゾーンを明示的に指定していないことです。この場合、Javaは、JVMを実行しているオペレーティングシステムで設定されたタイムゾーンを使用します。たとえば、+08:00のタイムゾーンを使用して、シンガポールで稼働しているシステムで、米国/イースターの日付/時刻2011/11/11 16:45をフォーマットする場合は、システムは日付/時刻を正確に同じ時刻を表す2011/11/12 06:00と解釈します。システムはフォーマッタを使用し、'11/12/2011 'を与えます。

使用したいのは、タイムゾーンを指定することでこの問題を解決してみましょう:

また
public String getDateFromDateTime(Date date, Boolean display) { 

    DateTimeFormatter dtFormatter; 

    if (display) 
     dtFormatter = DateTimeFormat.forPattern("MM/dd/yyyy"); 
    else 
     dtFormatter = DateTimeFormat.forPattern("yyyy-MM-dd"); 

    DateTimeZone timeZone = DateTimeZone.forID("US/Eastern"); 
    DateTime dt = new DateTime(date, timeZone); 
    String myDate = dt.toString(dtFormatter); 

    return myDate; 
} 

はオリエンが言ったように米国の夏時間は11月6日

+0

答え/応答とコードの変更/お勧めをありがとう。上記でJonSkeetに言及したように、SimpleDateFormatは設計どおりに動作していることがわかりました。 JSF /コントローラロジックをデバッグし、Orders.returnDateTimeがこの注文レコード/行の正しい日付/時刻でない理由を確認する必要があります。どこにいても、この注文。returnDateTimeは正しく表示されますが、OrdersController.selected.returnDateTimeを使用して日付を表示すると正しく表示されません。 xhtmlは他の行のために設計されたように動作しますが、これは動作しません。以前私が誤って言ったので、私はここで質問を更新すべきですか?再度、感謝します。 – Howard

+0

ああ、忘れて、今、ターゲットエンドユーザは、おそらく私の家族のために、ネットワークファイアウォールの背後にあるこのウェブアプリケーションを使用しているでしょう。日付/時刻の値は、常にオフィスのWebアプリケーションを使用している担当者に基づいているため、localDateTimeは現在の要件を満たしています。正直なところ、このビジネス要件でTimeZoneを検討する必要があるユースケースは考えられません。再度、感謝します。 – Howard

関連する問題