如何在Java中将时间戳转换为日期格式 [英] How to convert Timestamp into Date format in java
本文介绍了如何在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屋!
查看全文