Creating a GregorianCalendar instance from milliseconds
Asked Answered
A

4

27

I have a certain time in milliseconds (in a Timestamp object) and I want to use it to create a GregorianCalendar object. How can I do that?

EDIT: How do I do the reverse?

Archivist answered 15/12, 2010 at 13:13 Comment(0)
I
45

Just get an instance of GregorianCalendar and setTime with your java.sql.Timestamp timestamp:

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

Edit: As peterh pointed out, GregorianCalendar.getInstance() will not provide a GregorianCalendar by default, because it is inherited fromCalendar.getInstance(), which can provide for example a BuddhistCalendar on some installations. To be sure to use a GregorianCalender use new GregorianCalendar() instead.

Ineslta answered 15/12, 2010 at 13:30 Comment(1)
Odd as it may seem you are not guaranteed a GregorianCalender by calling GregorianCalendar.getInstance() as you do. Since the question is specifically about GregorianCalendar I think this answer is slightly incorrect. (haaduken's answer is more correct)Person
D
47

To get a GregorianCalendar object and not a Calendar object. Like Michael's answer provides, you can also do the following:

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

This assumes a UTC epoch timestamp.

Dysphasia answered 7/5, 2013 at 9:53 Comment(0)
I
45

Just get an instance of GregorianCalendar and setTime with your java.sql.Timestamp timestamp:

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

Edit: As peterh pointed out, GregorianCalendar.getInstance() will not provide a GregorianCalendar by default, because it is inherited fromCalendar.getInstance(), which can provide for example a BuddhistCalendar on some installations. To be sure to use a GregorianCalender use new GregorianCalendar() instead.

Ineslta answered 15/12, 2010 at 13:30 Comment(1)
Odd as it may seem you are not guaranteed a GregorianCalender by calling GregorianCalendar.getInstance() as you do. Since the question is specifically about GregorianCalendar I think this answer is slightly incorrect. (haaduken's answer is more correct)Person
D
11
Timestamp timestamp = new Timestamp(23423434);
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTimeInMillis(timestamp.getTime());
Doolittle answered 15/12, 2010 at 13:25 Comment(3)
it's worth noting that the setTimeInMillis(...) method is inherited from the Calendar class. Also, formatting the code in your answer would be helpful. :)Tchao
so if you are setting time in timestamp and then getting, why create timestamp object at all? beats meOversize
@KalpeshSoni "I have a certain time in milliseconds (in a Timestamp object)" - OPDoolittle
C
1

I believe this works, although it may not be the best approach:

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);
    }

}
Chinoiserie answered 15/12, 2010 at 13:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.