問題描述
嗯,我有一個使用 Date
的詳細信息,因為我從我的數據庫和變量fecha"(日期)中獲取了一個我正在獲取的同一對象的對象java.sql.Timestamp
,所以格式是毫秒,但我不希望毫秒出現.所以我需要將我從數據庫接收到的日期格式化為沒有毫秒的新日期.
Well, I'm having a detail using a Date
, because I'm getting an Object from my DataBase and in the variable "fecha" (date) from that same object I'm getting a java.sql.Timestamp
, so the formatt is with miliseconds, but i don't want the miliseconds to appear. So i need to formatt the date that I'm receiving from my DB to a new date that doesn't have miliseconds.
這是 Factura 的對象:
This is the object Factura:
public class Factura implements java.io.Serializable {
private FacturaId id;
...
private boolean activo;
private Date fecha;
}
在映射到數據庫的 xml 中,我有這個變量fecha"的代碼:
In the xml that is mapped to the DB I have this code for that variable "fecha":
<property name="fecha" type="timestamp">
<column length="19" name="fecha" not-null="true"/>
</property>
在數據庫中該列是 fecha DATETIME
.
當我從我的數據庫中獲得一個對象 Factura
時,我得到了這種日期 2013-10-10 10:49:29.0
但我希望它沒有.0
(毫秒).
And when I get an object Factura
from my DB I'm getting this kind of date 2013-10-10 10:49:29.0
but I want it without the .0
(miliseconds).
我試過這個(factura
是 Factura
對象):
I've tried this (factura
is the Factura
object):
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date fechaNueva = null;
String fechaStr = factura.getFecha().toString();
int tama?o = fechaStr.length()-2;
fechaStr = fechaStr.substring(0, tama?o); //I get a string without the miliseconds
fechaNueva = format.parse(fechaStr);
} catch(ParseException ex) {
...
}
但是 fechaNueva
給了我 Thu Oct 10 10:49:29 CDT 2013
而我只想要 2013-10-10 10:49:29
,你能幫幫我嗎?
But fechaNueva
is giving me Thu Oct 10 10:49:29 CDT 2013
and I only want 2013-10-10 10:49:29
, can you help me, please?
非常感謝,提前.
推薦答案
您根本不需要使用子字符串,因為您的 format
不包含該信息.
You do not need to use substring at all since your format
doesn't hold that info.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String fechaStr = "2013-10-10 10:49:29.10000";
Date fechaNueva = format.parse(fechaStr);
System.out.println(format.format(fechaNueva)); // Prints 2013-10-10 10:49:29
這篇關于如何將 java.sql.Timestamp(yyyy-MM-dd HH:mm:ss.S) 格式化為日期(yyyy-MM-dd HH:mm:ss)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!