JCalendar - 使用正确的格式在Java中正确设置日期 [英] JCalendar - setting date correctly in Java using correct format
问题描述
我正在使用 JCalendar
: http://www.toedter.com/en/jcalendar/
我使用JDateChooser在表单中设置日期并导入到 SqLite
格式为 yyyy-MM-dd
。当我尝试使用以下代码从数据库中检索相同格式的日期 yyyy-MM-dd
:
I am using the JCalendar
from here: http://www.toedter.com/en/jcalendar/
I have used the JDateChooser to set date in a form and import into SqLite
in the format yyyy-MM-dd
. WhenI try retrieving the date in the same format yyyy-MM-dd
from the database with this code:
Date date = rs.getDate(10);
fieldClose.setDate(date); //displays 01-Jan-1970 - I want (2013-01-08), one that is similar in database.
我看过stackoverflow,还有一些类似的帖子存在,但是没有显示格式的建议 yyyy-MM-dd
和考虑 jCalender
和SQLite,所以我不得不发布。任何想法如何实现类似于数据库的日期,以相同的格式插入和相同类型。
I have looked at stackoverflow, and some similar posts exists, but fails to show recommendations in terms of format yyyy-MM-dd
and consideration of jCalender
and SQLite so i am having to post this. any ideas how to achieve a date similar to database, in the same format it was inserted and of same type.
我刚刚尝试过:
String date =rs.getString(10);
((JTextField)fieldClose.getDateEditor().getUiComponent()).setText(date);
返回:
2013-05- 22 //进入红线,意思是jDatechooser不认识它
2013-05-22 // comes in red line, meaning jDatechooser doesnt recognise it
推荐答案
使用SimpleDateFormat,尝试这个例子
Use SimpleDateFormat, try this example
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date=null;
try{
date=sdf.parse(rs.getString(10));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fieldClose.setDate(date);
这篇关于JCalendar - 使用正确的格式在Java中正确设置日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!