-1

when I run the following code:

package testframe;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class dates {

    public static void handleLastValueDate()  {

        Timestamp stamp = new Timestamp(1490018838);
        Date date = new Date(stamp.getTime());
         SimpleDateFormat df =  new SimpleDateFormat("yyyy-MM-dd HH:mm");

    }

    public static void main(String[] args) {

        handleLastValueDate();

    }

}

I get the following result: 1970-01-18 07:53 which is incorrect and should be

GMT: Mon, 20 Mar 2017 12:55:38 GMT
Your time zone: 3/20/2017, 2:55:38 PM GMT+2:00

can anyone please explain for me why this is happening? I tired to play with locale but also that didn't help! how can I see the correct time?

thanks.

flashDisk
  • 55
  • 5

1 Answers1

2

Your code is printing 1970-01-18 07:53 because you need to multiply 1490018838 by 1000 (milliseconds), on the other hand, no need to use the TimeStamp, Date class has an overloaded Constructor taking the epoch in milliseconds

Date date = new Date(1490019439L * 1000);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
System.out.println(df.format(date));

be awware also about this.

new Date(1490019439L * 1000)

am using the timeStamp as Long literal, if you dont, then an overflow in the integer will happens and you will get again a wrong date...

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97