new Date() vs Calendar.getInstance().getTime()

博客对比了不同时间处理方式的性能,指出直接处理长时间戳效率最高,某对象性能次之,而处理日期时间相对较慢且复杂。建议在应用中主要处理长时间戳或特定对象,仅在必要时进行日期时间计算或格式化,大量处理时可考虑用Joda Time。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

System.currentTimeMillis()

vs.

new Date()

vs.

Calendar.getInstance().getTime()

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.).

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.

转载于:https://www.cnblogs.com/kakaisgood/p/11165057.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值