System.currentTimeMillis() 何时会溢出? [英] When will System.currentTimeMillis() overflow?

查看:23
本文介绍了System.currentTimeMillis() 何时会溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网络应用程序,它使用一个很长的时间戳来订购东西.我的 Web 应用程序后端恰好是用 Java 编写的,所以我正在使用:

long timestamp = System.currentTimeMillis();

这将在哪一年(大约)失败?我的意思是在某个时候, long 的范围会溢出,对吗?我们可能都死了很久,但我只是好奇.会不会再次像 y2k 一样?我可以做些什么来为此做准备?可笑,我知道,只是好奇!

解决方案

它会在

溢出

System.out.println(new Date(Long.MAX_VALUE));

哪个打印

Sun Aug 17 03:12:55 GMT-04:00 292278994

这是在超过 2.92 亿年之后的结果.我想说,同时有充足时间来发明解决方案.老实说,我不指望人性能幸存下来.与以小时为单位的

I have a web app which orders stuff using a timestamp, which is just a long. My web app backend happens to be written in java, so I am using:

long timestamp = System.currentTimeMillis();

what year (approximately) will this fail? I mean at some point, the range of a long is going to overflow, right? We may all be long-dead, but I'm just curious. Will it be like y2k all over again? What can I do to prepare for this? Ridiculous, I know, just curious!

解决方案

It will overflow at

System.out.println(new Date(Long.MAX_VALUE));

which prints

Sun Aug 17 03:12:55 GMT-04:00 292278994

That's thus after a bit more than 292 million years. I'd say, there's a plenty of time to invent a solution in the meanwhile. To be honest, I don't expect the humanhood to survive this. We exist only a few seconds as compared to the age of the world in hour scale and it won't take long.

这篇关于System.currentTimeMillis() 何时会溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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