将字符串日期转换为java.sql.Date [英] convert string date to java.sql.Date
本文介绍了将字符串日期转换为java.sql.Date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以将字符串
20110210
转换为 java.sql.Date
2011 -02-10?
我试过 SimpleDateFormat
我得到 java.text .ParseException:Unparseable date:20110210
我做错了什么?
<我有
新的SimpleDateFormat(yyyy-MM-dd)
而不是
新的SimpleDateFormat(yyyyMMdd)
解决方案
这对我有用而不会抛出异常:
package com.sandbox;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Sandbox {
public static void main(String [] args)throws ParseException {
SimpleDateFormat format = new SimpleDateFormat(yyyyMMdd);
解析日期= format.parse(20110210);
java.sql.Date sql = new java.sql.Date(parsed.getTime());
}
}
Is it possible to convert string
"20110210"
to a java.sql.Date
2011-02-10?
I've tried SimpleDateFormat
and I get java.text.ParseException: Unparseable date: "20110210"
What am I doing wrong?
i had new SimpleDateFormat("yyyy-MM-dd") instead of new SimpleDateFormat("yyyyMMdd")
解决方案
This works for me without throwing an exception:
package com.sandbox;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Sandbox {
public static void main(String[] args) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Date parsed = format.parse("20110210");
java.sql.Date sql = new java.sql.Date(parsed.getTime());
}
}
这篇关于将字符串日期转换为java.sql.Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文