Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a java.util.Calendar instance at epoch time?

Tags:

calendar

Is there any existing way to get a Calendar populated with time at epoch using Calendar API's other than explicitly setting them at epoch? All I was able to do was to get the current time.

like image 602
chamibuddhika Avatar asked Oct 20 '10 13:10

chamibuddhika


People also ask

What is Calendar getInstance () in Java?

The getInstance() method in Calendar class is used to get a calendar using the current time zone and locale of the system. Syntax: public static Calendar getInstance() Parameters: The method does not take any parameters. Return Value: The method returns the calendar.

How do you convert date to epoch time?

Convert from human-readable date to epochlong epoch = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime() / 1000; Timestamp in seconds, remove '/1000' for milliseconds. date +%s -d"Jan 1, 1980 00:00:01" Replace '-d' with '-ud' to input in GMT/UTC time.


1 Answers

There is no pre defined constructor or factory method to do this, but it is fairly simple:

Calendar c = Calendar.getInstance();
c.setTimeInMillis(0);
like image 158
Nivas Avatar answered Sep 20 '22 06:09

Nivas