0

I'm having an issue using the SimpleDateFormat component.

I have a date stored in my database as a DateTime, and i would like to get the value of this datetime in my application.

I'm using a SimpleDateFormat in order to do this, but the problem is that it always returns me 00:00:00 as Time. The date is well returned though.

So i'm doing as follows :

private final static SimpleDateFormat ft = new SimpleDateFormat("dd.MM - HH:mm:ss");

public Push(int idp, String titrefr, String contenufr, String titreuk, String contenuuk, String pays, String marche, String type, Date datep, int isImportant, String  image) {

    super();

    this.idp = idp;
    this.titrefr = titrefr;
    this.contenufr= contenufr;
    this.titreuk = titreuk;
    this.contenuuk= contenuuk;
    this.pays = pays;
    this.marche = marche;
    this.type = type;
    this.datep = ft.format(datep);
    this.isImportant = isImportant;
    this.image = image;
    System.out.println(this.datep);
}

Here is the method where I get the date :

Modele.java:

public List<Push> getPushfr() {
    String queryPushfr = "SELECT idp,titrefr,contenufr,titreuk,contenuuk,pays,marche,type,datep,isImportant, image FROM push WHERE datep > DATE_SUB(CURRENT_DATE, INTERVAL 3 MONTH) ORDER BY datep DESC;";
    try {
        connexion = ConnexionBDD.getConnexion();
        PreparedStatement pstmt = connexion.prepareStatement(queryPushfr);
        resultat = pstmt.executeQuery(queryPushfr);
        while (resultat.next()) {
            int idp = resultat.getInt("idp");
            String titrefr = resultat.getString("titrefr");
            String contenufr = resultat.getString("contenufr");
            String titreuk = resultat.getString("titreuk");
            String contenuuk = resultat.getString("contenuuk");
            String pays = resultat.getString("pays");
            String marche = resultat.getString("marche");
            String type = resultat.getString("type");
            Date datep = resultat.getDate("datep");
            int isImportant = resultat.getInt("isImportant");
            String image = resultat.getString("image");
            this.pushfr.add(
                    new Push(idp, titrefr, contenufr, titreuk, contenuuk, pays, marche, type, datep, isImportant,image));
        }
    } catch (Exception ex) {
        System.err.println(ex.getMessage());
    }
    return pushfr;
}

In my database the date is :

2015-09-03 16:13:09

The output i get from my System.out.println(datep) is

03.09 - 00:00:00

I've no idea why it's not returning me the time properly..

2 Answers2

0

If you are loading results from a ResultSet, use getTimestamp method, not getDate. See JDBC ResultSet getDate losing precision

Community
  • 1
  • 1
chrisl08
  • 1,658
  • 1
  • 15
  • 24
0

If you are using java.sql.Date you will lose information about the time.

java.sql.Date corresponds to SQL DATE which means it stores years, months and days while hour, minute, second and millisecond are ignored.

Change code to:

public List<Push> getPushfr() {
    String queryPushfr = "SELECT idp,titrefr,contenufr,titreuk,contenuuk,pays,marche,type,datep,isImportant, image FROM push WHERE datep > DATE_SUB(CURRENT_DATE, INTERVAL 3 MONTH) ORDER BY datep DESC;";
    try {
        connexion = ConnexionBDD.getConnexion();
        PreparedStatement pstmt = connexion.prepareStatement(queryPushfr);
        resultat = pstmt.executeQuery(queryPushfr);
        while (resultat.next()) {
            int idp = resultat.getInt("idp");
            String titrefr = resultat.getString("titrefr");
            String contenufr = resultat.getString("contenufr");
            String titreuk = resultat.getString("titreuk");
            String contenuuk = resultat.getString("contenuuk");
            String pays = resultat.getString("pays");
            String marche = resultat.getString("marche");
            String type = resultat.getString("type");
            Date datep = resultat.getTimestamp("datep");
            int isImportant = resultat.getInt("isImportant");
            String image = resultat.getString("image");
            this.pushfr.add(
                    new Push(idp, titrefr, contenufr, titreuk, contenuuk, pays, marche, type, datep, isImportant,image));
        }
    } catch (Exception ex) {
        System.err.println(ex.getMessage());
    }
    return pushfr;
}

PD:I recomend you to store the TimeStamt in milisecons in the database.

David Herrero
  • 704
  • 5
  • 17