日期 java 中的 java.lang.IllegalArgumentException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29004460/
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
java.lang.IllegalArgumentException in Date java
提问by amol saxena
Getting error
获取错误
java.lang.IllegalArgumentException at java.util.Date.parse(Unknown Source) at java.util.Date.(Unknown Source)
java.lang.IllegalArgumentException at java.util.Date.parse(Unknown Source) at java.util.Date.(Unknown Source)
Here is my java code
这是我的java代码
import java.util.Date;
public class DateCheck {
public static void main(String[] args) {
String dDate="Sat Apr 11 12:16:44 IST 2015";
Date cDate=null;
cDate = new Date(dDate);
}
}
I am using java 1.6
我正在使用 Java 1.6
回答by Davide Lorenzo MARINO
You have to use the method parse()
of an implementation class of DateFormat
.
The simplest way is using SimpleDateFormat
.
您必须使用 的parse()
实现类的方法DateFormat
。最简单的方法是使用SimpleDateFormat
.
String dDate="Sat Apr 11 12:16:44 IST 2015";
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date cDate = df.parse(dDate);
回答by The Well
Try this code:
试试这个代码:
String dDate="Sat Apr 11 12:16:44 IST 2015"
DateFormat formatter = new SimpleDateFormat("d-MMM-yyyy,HH:mm:ss aaa");
Date date = formatter.parse(dDate);
System.out.println(date);