将字符串转换为日期java错误“类型不兼容”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21359312/
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
convert string to date java error "incompatible types"
提问by ayoubitou
I success to get my date from JDateChooser into a string with this line:
我成功地将我的日期从 JDateChooser 转换为带有以下行的字符串:
String d1 = ((JTextField)jDateChooser1.getDateEditor().getUiComponent()).getText();
I want to know if there is any method to get a date from a JDateChooser ?
我想知道是否有任何方法可以从 JDateChooser 获取日期?
I tried to convert this string into a date , and i have an error in the third line .
我试图将此字符串转换为日期,但在第三行中出现错误。
"incompatible types : java.util.date cannot be converted to java.sql.Date "
“不兼容的类型:java.util.date 无法转换为 java.sql.Date”
String s = "2011-07-08";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(s);
i have to import the java.util.Date , But i have another error in the same line
我必须导入 java.util.Date ,但我在同一行还有另一个错误
unreported exception ParseException ; must be caught or declared to be thrown
未报告的异常 ParseException ;必须被捕获或声明被抛出
回答by Sotirios Delimanolis
You've imported
您已导入
java.sql.Date
instead of
代替
java.util.Date
Change the import
statement to the appropriate type, ie. java.util.Date
.
将import
语句更改为适当的类型,即。java.util.Date
.
回答by Leo
try
尝试
String s = "2011-07-08";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = sdf.parse(s);