在Java中解析YYYYMMdd日期的最快方式 [英] Fastest way to parse a YYYYMMdd date in Java

查看:510
本文介绍了在Java中解析YYYYMMdd日期的最快方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解析YYYYMMdd日期时,例如20120405为2012年4月5日,最快的方法是什么?

  int year = Integer.parseInt(dateString.substring(0,4 )); 
int month = Integer.parseInt(dateString.substring(4,6));
int day = Integer.parseInt(dateString.substring(6));

vs。

 code> int date = Integer.parseInt(dateString)
year = date / 10000;
month =(date%10000)/ 100;
day = date%100;

mod 10000为一个月将是因为mod 10000导致MMdd,结果/ 100是MM

在第一个例子中,我们做3个字符串操作和3个解析到int,在第二个例子中,我们通过模数来做很多事情。



什么是更快?有更快的方法吗?

解决方案

如下所示,当您查看日期处理的性能是相关的数百万次的迭代。相反,您应该选择易于阅读和维护的解决方案。



尽管您可以使用 SimpleDateFormat 不可重入,所以应该避免。最好的解决方案是使用伟大的Joda时间类:

  private static final DateTimeFormatter DATE_FORMATTER = new DateTimeFormatterBuilder()
.appendYear(4,4).appendMonthOfYear(2).appendDayOfMonth(2).toFormatter();
...
日期date = DATE_FORMATTER.parseDateTime(dateOfBirth).toDate();






如果我们在谈论你的数学功能,首先要指出的是,我已经修正了你的数学代码中的错误。这是手工操作的问题。也就是说,处理字符串的次数将是最快的。快速测试运行显示:

  year = Integer.parseInt(dateString.substring(0,4)); 
month = Integer.parseInt(dateString.substring(4,6));
day = Integer.parseInt(dateString.substring(6));

花费约800毫秒:

  int date = Integer.parseInt(dateString); 
year = date / 10000;
month =(date%10000)/ 100;
day = date%100;
总+ =年+月+日

花费约400毫秒



然而...再次... ,您需要考虑到这是在 1000万次迭代之后。这是过早优化的完美例子。我会选择最可读和最容易维护的。这就是为什么Joda时间的答案是最好的。


When parsing a YYYYMMdd date, e.g. 20120405 for 5th April 2012, what is the fastest method?

int year = Integer.parseInt(dateString.substring(0, 4));
int month = Integer.parseInt(dateString.substring(4, 6));
int day = Integer.parseInt(dateString.substring(6));

vs.

int date = Integer.parseInt(dateString)
year = date / 10000;
month = (date % 10000) / 100; 
day = date % 100;

mod 10000 for month would be because mod 10000 results in MMdd and the result / 100 is MM

In the first example we do 3 String operations and 3 "parse to int", in the second example we do many things via modulo.

What is faster? Is there an even faster method?

解决方案

As you see below, the performance of the date processing only is relevant when you look at millions of iterations. Instead, you should choose a solution that is easy to read and maintain.

Although you could use SimpleDateFormat, it is not reentrant so should be avoided. The best solution is to use the great Joda time classes:

private static final DateTimeFormatter DATE_FORMATTER = new DateTimeFormatterBuilder()
     .appendYear(4,4).appendMonthOfYear(2).appendDayOfMonth(2).toFormatter();
...
Date date = DATE_FORMATTER.parseDateTime(dateOfBirth).toDate();


If we are talking about your math functions, the first thing to point out is that there were bugs in your math code that I've fixed. That's the problem with doing by hand. That said, the ones that process the string once will be the fastest. A quick test run shows that:

year = Integer.parseInt(dateString.substring(0, 4));
month = Integer.parseInt(dateString.substring(4, 6));
day = Integer.parseInt(dateString.substring(6));

Takes ~800ms while:

int date = Integer.parseInt(dateString);
year = date / 10000;
month = (date % 10000) / 100; 
day = date % 100;
total += year + month + day;

Takes ~400ms.

However ... again... you need to take into account that this is after 10 million iterations. This is a perfect example of premature optimization. I'd choose the one that is the most readable and the easiest to maintain. That's why the Joda time answer is the best.

这篇关于在Java中解析YYYYMMdd日期的最快方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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