0

I have a MySQL table , some columns and one of them is of DATE type. Now , in Java i have fields for columns , but when i get to date i just don't know what to do ...

package com.keeptrack.model;
public class Racun {
    int id;
    String uplatilac;
    String svrha_uplate;
    String primalac;
    int sifra_placanja;
    String valuta;
    Double iznos;
    Long racun_primaoca;
    int model;
    String poziv_na_broj;
    ?????? date;
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126

1 Answers1

4

java.time.LocalDate

MySQL's DATE type is used for values with a date part but no time part, so you're better off to use the LocalDate API.

Another variant is the LocalDateTime API. Use this if you want to represent a date as well as a time i.e. MySQL's DATETIME type would require LocalDateTime object to represent it on the client side (Java program).

As an aside, avoid exposing fields to the outside world. make them all private to enforce encapsulation and only provide getters and setters where required.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • OK ty , i tried LocalDate and it worked , but later in the program i had a PreparedStatement ps ........ and when i try ps.setDate (10,r.date); it says that LocalDate cannot be converted into Date so i used just Date and imported java.sql.Date; – Aleksandar Stojanovic Dec 31 '17 at 01:52
  • 1
    @AleksandarStojanovic Using the java.time in JDBC requires a JDBC driver compliant with JDBC 4.2 or later. `myPreparedStatement.setObject( … , myLocalDate );` for a MySQL `DATE` column. – Basil Bourque Dec 31 '17 at 03:18