タイムゾーンを操作することは私の宿敵だと言わざるを得ません!
私のDB(PostGres)には、タイプ「タイムスタンプなしのタイムスタンプ」ゾーンのフィールドがあります。
保存する値は UTC 時間です。
私がやりたいことは、マシンのデフォルトのタイムゾーンに従って値を表示することです。
したがって、DB から値を取得するときは、まずこれが UTC 時間であることを「言う」必要があるため、タイムゾーンを UTC に設定します。
private Date lastUpdateToUTC( Date myDate)
{
if ( myDate!= null )
{
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
System.out.println("orig: " + dateFormat.format(myDate));
try
{
Date convertedDate = dateFormat.parse( dateFormat.format( myDate) );
dateFormat.setTimeZone( TimeZone.getDefault( ) );
System.out.println("converted: " + dateFormat.format(convertedDate));
return dateFormat.parse( dateFormat.format( convertedDate ) );
....
myDate が 2013-08-05 10:44:08 だとします。
私が期待しているのは、最初の出力が 2013-08-05 10:44:08 で、2 番目の出力が 2013-08-05 12:44:08 であることです。
代わりに、2013-08-05 08:44:08 と 2013-08-05 10:44:08 を取得します...
推論のどこに誤りがありますか? 期待どおりの結果を得るにはどうすればよいですか?