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

The above is used to extract the DATE value from database to set it textfield. When the date was inserted initially, it was of com.toedter.calendar.JDateChooser(); type. But having retrieved it, I cannot properly set it on the field. I will support this with picture below for you understanding:

It comes in Red:

enter image description here

but ideally should be recognised and date colour should be black as below, but this has to be done manually...how can i fix this so it is automatically black.

Recognised date

Catalina Island
  • 7,027
  • 2
  • 23
  • 42
Hoody
  • 2,942
  • 5
  • 28
  • 32

2 Answers2

1

again JCalendar - setting date correctly in Java using correct format, notice I'm never used packaged JCalendar in jar File, always code source

  1. set setDateFormat for concrete instance

    a) cal.setDateFormat(new SimpleDateFormat("dd.MM.yyyy"));

    b) cal.setLocale(Locale.FRANCE);//for example

  2. from JDBC Statement to return getDate(10) (instad of rs.getString(10);), this is valid java.util.Date instance for method setDate() in JCalendar

  3. Editor is derived JSpinner, there you can to use and set editor.getTextField().setXxx

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

The easiest way is to use the SimpleDateFormat class:

String dateString = rs.getString(10); 
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = inputFormat.parse(dateString);
SimpleDateFormat outputFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
((JTextField)fieldClose.getDateEditor().getUiComponent()).setText(outputFormat.format(date));

Simply by changing the Locale value, you'll change the language in which the three letter abbreviation for month will be represented.
reference for SimpleDateFormat methods:

linski
  • 5,046
  • 3
  • 22
  • 35
  • @linsky first its displaying green as opposed to red from before, the format is now recognised which is good. However, I am not sure how to save the date using pst.setDate(7, fieldClose.....) in format` yyyy-MM-dd` exactly like this as opposed to dd-mm-yyyy – Hoody Jan 10 '13 at 13:40
  • @ali if you can't store it in that format, then reformat it using the above procedure, and store it. though it would be simpler to store it in the dd-MMM-yyyy format in the first place – linski Jan 10 '13 at 13:45
  • not to use built_in methods implemented in API [xxx.setDateFormatString()](http://www.toedter.com/en/jcalendar/api/com/toedter/calendar/JDateChooser.html#setDateFormatString%28java.lang.String%29) or [setLocale in the JCalendar the constuctor](http://www.toedter.com/en/jcalendar/api/com/toedter/calendar/JCalendar.html#JCalendar%28java.util.Locale,%20boolean%29) – mKorbel Jan 10 '13 at 13:45
  • @mKorbel I understood that he made his custom control and needs to reformat the date. – linski Jan 10 '13 at 13:53
  • @ali if you are using some library components then by all means use that component's methods as mKorbel described. no point in reinventing the wheel. – linski Jan 10 '13 at 13:54
  • I am using Sqlite, and there are date format restriction. I want to save it as yyyy-MM-dd, but not sure what would follow from linski's answer above i.e. what should i put in pst.setString(7, fieldClose....) – Hoody Jan 10 '13 at 13:56
  • @Mumin Ali what please, returns Date, DateTime, Timestampt, from all Date & Time and /or Timestmapt you can to returns value for java.util.Date(), don't to parse, let it for implemented settings in JCalandar described at 5th. comments above, have to accepting Date instance and setting in GUI for dipslaying (sure by setting proper parameter/s), both are different Object, let it be use that only proper way – mKorbel Jan 10 '13 at 14:02
  • http://stackoverflow.com/questions/14259731/jcaladnder-inserting-date-into-sqlite-in-yyyy-mm-dd – Hoody Jan 10 '13 at 14:07
  • @mKorbel I dont understand, example code to fully get picture? – Hoody Jan 10 '13 at 14:10
  • @Mumin Ali please how is declared `column in database` where is stored `Datum` for `JCalendar`, id there `Date`, `DateTime` or `Timestapmt`, then from `Resultset` to load only `getDate` or g`etTimestampt(have to convet to java.util.Date)`, put this value directly to `JCalendar.setDate(date_taken_from_resultset)`, nothing else, simple and easy, this is advantage of JCalendar – mKorbel Jan 10 '13 at 14:15
  • @mKorbel he posted a [new question](http://stackoverflow.com/questions/14259731/jcaladnder-inserting-date-into-sqlite-in-yyyy-mm-dd) in the 3rd comment above, perhaps you should answer him there ? :) – linski Jan 10 '13 at 14:22
  • @mKorbel can you show me an example of how to code this in the new question page – Hoody Jan 10 '13 at 14:29