2017-01-20 7 views
3

現在、私のアプリの日付はISOの日付2016-08-26T11:03:39.000+01:00として保存されています。1つのタイムゾーンのロケール時間をすべてのタイムゾーンの時間として保持する方法は?

ユーザーが英国の11時03分39秒にイベントに行った場合、この時間は06:03:39のように米国フロリダに表示されません。これは現在私のアプリで起こっている。

ISOの日付を、イベントが発生した場所とユーザーの場所に関係なく、ローカルで発生した時刻に戻すにはどうすればよいですか。

以下は現在使用しているコードです。

DateFormat isoDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); 
DateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy"); 
DateFormat timeFormat = new SimpleDateFormat("hh:mm a"); 
mDate = isoDateFormat.parse(isoDateString); 
dateString = dateFormat.format(mDate); 
timeString = timeFormat.format(mDate); 
+0

java.util.Date/Calendarの代わりにjava.timeを使用できますか?もしそうなら、それはあなたの人生を楽にするはずです... –

+0

@JonSkeetはわかりません。私はちょうどISOの日付文字列を解析することができるようにする必要がありますので、私は日付の文字列と時間の文字列を持っています。 –

+0

これはあなたが探しているものですか? http://stackoverflow.com/questions/9429357/date-and-time-conversion-to-some-other-timezone-in-java – weston

答えて

1

これは、ISO文字列からオフセットを取得し、そのようにタイムゾーンを取得することによって行うことができます。

import java.text.SimpleDateFormat; 
import java.text.DateFormat; 
import java.text.ParseException; 
import java.util.TimeZone; 
import java.util.Date; 

class Main { 
    public static void main(String[] args) { 
    DateFormat isoDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); 
    //DateFormat isoDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); I needed to use XXX in repl. 
    DateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy"); 
    DateFormat timeFormat = new SimpleDateFormat("hh:mm a"); 
    try 
    { 
     String isoString = "2016-08-26T11:03:39.000+01:00"; 
     Date mDate = isoDateFormat.parse(isoString); 
     System.out.println("GMT" + isoString.substring(23)); 
     TimeZone mTimeZone = TimeZone.getTimeZone("GMT" + isoString.substring(23)); 
     dateFormat.setTimeZone(mTimeZone); 
     timeFormat.setTimeZone(mTimeZone); 
     System.out.println(mTimeZone.toString()); 
     String dateString = dateFormat.format(mDate); 
     String timeString = timeFormat.format(mDate); 
     System.out.println(dateString + " " + timeString); 
    } 
    catch(ParseException e) 
    { 
     System.out.println("Error"); 
    } 
    } 
} 

ここでは、再生に使用できるreplリンクがあります。

https://repl.it/FPrN/2

関連する問題