Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

datetime - How can I convert epoch time to date and time in Java?

I need to convert epoch timestamp to date and time. I have used the following code to convert but it converts to a wrong date, year and time.

String date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss")
                    .format(new java.util.Date (1319286637/1000));

Expected output was today’s date at some time, but the result I got was:

01/01/1970 05:51:59

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The Date(long) constructor takes milliseconds. You should be multiplying by 1000, not dividing the epoch time you have.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...