1)使用正则表达式,模式
2)在String类中使用split函数
我尝试使用分割函数,我认为它不是一个好的编码实践。
像这样使用'_'迭代数组分割输入字符串
弦乐
START_READING_20140723_093539.jpg
Description_20140723_093539.jpg
Qunantity_20140723_183539.jpg
END_READING_20140723_093539.jpg
输入格式为:
yyyyMMdd_HHmmss
输出格式为:
23-07-2014 6:35:39PM
最佳答案
为什么不对整个内容使用joda time?
import joda.time.format.*;
import joda.time.*;
DateTimeFormatter inputFormat = DateTimeFormat.forPattern("yyyyMMdd'_'HHmmss");
LocalDateTime timestamp = inputFormat.parseLocalDateTime(theString);
DateTimeFormatter outputFormat = DateTimeFormat.forPattern("dd'-'MM'-'yyyy' 'hh':'mm':'ssa");
String output = outputFormat.print(timestamp);
如果您使用的是Java 8,则应改用java.time包。