1

How to convert PHP timestamp to display datetime format as string in Java (difference UTC)?

<?php
echo date('d-m-Y h:i:sa',1495221086);
// Result: 19-05-2017 09:11:26pm

<?php
date_default_timezone_set('Asia/Phnom_Penh'); // UTC+07:00
echo date('d-m-Y h:i:sa',1495221086);
// Result: 20-05-2017 02:11:26am

Question: how to convert 1495221086 to 20-05-2017 02:11:26am in Java?

4
  • Possible duplicate of How to convert TimeStamp to Date in Java? Commented May 19, 2017 at 21:11
  • No duplicated .. Commented May 19, 2017 at 21:18
  • difference timezone (UTC+7:00) Commented May 19, 2017 at 21:19
  • How to change UTC+0:00 to UTC+7:00 in Java? Commented May 19, 2017 at 21:20

2 Answers 2

3

You can do this using Java's standard (legacy) classes Date and SimpleDateFormat, as follows:

Date date = new Date(1495221086*1000L);
DateFormat df = new SimpleDateFormat(
    "dd-MM-yyyy hh:mm:ssa", Locale.UK /* for displaying "AM" correctly */);
df.setTimeZone(TimeZone.getTimeZone("Asia/Phnom_Penh"));
System.out.println(df.format(date));

This prints out

20-05-2017 02:11:26AM
Sign up to request clarification or add additional context in comments.

1 Comment

I spent many times for searching this problem, now It work. Thank you so much.
3

I think this would do the trick:

Date date = new Date(1495221086 * 1000L);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss aaa");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Phnom_Penh"));
String dateFormated = simpleDateFormat.format(date);
Log.i("MyInfo", "dateFormated: " + dateFormated);

4 Comments

The actual date is off here (millis vs seconds).
I/MyInfo: dateFormated: 18-01-1970 14:20:21 下午
Yes. You need to change the Timezone to "Asia/Phnom_Penh"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.