java中如何将时间戳转换为日期格式 [英] How to convert Timestamp into Date format in java

查看:109
本文介绍了java中如何将时间戳转换为日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的时间戳(形成一个 json 响应):

I have a time stamp like this(form a json response) :

"/日期(1479974400000-0800)/"

"/Date(1479974400000-0800)/"

我正在尝试使用此功能将时间戳转换为日期:

I'm trying this function to convert time stamp into date:

public String getDate() {
    Calendar cal = Calendar.getInstance(Locale.ENGLISH);
    cal.setTimeInMillis(time);
    String date = DateFormat.format("dd-MM-yyyy", cal).toString();
    return date;
}

如何将此时间戳转换为日期格式?

推荐答案

解决方案有效对我来说是这样的:

The solution works for me is like this:

 String str = obj.getString("eventdate").replaceAll("\D+", "");
String upToNCharacters = str.substring(0, Math.min(str.length(), 13));
DateFormat timeZoneFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
timeZoneFormat.setTimeZone(TimeZone.getTimeZone("GMT-8"));

Date time = new java.util.Date(Long.parseLong(upToNCharacters));
//                                System.out.println(time);
model.setDate(String.valueOf(timeZoneFormat.format(time)));

在需要的地方使用时间变量

Use time variable where you want

这篇关于java中如何将时间戳转换为日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆