System.currentTimeMillis()vs. new Date()vs. Calendar.getInstance()。getTime() [英] System.currentTimeMillis() vs. new Date() vs. Calendar.getInstance().getTime()

查看:134
本文介绍了System.currentTimeMillis()vs. new Date()vs. Calendar.getInstance()。getTime()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,使用

System.currentTimeMillis()

vs。

new Date()

vs。

Calendar.getInstance().getTime()

根据我的理解,System.currentTimeMillis()是最有效的。但是,在大多数应用程序中,该长值需要转换为Date或某些类似的对象,以对人类做任何有意义的事情。

As I understand it, System.currentTimeMillis() is the most efficient. However, in most applications, that long value would need to be converted to a Date or some similar object to do anything meaningful to humans.

推荐答案

System.currentTimeMillis()显然是最有效,因为它甚至不创建对象,但 new Date()实际上只是一个很薄的包装,所以不远处。 日历,另一方面,是相对缓慢和非常复杂,因为它必须处理相当复杂和日期和时间固有的所有怪异(闰年,日光节约,时区等)。

System.currentTimeMillis() is obviously the most efficient since it does not even create an object, but new Date() is really just a thin wrapper about a long, so it is not far behind. Calendar, on the other hand, is relatively slow and very complex, since it has to deal with the considerably complexity and all the oddities that are inherent to dates and times (leap years, daylight savings, timezones, etc.).

一般来说,只处理长时间戳或 Date 对象,并且只有在实际需要执行日期/时间计算时才使用日历,或者格式化日期以显示给用户。如果你必须做很多事情,使用 Joda Time 可能是一个好主意,为了更清洁的界面和更好的性能。

It's generally a good idea to deal only with long timestamps or Date objects within your application, and only use Calendar when you actually need to perform date/time calculations, or to format dates for displaying them to the user. If you have to do a lot of this, using Joda Time is probably a good idea, for the cleaner interface and better performance.

这篇关于System.currentTimeMillis()vs. new Date()vs. Calendar.getInstance()。getTime()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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