简体   繁体   English

最快的字符串格式转换

[英]Fastest String Format conversion

I have the following string: 20140420000001 I want to format it to: 2014-04-20+00:00:01 我有以下字符串: 20140420000001我想将其格式化为: 2014-04-20+00:00:01 20140420000001

What is the fastest possible way to do this in Java? 用Java最快的方法是什么?

Anything faster than this? 有什么比这更快的吗?

String dt = "20140420000001";
System.out.println(dt.substring(0, 4)+"-"+dt.substring(4, 6)+"-"+dt.substring(6, 8)+"+"+dt.substring(8, 10)+":"+dt.substring(10, 12)+":"+dt.substring(12, 14));
    return new String(new char[] {
            s.charAt(0), s.charAt(1), s.charAt(2), s.charAt(3),
            '-', s.charAt(4), s.charAt(5),
            '-', s.charAt(6), s.charAt(7),
            '+', s.charAt(8), s.charAt(9),
            ':', s.charAt(10), s.charAt(11),
            ':', s.charAt(12), s.charAt(13)
    });

Not too pretty, but there is hardly anything faster in pure Java. 不太漂亮,但是纯Java几乎没有什么更快的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM