java java中如何将字符串转换为日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15945428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:22:57  来源:igfitidea点击:

How to convert String to Date in java

javastringdate-formatsimpledateformat

提问by R J.

I have a string something like date month year(31 08 2012) . I want to convert it to date format.

我有一个类似于 date month year(31 08 2012) 的字符串。我想将其转换为日期格式。

String string = "31082012";
Date date = new SimpleDateFormat("DDMMYYYY").parse(string);
System.out.println(date);

回答by PermGenError

Firstly, Your format is wrong, it should be ddMMyyyy

首先,你的格式是错误的,应该是 ddMMyyyy

Date date = new SimpleDateFormat("ddMMyyyy").parse(string);