2010-12-15 16 views

答えて

39

ちょうどあなたをjava.sql.Timestamp timestampとのGregorianCalendarとのsetTimeのインスタンスを取得:

Calendar cal=GregorianCalendar.getInstance(); 
cal.setTime(timestamp); 
1

それが最善のアプローチではないかもしれないが、私は、この作品信じる:

import java.sql.Date; 
import java.sql.Timestamp; 
import java.util.GregorianCalendar; 

public class TimestampToGregorianCalendar { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     Timestamp t = new Timestamp(12356342); // replace with existing timestamp 
     Date d = new Date(t.getTime()); 
     Calendar gregorianCalendar = GregorianCalendar.getInstance(); 
     gregorianCalendar.setTime(d); 
    } 

} 
8
Timestamp timestamp = new Timestamp(23423434); 
Calendar calendar = GregorianCalendar.getInstance(); 
calendar.setTimeInMillis(timestamp.getTime()); 
+0

setTimeInMillis(...)メソッドがCalendarクラスから継承されていることには注意してください。また、答えにコードを書式設定すると便利です。 :) – Riggy

+0

タイムスタンプの時刻を設定してからタイムスタンプオブジェクトを作成するのはなぜですか?私を打ち負かす –

+0

@KalpeshSoni "タイムスタンプオブジェクトの中にあるミリ秒単位で特定の時間がある" - OP – aepryus

33

Michaelの答えのようにCalendarオブジェクトではなくGregorianCalendarオブジェクトを取得するには、次を試すこともできます。

long timestamp = 1234567890; 
GregorianCalendar cal = new GregorianCalendar(); 
cal.setTimeInMillis(timestamp); 

これは、UTCエポックタイムスタンプを前提としています。

+2

+1 UTCエポックのタイムスタンプ情報+1 –

関連する問題