30

I have seconds since 1970 january 1 UTC (Epoch time).

1320105600

I need to convert that seconds into date and time in below format.

Friday,November 4,2011 5:00,AM

How can I achieve this?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
androiduser
  • 917
  • 3
  • 11
  • 15

5 Answers5

51

In case you're restricted to legacy java.util.Date and java.util.Calendar APIs, you need to take into account that the timestamps are interpreted in milliseconds, not seconds. So you first need to multiply it by 1000 to get the timestamp in milliseconds.

long seconds = 1320105600;
long millis = seconds * 1000;

This way you can feed it to a.o. the constructor of java.util.Date and finally use SimpleDateFormat to convert a java.util.Date to java.lang.String in the desired date format pattern, if necessary with a predefined time zone (otherwise it would use the system default time zone, which is not GMT/UTC per se and thus the formatted time might be off).

Date date = new Date(millis);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE,MMMM d,yyyy h:mm,a", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String formattedDate = sdf.format(date);
System.out.println(formattedDate); // Tuesday,November 1,2011 12:00,AM

In case you're already on Java8, there's a LocalDateTime#ofEpochSecond() which allows you to feed epoch seconds directly without the need for multiplying into milliseconds flavor.

LocalDateTime dateTime = LocalDateTime.ofEpochSecond(seconds, 0, ZoneOffset.UTC);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE,MMMM d,yyyy h:mm,a", Locale.ENGLISH);
String formattedDate = dateTime.format(formatter);
System.out.println(formattedDate); // Tuesday,November 1,2011 12:00,AM
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Actually i made a mistake to post the question clearly.Here iam working with US chicago timings.And my seconds are 1320105600.The correct result for this question is Tuesday,November 0l,2011 7:00 AM but iam getting Monday,October 31,2011 7:00,PM by using the above code. – androiduser Nov 25 '11 at 00:30
  • I understand that there's a timezone difference, but 12 hours? – BalusC Nov 25 '11 at 00:40
  • I verified it once again for Chicago time (CST, GMT-6). Your comparison material is likely wrong or misinterpreted. Perhaps the AM/PM marker or 24 hour time information was not properly been interpreted. The exact 12-hour difference is also a too big coincidence. Note that the answer which you got on your [another question](http://stackoverflow.com/questions/8263148/how-to-find-seconds-since-1970-in-java) also confirms this. Try it with `CST` as timezone and `calendar.set(2011, Calendar.OCTOBER, 31, 19, 0);`. You'll get exactly `1320105600`. – BalusC Nov 25 '11 at 04:39
  • It seems that you or your colleague still didn't understand the timezone influence: http://stackoverflow.com/questions/8273200/code-to-convert-seconds-to-date-and-time-combination-in-java This also confirms that your comparison material is wrong. I'll edit the answer to include the UTC timezone. – BalusC Nov 25 '11 at 19:00
20
long yourSeconds = 1320105600L;
Date date = new Date(yourSeconds * 1000);

See this javadoc for more info. The constructor needs milliseconds.

To display this date in an appropriate format you should check DateFormat

Here is an example:

DateFormat df = new SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz");
System.out.println(df.format(date));
Gene
  • 10,819
  • 1
  • 66
  • 58
lukastymo
  • 26,145
  • 14
  • 53
  • 66
11

java.time

The Answer by BalusC is good in that it points you to using java.time. But that Answer uses LocalDateTime where Instant is more appropriate. A LocalDateTime is not a moment on the timeline as it purposely has no concept of offset-from-UTC or time zone.

java.time

The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat. The Joda-Time team also advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

Instant

A moment on the timeline in UTC with a resolution up to nanoseconds is represented by the Instant class.

Instant instant = Instant.ofEpochSecond ( 1_320_105_600L );

Dump to console. Your input value is the first moment of November 1, 2011 in UTC. The Z on the end, short for 'Zulu', means UTC.

System.out.println ( "instant: " + instant );

instant: 2011-11-01T00:00:00Z

ZonedDateTime

In your comments you mention wanting to see this date-time through the lens of the America/Chicago time zone. Use a proper time zone name. Apply a time zone, ZoneId, to get a ZonedDateTime object. We see that Chicago is five hours behind UTC on that date.

ZoneId zoneId = ZoneId.of ( "America/Chicago" );
ZonedDateTime zdt = instant.atZone ( zoneId );

zdt: 2011-10-31T19:00-05:00[America/Chicago]

Strings

The Strings seen above are in standard ISO 8601 format. To generate strings in other formats, use the DateTimeFormatter class. You can specify your own custom pattern. But generally best to let java.time automatically localize to the human language and cultural norms encoded in a Locale object.

DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime ( FormatStyle.FULL );
formatter = formatter.withLocale ( Locale.US );
String output = zdt.format ( formatter );

Monday, October 31, 2011 7:00:00 PM CDT

To specify your own custom format, search Stack Overflow for many examples and more discussion.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
2
int seconds = 1320105600;
Date date = new Date(seconds * 1000);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE,MMMM d,yyyy h:mm,a");
System.out.println(sdf.format(date));
Neil
  • 54,642
  • 8
  • 60
  • 72
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
0

The trick is to use java.util.Date and java.text.DateFormat to get the format you want. You can look up how to do it in tutorials on the Web.

Malcolm
  • 41,014
  • 11
  • 68
  • 91