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

查看:141
本文介绍了System.currentTimeMillis()与新的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 您的应用程序中的对象,并且只有在您实际需要执行日期/时间计算时才使用 Calendar ,或将用于显示给用户的日期格式化。如果你需要做很多,使用 Joda时间可能是一个好主意,为了更干净界面和更好的表现。

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()与新的Date()vs. Calendar.getInstance()。getTime()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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