Java: epoch date to MM/DD/YYYY

假装没事ソ 提交于 2019-12-12 12:26:23

问题


My time: 1386696238

My code:

Date date = new Date(Long.parseLong(currentNotification.getDate_created()));
        SimpleDateFormat formatter = new SimpleDateFormat("MM/DD/yyyy", Locale.getDefault());
        String dateString = formatter.format(date);

My result: 1/16/1970

Desired result: 12/10/2013

What am I doing wrong?


回答1:


Your value is in seconds, so you need to multiply it by 1000.

Also, DD is day of year, you want dd:

SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy", Locale.getDefault());



回答2:


I suppose you should multiply your input with 1000 (UNIX timestamp in seconds, but Java j.u.Date in milliseconds).

Edit:

Oh another mistake in your code: The pattern symbol should be d, not D (day-of-year).



来源:https://stackoverflow.com/questions/22326468/java-epoch-date-to-mm-dd-yyyy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!