2017-03-24 2 views
-3

この回答を探していましたが、これを実装する方法について間違いがあります。私はZonedDateTime変数を持っていて、現在はそれを印刷すると、2017-12-03T10:15:30 + 01:00のように印刷されます。オフセットが既に追加または削除されている時間を印刷する方法はありますか?例えば、私は上記の例16:30を見たいと思います。ご協力ありがとうございました!オフセットを含む現在の時刻を表示するZonedDateTime形式

+2

私が正しく理解していれば、あなただけでZonedDateTimeインスタンスを表示するにのぼる何をしたいあなたはDateオブジェクトを持った後、次にあなたが望むオフセット追加しますUTC。 DateTimeFormatterでタイムゾーンを設定することができます。 – korolar

+1

'2017-12-03T10:15:30 + 01:00'は、指定されたタイムゾーンでは' 10:15:30 '、UTCタイムゾーンでは '09:15:30'(' 16: 30 ')。時間はすでに指定された時間帯にあります。オフセット情報を削除する場合は、['toLocalDateTime()'](https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html#toLocalDateTime--)を呼び出して'2017-12-03T10:15:30'。この質問は[ISO日付形式](https://en.wikipedia.org/wiki/ISO_8601)の誤解に基づいているため、投票を中止してください。 – Andreas

+0

ご協力いただきありがとうございます!私は私の問題を解決しました! – paul590

答えて

0

はこれを見てください:

public Date parseDateTime(String input) throws java.text.ParseException { 

    //NOTE: SimpleDateFormat uses GMT[-+]hh:mm for the TZ which breaks 
    //things a bit. Before we go on we have to repair this. 
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz"); 

    //this is zero time so we need to add that TZ indicator for 
    if (input.endsWith("Z")) { 
     input = input.substring(0, input.length() - 1) + "GMT-00:00"; 
    } else { 
     int inset = 6; 

     String s0 = input.substring(0, input.length() - inset); 
     String s1 = input.substring(input.length() - inset, input.length()); 

     input = s0 + "GMT" + s1; 
    } 

    return df.parse(input); 
} 

ので、それはあなたの必要性に合う作るには少し変更があります。あなたは少しの努力でそれを行うことができるはずです。

int myNumHours = 4; //Any number of hours you want 
myDateObject.add(Calendar.HOUR,myNumHours); 
//Adds myNumHours to the Hour slot and processes all carrys if needed be. 

はそれを印刷する:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
String myDateString = df.format(myDateObject); 
//Now just use your myDateString wherever you want. 
関連する問題