最快&格式化字符串的最有效方法

发布于 2024-12-03 16:39:39 字数 57 浏览 1 评论 0原文

在Java中将格式为“20110913”的字符串日期转换为“2011-09-13”的最快方法是什么?

What is the quickest way for converting a date which is a string with the format "20110913" to "2011-09-13" in Java.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帅的被狗咬 2024-12-10 16:39:39

使用java.text.DateFormat:

DateFormat inputFormat = new SimpleDateFormat("yyyyMMdd");
inputFormat.setLenient(false);
DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
Date inputDate = inputFormat.parse("20110913);
System.out.println(outputFormat.format(inputDate));

Use java.text.DateFormat:

DateFormat inputFormat = new SimpleDateFormat("yyyyMMdd");
inputFormat.setLenient(false);
DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
Date inputDate = inputFormat.parse("20110913);
System.out.println(outputFormat.format(inputDate));
祁梦 2024-12-10 16:39:39

我做了一些简单的分析并发现了一些有趣的结果。

public static String strcat(String ori){
    return ori.substring(0, 4) + '-' + ori.substring(4, 6) + '-' + ori.substring(6);
}

public static String sdf(String ori){
    try {
        SimpleDateFormat in = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd");
        Date temp = in.parse(ori);
        return out.format(temp);
    } catch (ParseException ex) {
        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

public static String sb(String ori){
    return new StringBuilder(10).append(ori.substring(0, 4)).append('-').append(ori.substring(4, 6)).append('-').append(ori.substring(6)).toString();
}

public static String nio(String ori){
    byte[] temp = ori.getBytes();
    ByteBuffer bb = ByteBuffer.allocate(temp.length + 2);
    bb.put(temp, 0, 4);
    byte hyphen = '-';
    bb.put(hyphen);
    bb.put(temp, 4, 2);
    bb.put(hyphen);
    bb.put(temp, 6, 2);
    return new String(bb.array());
}

public static String qd(String ori){
    char[] result = new char[10];
    result[4] = result[7] = '-';

    char[] temp = ori.toCharArray();
    System.arraycopy(temp, 0, result, 0, 4);
    System.arraycopy(temp, 4, result, 5, 2);
    System.arraycopy(temp, 6, result, 8, 2);
    return new String(result);
}

public static void main(String[] args) {
    String ori = "20110913";
    int rounds = 10000;
    for (int i = 0; i < rounds; i++) {
        qd(ori);
        nio(ori);
        sb(ori);
        sdf(ori);
        strcat(ori);
    }
}

通过上述几种方法,我运行了测试 3 次,结果(平均值)如下: -

sb      15.7ms
strcat  15.2ms
qd      27.2ms
nio     137.6ms
sdf     582ms

测试是使用 JDK 7 运行的。请注意,这不是一个广泛的分析,因为有很多优化可以完成(例如缓存 SimpleDateFormat、StringBuilder)。此外,此测试不是多线程的。所以,请配置您自己的程序!
ps strcat 比 sb 快不是我所期望的。我想编译器优化在这里起着很大的作用。

I did some simple profiling and found some interesting results.

public static String strcat(String ori){
    return ori.substring(0, 4) + '-' + ori.substring(4, 6) + '-' + ori.substring(6);
}

public static String sdf(String ori){
    try {
        SimpleDateFormat in = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd");
        Date temp = in.parse(ori);
        return out.format(temp);
    } catch (ParseException ex) {
        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

public static String sb(String ori){
    return new StringBuilder(10).append(ori.substring(0, 4)).append('-').append(ori.substring(4, 6)).append('-').append(ori.substring(6)).toString();
}

public static String nio(String ori){
    byte[] temp = ori.getBytes();
    ByteBuffer bb = ByteBuffer.allocate(temp.length + 2);
    bb.put(temp, 0, 4);
    byte hyphen = '-';
    bb.put(hyphen);
    bb.put(temp, 4, 2);
    bb.put(hyphen);
    bb.put(temp, 6, 2);
    return new String(bb.array());
}

public static String qd(String ori){
    char[] result = new char[10];
    result[4] = result[7] = '-';

    char[] temp = ori.toCharArray();
    System.arraycopy(temp, 0, result, 0, 4);
    System.arraycopy(temp, 4, result, 5, 2);
    System.arraycopy(temp, 6, result, 8, 2);
    return new String(result);
}

public static void main(String[] args) {
    String ori = "20110913";
    int rounds = 10000;
    for (int i = 0; i < rounds; i++) {
        qd(ori);
        nio(ori);
        sb(ori);
        sdf(ori);
        strcat(ori);
    }
}

With the above few methods, I ran the test three times and the results(averaged) are as follow:-

sb      15.7ms
strcat  15.2ms
qd      27.2ms
nio     137.6ms
sdf     582ms

The test is ran using JDK 7. Do take note that this is not an extensive profiling as there are a lot of optimization that can be done (e.g. caching the SimpleDateFormat, StringBuilder). Also, this test is not multithreaded. So, do profile your own program!
p.s. the strcat is faster than sb is not what I expected. I guess the compiler optimization plays a big role here.

一口甜 2024-12-10 16:39:39

使用 StringBuilder,附加子字符串,并在子字符串之间插入“-”字符。

Use a StringBuilder, append the substrings inserting '-' chars in between them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文