我正在做一项工作,我需要从 Java 中的字符串中获取年、月、日。该字符串是这样的:20170119T163048
其中 2017 是年,01 是月,19 是日,16:30:48 是时间。
我实现了如下代码:
public void convertStringToDate (String string) {
String dateInString = "";
SimpleDateFormat formatter = new SimpleDateFormat();
Date date = formatter.parse("20170119T163048");
System.out.println(date);
}
但是好像不起作用。环顾四周,我实际上发现了很多类似的答案,但没有一个真正适合我,所以......
最佳答案
您应始终引用文档。 Simple Date Format Documentation
public static void main(String[] args) throws Exception{
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
Date date = formatter.parse("20170119T163048");
System.out.println(date.toString());
}
关于java - Java中如何从字符串格式中获取年、月、日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41754857/