0

So I tried now for about hours to convert a Timestamp to a local date (CEST).

     Date date = new Date(stamp*1000);
     SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     simpleDateFormat.setTimeZone(TimeZone.getTimeZone("CEST"));
     String myDate = simpleDateFormat.format(date);

It's not working whatever I tried and looked up in Internet I always get back the UTC time......

for better understanding: stamp is a variable timestamp with type long which I will receive from a service

Nali
  • 157
  • 2
  • 5
  • 22
  • This should help: http://stackoverflow.com/questions/7670355/convert-date-time-for-given-timezone-java – Jens Jul 08 '16 at 13:51
  • I tried the advice with 11 upvotes as you might have seen, its not working – Nali Jul 08 '16 at 13:52
  • try [this](http://stackoverflow.com/questions/19431234/converting-between-java-time-localdatetime-and-java-util-date) – Rahul Sharma Jul 08 '16 at 13:53
  • I'm not using Java 8, working on Java 6 EE – Nali Jul 08 '16 at 13:55
  • @Nali is that 6 the version of the Server ? notice that jre version is different than the Java EE version that comes with the application server(Glassfish,JBoss,etc) – niceman Jul 08 '16 at 14:11
  • so you might have java 8 with a server that supports Java EE 6 only :) and they will run together, there isn't any strong relation between Java EE version and jre(jdk) version – niceman Jul 08 '16 at 14:14
  • 1
    @Nali You should post example data and results so we can verify. – Basil Bourque Jul 08 '16 at 22:21

2 Answers2

2

tl;dr

String output = ZonedDateTime.ofInstant ( Instant.ofEpochSecond ( 1_468_015_200L ) , ZoneId.of ( "Europe/Paris" ) ).toString();

Details

A few issues:

  • You are not using proper time zone names.
    • Proper names are in continent/region format.
    • The 3-4 letter abbreviations so commonly seen in the media such as CEST are not true time zones. Avoid them. They are neither standardized nor unique(!).
  • You are using old outmoded date-time classes that are poorly designed and confusing. They have been supplanted by the java.time framework.

If by CEST you meant 2 hours ahead of UTC in the summer, then let's take Europe/Paris as an example time zone. Your Question lacks example data, so I'll make up this example.

Apparently your input is a count of whole seconds from the epoch of first moment of 1970 in UTC. That value can be used directly, no need to multiply.

The ZoneId class represents the time zone. An Instant is a point on the timeline in UTC with a resolution up to nanoseconds. A ZonedDateTime is the Instant adjusted into the ZoneId.

ZoneId zoneId = ZoneId.of ( "Europe/Paris" );
long input = 1_468_015_200L;   // Whole seconds since start of 1970.
Instant instant = Instant.ofEpochSecond ( input );
ZonedDateTime zdt = ZonedDateTime.ofInstant ( instant , zoneId );

Dump to console.

System.out.println ( "input: " + input + " | instant: " + instant + " | zdt: " + zdt );

input: 1468015200 | instant: 2016-07-08T22:00:00Z | zdt: 2016-07-09T00:00+02:00[Europe/Paris]

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • @Nali Do you mean the format of the String that represents the value of the date-time? Your Question says nothing about formatting. Anyways, search Stack Overflow for many examples of using `DateTimeFormatter` to generate Strings in any format. The formats shown here comply with the ISO 8601 standard. You can specify other formats or let java.time localize automatically to any `Locale`. – Basil Bourque Jul 11 '16 at 08:20
  • @BasilBourque As stated in the comments the OP doesn't use Java 8 yet. I don't know what are his constraints but I suppose he has to deal with it for the moment. – C.Champagne Jul 11 '16 at 08:23
  • 1
    @C.Champagne Most of the java.time functionality is back-ported to Java 6 & 7 in the [ThreeTen-Backport](http://www.threeten.org/threetenbp/) project, and further adapted to Android in the ThreeTenABP project. Those names come from the JSR 310 that defines java.time. – Basil Bourque Jul 11 '16 at 08:27
  • @BasilBourque Nice! I didn't know it. Thanks! – C.Champagne Jul 11 '16 at 08:47
0

Your TimeZone id is likely to be incorrect (well, not recognized by Java). It seems that instead of throwing an exception the TimeZone is evaluated to UTC in that case.

Try this instead:

simpleDateFormat.setTimeZone(TimeZone.getTimeZone("ECT"));

Here is a page giving some information about Java's TimeZone and a list of timezone ids.

C.Champagne
  • 5,381
  • 2
  • 23
  • 35
  • I tried everything it will just show me the UTC time nothing else – Nali Jul 11 '16 at 08:07
  • @Nali What do you mean exactly by everything? Have you tried the `continent/region`format? `simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Europe/Brussels"));`for example – C.Champagne Jul 11 '16 at 08:17
  • @Nali What does `TimeZone.getAvailableIDs()` return? Anyway, you should use one of those values. – C.Champagne Jul 11 '16 at 08:28
  • nevermind it works now, don't know why but it works :D thanks – Nali Jul 11 '16 at 13:39