0

I have a JavaFX TableView representing job data that is populated by a MySQL database. All the data displays fine except two date columns, both of which are represented YYYY-MM-DD. What I would like to do is have this the other way around i.e DD-MM-YYYY.

+--------------+------------+
| Date Created |  Deadline  |
+--------------+------------+
| 2015-09-07   | 2015-09-14 |
| 2015-09-07   | 2015-09-14 |
+--------------+------------+

Would it be best to do this by modifying the MySQL query I use to populate the table: final String finalQuery = "SELECT * FROM kitJobs ORDER BY kitJobs.CaseNO ASC"; ? If so, how would I modify the query to get the desired date format?

vhu
  • 12,244
  • 11
  • 38
  • 48
jbanks
  • 163
  • 1
  • 6
  • 17
  • Are you storing the date information in text fields in the database? If so, I'd change *that* first, if at all possible. Then, change how the information is formatted within JavaFX - don't change the query. (It should be the UI that determines the format, not the data layer.) – Jon Skeet Sep 07 '15 at 09:27
  • @JonSkeet they are stored as date types in the database. How would I go about changing the formatting of the data from within JavaFX? – jbanks Sep 07 '15 at 09:29
  • See http://stackoverflow.com/questions/11412360/javafx-table-cell-formatting – Jon Skeet Sep 07 '15 at 09:38

3 Answers3

0

You can use SimpleDateFormat;

  String s;
  Format formatter;
  Date date = new Date();

  // 01/09/02
  formatter = new SimpleDateFormat("dd/MM/yyyy");
  s = formatter.format(date);
  System.out.println(s);
hurricane
  • 6,521
  • 2
  • 34
  • 44
  • The only issue I have with this solution is when do I apply the format to the date and how would it be applied to the JavaFX table column? I tried formatting after getting the date from the database but this didn't work. – jbanks Sep 07 '15 at 09:39
0

For future reference for anyone else seeking a similar solution I successfuly formatted my columns in the UI by doing the following;

deadlineDateCol.setCellValueFactory(
           Job -> {
              SimpleStringProperty property = new SimpleStringProperty();
              DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
              property.setValue(dateFormat.format(Job.getValue().getDeadlineDate()));
              return property;
           });
jbanks
  • 163
  • 1
  • 6
  • 17
0

This is what i did and i worked perfectly.

tbColDataMovt.setCellFactory((TableColumn<Auditoria, Timestamp> column) -> {
  return new TableCell<Auditoria, Timestamp>() {
     @Override
     protected void updateItem(Timestamp item, boolean empty) {
        super.updateItem(item, empty);
         if (item == null || empty) {
            setText(null);
         } 
         else {
            setText(item.toLocalDateTime().format(DateTimeFormatter.ofPattern("dd/MM/yyyy")));
         }
      }
   };
});
R3ing
  • 437
  • 3
  • 12