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?
GregorianCalendar is a concrete subclass of Calendar and provides the standard calendar system used by most of the world.
getGregorianChange() is an inbuilt method in Java which returns the Gregorian Calendar change date which is the change from Julian Calendar dates to Gregorian Calendar dates.
Calendar 's getInstance method returns a Calendar object whose calendar fields have been initialized with the current date and time: Calendar rightNow = Calendar.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With