0

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. 

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.

I have just tried this:

String date =rs.getString(10); 
                ((JTextField)fieldClose.getDateEditor().getUiComponent()).setText(date);

returns:

2013-05-22 // comes in red line, meaning jDatechooser doesnt recognise it

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Hoody
  • 2,942
  • 5
  • 28
  • 32
  • there are two ways have to set SimpleDateFormat or setLocale, both are implemented in this Java Swing JCalendar – mKorbel Jan 09 '13 at 15:34

3 Answers3

1

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);
muffy
  • 429
  • 3
  • 7
0
  1. either you can use SimpleDateFormat in java
  2. or use DATE_FORMAT('ColumnName', "Date Pattern") in Mysql SELECT query.
0

You can use String.format. Let me help you with the code

Date o = jDateChooser1.getDate();   //Import java.util not java.sql
String x = String.format("%1$ty-%1$tm-%1$td",o);   
Malwaregeek
  • 2,274
  • 3
  • 15
  • 18