简体   繁体   English

为什么我的历元值无法获得正确的人类可读的日期和时间值?

[英]Why am I not getting correct human readable date and time value from my epoch value?

I want to bulk convert my epoch values into human readable date and time value. 我想将我的纪元值批量转换为人类可读的日期和时间值。 Taking a value 1342179766, I converted it on epoch-converter and got Fri, 13 Jul 2012 11:42:46 GMT as answer which I know is correct. 取值为1342179766,我在时代转换器上对其进行了转换,并得到格林尼治标准时间2012年7月13日星期五11:42:46作为星期五的答案,我知道这是正确的。 Now I used the java code provided on their site to convert it into date and time format but I am getting wrong answer. 现在,我使用他们网站上提供的Java代码将其转换为日期和时间格式,但答案却不正确。 Code: 码:

String date = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date (1342179766*1000));
System.out.println(date);

Output: 输出:

12/07/1969 09:40:02

I looked at other questions on stackoverflow but mostly this code is given everywhere. 我看了关于stackoverflow的其他问题,但大多数情况下此代码都给出了。 I found 2-3 other code but all of them are also giving me same output. 我发现了2-3个其他代码,但所有这些代码也都给了我相同的输出。

我猜您应该使用1342179766L*1000L而不是1342179766*1000

Your issue comes from the fact that Java is trying to do integer multiplication on 1342179766*1000 and then convert it into a long. 您的问题来自Java试图在1342179766*1000上进行整数乘法, 然后将其转换为long的事实。 If you explicitly make this a long, then it works: 如果您明确地将其加长,那么它将起作用:

String date = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date (1342179766L*1000));
System.out.println(date);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Excel 中将 Unix 纪元时间戳转换为人类可读的日期/时间? - How do I convert a Unix epoch timestamp into a human readable date/time in Excel? JComboBox中来自枚举的人类可读值 - Human Readable Value from Enum in JComboBox SimpleDateFormat 是否有格式字符串来获取毫秒日期时间值而不是人类可读的形式? - Is there a format string for SimpleDateFormat to get milliseconds date-time value instead of human-readable form? 使用Java将Unix时代转换为人类可读的日期不正确 - Incorrect date when converting unix epoch to human readable using Java 为什么我在第一次运行应用程序时得到默认值? - Why am I getting the default value the first time running the app? 为什么我在alertDialog.setMessage()上获得空值? - Why am i getting a null value on my alertDialog.setMessage()? 将字符串日期转换为纪元时间的意外值? - Unexpected value converting String date into epoch time? 将纪元时间戳转换为可读日期不起作用 - Converting epoch time stamp to readable date not working 当我从html表单传递值时,为什么在我的post api期间出现'Column not not null'的错误 - Why am I getting an error for 'Column cannot be null' during my post api when I am passing a value from a html form 我是从我的方法中返回正确的值,还是正确编写了它? - Am I returning the correct value from my method & am I writing it correctly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM