SimpleDateFormat 返回错误的时间值 (00:00:00)
SimpleDateFormat returning wrong time value ( 00:00:00)
我在使用 SimpleDateFormat 组件时遇到问题。
我有一个日期作为 DateTime 存储在我的数据库中,我想在我的应用程序中获取这个日期时间的值。
我正在使用 SimpleDateFormat 来执行此操作,但问题是它总是 returns 我 00:00:00 作为时间。虽然返回日期很好。
所以我正在做如下:
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);
}
这是我获取日期的方法:
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;
}
在我的数据库中,日期是:
2015-09-03 16:13:09
我从 System.out.println(datep)
得到的输出是
03.09 - 00:00:00
我不知道为什么它没有正确返回我的时间..
如果您从 ResultSet, use getTimestamp method, not getDate. See JDBC ResultSet getDate losing precision
加载结果
如果您正在使用 java.sql.Date,您将丢失有关时间的信息。
java.sql.Date corresponds to SQL DATE which means it stores years,
months and days while hour, minute, second and millisecond are
ignored.
将代码更改为:
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:我建议您将 TimeStamt 以毫秒为单位存储在数据库中。
我在使用 SimpleDateFormat 组件时遇到问题。
我有一个日期作为 DateTime 存储在我的数据库中,我想在我的应用程序中获取这个日期时间的值。
我正在使用 SimpleDateFormat 来执行此操作,但问题是它总是 returns 我 00:00:00 作为时间。虽然返回日期很好。
所以我正在做如下:
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);
}
这是我获取日期的方法:
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;
}
在我的数据库中,日期是:
2015-09-03 16:13:09
我从 System.out.println(datep)
得到的输出是
03.09 - 00:00:00
我不知道为什么它没有正确返回我的时间..
如果您从 ResultSet, use getTimestamp method, not getDate. See JDBC ResultSet getDate losing precision
加载结果如果您正在使用 java.sql.Date,您将丢失有关时间的信息。
java.sql.Date corresponds to SQL DATE which means it stores years, months and days while hour, minute, second and millisecond are ignored.
将代码更改为:
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:我建议您将 TimeStamt 以毫秒为单位存储在数据库中。