如何将 java.sql.Timestamp(yyyy-MM-dd HH:mm:ss.S) 格式化为日期(yyyy-MM-dd HH:mm:ss)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19544067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to format a java.sql.Timestamp(yyyy-MM-dd HH:mm:ss.S) to a date(yyyy-MM-dd HH:mm:ss)
提问by user2284257
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.
嗯,我有一个使用 a 的详细信息Date
,因为我从我的数据库中获取一个对象,并且在来自同一个对象的变量“fecha”(日期)中我得到了一个java.sql.Timestamp
,所以格式是毫秒,但我不希望毫秒出现。所以我需要将从我的数据库收到的日期格式化为一个没有毫秒的新日期。
This is the object Factura:
这是 Factura 对象:
public class Factura implements java.io.Serializable {
private FacturaId id;
...
private boolean activo;
private Date fecha;
}
In the xml that is mapped to the DB I have this code for that variable "fecha":
在映射到数据库的 xml 中,我有这个变量“fecha”的代码:
<property name="fecha" type="timestamp">
<column length="19" name="fecha" not-null="true"/>
</property>
In the Database that column is fecha DATETIME
.
在数据库中,该列是fecha DATETIME
.
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
从我的数据库中获取一个对象时,我得到了这种日期,2013-10-10 10:49:29.0
但我希望它没有.0
(毫秒)。
I've tried this (factura
is the Factura
object):
我试过这个(factura
是Factura
对象):
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) {
...
}
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?
但是fechaNueva
给我Thu Oct 10 10:49:29 CDT 2013
而我只想要2013-10-10 10:49:29
,你能帮帮我吗?
Thanks a lot, in advance.
非常感谢,提前。
采纳答案by Sajal Dutta
You do not need to use substring at all since your format
doesn't hold that info.
您根本不需要使用子字符串,因为您format
不保存该信息。
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
回答by Basil Bourque
A date-time object is not a String
日期时间对象不是字符串
The java.sql.Timestamp class has no format. Its toString method generatesa String with a format.
java.sql.Timestamp 类没有格式。它的 toString 方法生成一个带有格式的字符串。
Do not conflate a date-time object with a String that may represent its value. A date-time object can parse strings and generate strings but is not itself a string.
不要将日期时间对象与可能表示其值的字符串混为一谈。日期时间对象可以解析字符串并生成字符串,但它本身不是字符串。
java.time
时间
First convert from the troubled old legacy date-time classes to java.time classes. Use the new methods added to the old classes.
首先从麻烦的旧日期时间类转换为 java.time 类。使用添加到旧类的新方法。
Instant instant = mySqlDate.toInstant() ;
Lose the fraction of a second you don't want.
失去你不想要的那几分之一秒。
instant = instant.truncatedTo( ChronoUnit.Seconds );
Assign the time zone to adjust from UTC used by Instant.
分配时区以根据 Instant 使用的 UTC 进行调整。
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = instant.atZone( z );
Generate a String close to your desired output. Replace its T
in the middle with a SPACE.
生成一个接近您想要的输出的字符串。用T
空格替换它的中间。
DateTimeFormatter f = DateTimeFormatter.ISO_LOCAL_DATE_TIME ;
String output = zdt.format( f ).replace( "T" , " " );