从Java日历迁移到Joda日期时间 [英] Migrating from Java Calendar to Joda Date Time

查看:117
本文介绍了从Java日历迁移到Joda日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前,当我第一次设计股票应用程序相关软件时,我决定使用 java.util.Date 来表示股票的日期/时间信息。

Previously, when I first design a stock application related software, I decide to use java.util.Date to represent date/time information of a stock.

后来,我意识到 java.util.Date 中的大部分方法都已弃用。因此,很快,我重构我的所有代码以使用 java.util.Calendar

Later, I realize most of the methods in java.util.Date is deprecated. Hence, very soon, I refactor all my code to make use of java.util.Calendar

但是,那里是我遇到的2个缺点。

However, there is 2 shortcomings I encounter.


  1. 构造 java.util.Calendar 比较慢比 java.util.Date

  2. 股票的访问者getCalendar方法中class,我需要克隆一个副本,因为 Calendar 是一个可变类

  1. Construct java.util.Calendar is comparative slower than java.util.Date
  2. Within the accessors getCalendar method of Stock class, I need to clone a copy, as Calendar is a mutable class

这是 Stock.java

最近,我发现 Joda-Time 。我通过创建1,000,000 java.util.Date java.util.Calendar 和<$ c进行以下基准测试$ C> org.joda.time.DateTime 。在实例化期间,我发现 org.joda.time.DateTime 的性能优于 java.util.Calendar

Recently, I discover Joda-Time. I do the following benchmarking, by creating 1,000,000 java.util.Date, java.util.Calendar and org.joda.time.DateTime. I found org.joda.time.DateTime performs better than java.util.Calendar, during instantiation.

这是基准结果。

此实例化速度很重要,特别是会创建很多股票实例,以表示股票的长期价格历史。

This instantiation speed is important, especially many instance of Stocks will be created, to represent a long price history of a stock.

您认为从Java Calendar迁移到Joda Date Time是否值得获得应用程序速度性能?有什么陷阱我需要注意吗?

Do you think is it worth to migrate from Java Calendar to Joda Date Time, to gain application speed performance? Is there any trap I need to pay attention to?

推荐答案

注意 java.util.Date 也是可变的 - 所以如果现在你使用的是 Calendar ,那么使用 Date <会出现问题/ code>也是。也就是说,使用Joda Time 绝对值得做的只是因为它是一个更好,更好的API。

Note that java.util.Date is mutable too - so if it's a problem now you're using Calendar, it would have been a problem using Date too. That said, using Joda Time is definitely worth doing just because it's a much, much better API.

你有多么确定性能实际上在您的特定应用中存在问题?你说会创建股票的很多实例......有多少?你有没有描述过它?我不希望它在大多数情况下实际上有显着差异。从您的基准测试图中看,您实际测量的内容并不明显。

How certain are you that performance is actually an issue in your particular app? You say there will be "many instances" of Stock created... how many? Have you profiled it? I wouldn't expect it to actually make a significant difference in most situations. It's not obvious from your benchmarking graph what you're actually measuring.

移动到Joda Time一般来说是一个好主意,但我会测量性能以查看多少真正给你带来的差异。

Moving to Joda Time is a good idea in general, but I would measure the performance to see how much difference it really makes for you.

这篇关于从Java日历迁移到Joda日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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