Reputation: 2311
I have the number of seconds from 31 Dec 1969 19:00:00 i.e. (EST) and I want to convert the same to a date format.
I have the following code which returns any invalid date. Can anyone point out what seems to be the issue here.
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class sample
{
public static void main(String args[])
{
DateFormat df = new SimpleDateFormat("dd/MMM/yyyy HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("EST"));
Date d1 = new Date(1000*1373604190); //Converting to millisecond
String formattedDate = df.format(d1);
System.out.println(formattedDate); //I'm getting 22/Dec/1969 16:50:55
}
}
How can I solve this.
Upvotes: 1
Views: 206
Reputation: 340350
Instant.ofEpochSeconds( 1_373_604_190L )
.atZone( ZoneId.of( "America/New_York" ) )
.format( DateTimeFormatter.ofPattern( "dd MMM uuuu HH:mm:ss" , Locale.CANADA ) )
the number of seconds from 31 Dec 1969 19:00:00 i.e. (EST)
Do not think parochially when doing date-time work. Rather than translate values such as the epoch reference moment in and out of your own zone, just think in UTC. Consider UTC to be The One True Time, all others being variations on that theme.
So, always refer to the epoch reference moment as 1970-01-01T00:00:00Z
, the first moment of 1970 in UTC, rather than in any other time zone.
(EST)
Also, never use those 3-4 letter abbreviations as time zones. Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Those 3-4 letter abbreviation such as EST
or IST
are not true time zones, not standardized, and not even unique(!).
The modern way to do this work is with the java.time classes.
long secondsSinceEpoch = 1_373_604_190L ;
We convert that to a Instant
object. The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).
Instant instant = Instant.ofEpochSeconds( secondsSinceEpoch ) ;
To see this value through the lens of some wall-clock time, apply a time zone as a ZoneId
to get a ZonedDateTime
object.
I am guessing that by EST
you meant the time zone used by much of the eastern portions of the United States and Canada. I will arbitrarily choose the time zone America/New_York
.
ZoneId z = ZoneId.of( "America/New_York" );
ZonedDateTime zdt = instant.atZone( "America/New_York" );
secondsSinceEpoch: 1373604190
instant.toString(): 2013-07-12T04:43:10Z
zdt.toString(): 2013-07-12T00:43:10-04:00[America/New_York]
To generate a string, you can either let java.time automatically localize or you can specify an explicit formatting pattern.
Locale locale = Locale.CANADA ;
DateTimeFormatter formatterLocalized = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.MEDIUM ).withLocale( locale );
String outputLocalized = zdt.format( formatterLocalized );
12-Jul-2013 12:43:10 AM
DateTimeFormatter formatterCustom = DateTimeFormatter.ofPattern( "dd MMM uuuu HH:mm:ss" , locale );
String output = zdt.format( formatterCustom );
12 Jul 2013 00:43:10
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Upvotes: 0
Reputation: 17422
You are using int
literals instead of long
in your numbers. By default a integral literal is int
unless you specify that is long
with L at the end. Try this:
Date d1 = new Date(1000*1373604190L);
(note the L at the end of your literal 1373604190)
Upvotes: 5