无法从Java Double转换为Java Date [英] Cannot convert from java Double to java Date

查看:115
本文介绍了无法从Java Double转换为Java Date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将double转换为以下格式的Java日期对象时遇到问题:yyyy-MM-dd HH:mm:ss

I am having an issue with converting a double to a Java date object in the format: yyyy-MM-dd HH:mm:ss

我试图将这个double值转换为long值,然后实例化带有结果的Date对象,但是我收到错误消息,说我无法从double转换为long。

I have tried to convert this double value to a long value then instanciate a Date object with the result, but I got errors saying that I cannot convert from double to long.

我的时间戳的double值格式如下:

my timestamp double values are in this format:

1.511554592277516E9

1.511554592277516E9

有人可以帮助我解决此问题,谢谢。

Can anybody help me with this issue please, thanks in advance.

推荐答案

假设此浮点值是格林尼治标准时间1970年1月1日0:00格林尼治标准时间之后的几秒钟,它将提供一个转换为具有该偏移量的 LocalDateTime

Assuming that this floating point value is seconds past the Unix Epoch of 1 Jan 1970 0:00 GMT, this will provide a conversion to a LocalDateTime with that offset:

LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(
                Double.valueOf(1.511554592277516E9).longValue(), 0, ZoneOffset.UTC);
System.out.println(localDateTime);

我将其转换为 Date 作为读者的练习。

I leave converting this to a Date as an exercise for the reader.

这篇关于无法从Java Double转换为Java Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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