我已经多次看到此问题,并且没有任何答案似乎是我需要的。将历元时间转换为日期字符串
我有一个很长的类型变量,它存储了一个纪元时间。
我想要做的是将其转换为字符串
例如,如果存储时代时间是今天最后一个字符串将是:
17/03/2012
会如何我对此?
我已经多次看到此问题,并且没有任何答案似乎是我需要的。将历元时间转换为日期字符串
我有一个很长的类型变量,它存储了一个纪元时间。
我想要做的是将其转换为字符串
例如,如果存储时代时间是今天最后一个字符串将是:
17/03/2012
会如何我对此?
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.format(new Date(myTimeAsLong));
你会创建一个从long
一个Date
- 这很简单:
Date date = new Date(epochTime);
注意epochTime
这里应该是在自毫秒为单位 - 如果你从纪元得到秒,乘由1000.
然后,你会创建一个SimpleDateFormat
指定相关模式,文化和时区。例如:
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
format.setTimeZone(...);
然后用它来格式化日期字符串:
String text = format.format(date);
我会为Timezone参数做些什么? – nexus490 2012-03-17 23:15:34
@ nexus490:那么你想在什么时区代表价值?例如,在一个小时内将在2012年3月18日的* my *时区中,但在美国它仍然是2012年3月17日,时间为自相同的毫秒数。正确答案是什么?这只是*你知道的,因为你知道你的应用程序需求(希望)。也许你想要UTC。也许你想要系统本地时区。也许你想要用户的家庭时区。 – 2012-03-17 23:40:24
感谢您强调这一点:“如果自从这个时代以来**秒**,乘以** 1000 **。” – Metallica 2015-01-10 21:42:58
你需要知道,在Java中出现时间是在毫秒,而你正在转换的可能是在几秒钟内。确保转换的两侧都是毫秒,然后您可以从Date对象中获取日期参数。
为什么我会将毫秒转换为毫秒? – 2013-12-04 16:09:44
我的错误,我的意思是你应该确保转换的双方都以毫秒为单位。 – Ajibola 2013-12-04 17:18:43
如果出现时间你的意思是毫秒的计数,因为在UTC的1970年第一时刻,那么这里就是使用乔达时库一些示例代码...
DateTimeZone timeZone = DateTimeZone.forID("Europe/Paris");
DateTime dateTime = new DateTime(yourMilliseconds, timeZone);
String output = DateTimeFormat.forStyle("S-").withLocale(Locale.CANADA_FRENCH).print(dateTime);
时代的定义很常见,因为它在Unix中的使用。但请注意,各种计算机系统至少使用了一个couple dozen epoch definitions。
Date date = new Date(String);
这已弃用。
解决方案
Date date = new Date(1406178443 * 1000L);
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String formatted = format.format(date);
确保乘1000L
只是一个小小的评论,您需要将HH:MM:ss.SSS更改为HH:mm:ss.SSS - MM是月份,而不是分钟。 – Leo 2016-03-08 18:15:34
谢谢Damith:D – 2016-10-10 19:45:47
尝试......
样品历元时间戳是1414492391238
方法:
public static String GetHumanReadableDate(long epochSec, String dateFormatStr) {
Date date = new Date(epochSec * 1000);
SimpleDateFormat format = new SimpleDateFormat(dateFormatStr,
Locale.getDefault());
return format.format(date);
}
可用性:
long timestamp = Long.parseLong(engTime)/1000;
String engTime_ = GetHumanReadableDate(timestamp, "dd-MM-yyyy HH:mm:ss aa");
结果:
28-10-2014 16时03分11秒时
快乐
这个答案有3个问题。这个答案忽略了时区的关键问题。此答案使用与问题中所要求的格式不同的格式。这个答案基本上重复了三个月前发布的其他答案。我在这里看不到任何增值。 – 2014-10-28 23:17:37
基于以前的答案,同时通过系统默认Locale
和TimeZone
编码...
String epochToIso8601(long time) {
String format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
sdf.setTimeZone(TimeZone.getDefault());
return sdf.format(new Date(time * 1000));
}
试试这个
Date date = new Date(1476126532838L);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String formatted = format.format(date);
format.setTimeZone(TimeZone.getTimeZone("Asia/Colombo"));//your zone
formatted = format.format(date);
System.out.println(formatted);
我已经打印出正确格式的字符串,但它打印出今天日期为1970年7月16日的错误日期。使用的长度是1332026487. – nexus490 2012-03-17 23:22:55
这是因为您可能需要在构造函数中传递参数。您可以在Jav API中找到所有可能的显示方式 – Reinard 2012-03-17 23:28:12
本答案是:打印*分*而不是*月*;忽视文化的选择(这非常重要);忽略时区的选择(这非常重要)。你真的必须考虑这些事情 - 没有任何帮助(海事组织)忽略它们。 – 2012-03-17 23:39:42