简体   繁体   中英

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

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?

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM