将java日期转换为Sql时间戳

时间:2022-06-06 17:01:22

I am trying to insert java.util.Date after converting it to java.sql.Timestamp and I am using the following snippet:

我正在尝试插入java.util。将其转换为java.sql之后的日期。时间戳和我正在使用以下代码片段:

java.util.Date utilDate = new java.util.Date();
java.sql.Timestamp sq = new java.sql.Timestamp(utilDate.getTime());

But this is giving me sq as 2014-04-04 13:30:17.533

但这给了我一个sq - as, 2014-04-04 13:30:17.533。

Is there any way to get the output without milliseconds?

有没有一种方法可以在没有毫秒的情况下得到输出?

5 个解决方案

#1


33  

You can cut off the milliseconds using a Calendar:

你可以使用日历缩短毫秒数:

java.util.Date utilDate = new java.util.Date();
Calendar cal = Calendar.getInstance();
cal.setTime(utilDate);
cal.set(Calendar.MILLISECOND, 0);
System.out.println(new java.sql.Timestamp(utilDate.getTime()));
System.out.println(new java.sql.Timestamp(cal.getTimeInMillis()));

Output:

输出:

2014-04-04 10:10:17.78
2014-04-04 10:10:17.0

#2


8  

Take a look at SimpleDateFormat:

看一下SimpleDateFormat:

java.util.Date utilDate = new java.util.Date();
java.sql.Timestamp sq = new java.sql.Timestamp(utilDate.getTime());  

SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
System.out.println(sdf.format(sq));

#3


8  

The problem is with the way you are printing the Time data

问题是你打印时间数据的方式

java.util.Date utilDate = new java.util.Date();
java.sql.Timestamp sq = new java.sql.Timestamp(utilDate.getTime());
System.out.println(sa); //this will print the milliseconds as the toString() has been written in that format

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(timestamp)); //this will print without ms

#4


2  

I suggest using DateUtils from apache.commons library.

我建议使用apache.commons图书馆的数据集。

long millis = DateUtils.truncate(utilDate, Calendar.MILISECONDS).getTime();
java.sql.Timestamp sq = new java.sql.Timestamp(millis );

#5


1  

java.util.Date utilDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
System.out.println("utilDate:" + utilDate);
System.out.println("sqlDate:" + sqlDate);

This gives me the following output:

这给了我以下输出:

 utilDate:Fri Apr 04 12:07:37 MSK 2014
 sqlDate:2014-04-04

#1


33  

You can cut off the milliseconds using a Calendar:

你可以使用日历缩短毫秒数:

java.util.Date utilDate = new java.util.Date();
Calendar cal = Calendar.getInstance();
cal.setTime(utilDate);
cal.set(Calendar.MILLISECOND, 0);
System.out.println(new java.sql.Timestamp(utilDate.getTime()));
System.out.println(new java.sql.Timestamp(cal.getTimeInMillis()));

Output:

输出:

2014-04-04 10:10:17.78
2014-04-04 10:10:17.0

#2


8  

Take a look at SimpleDateFormat:

看一下SimpleDateFormat:

java.util.Date utilDate = new java.util.Date();
java.sql.Timestamp sq = new java.sql.Timestamp(utilDate.getTime());  

SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
System.out.println(sdf.format(sq));

#3


8  

The problem is with the way you are printing the Time data

问题是你打印时间数据的方式

java.util.Date utilDate = new java.util.Date();
java.sql.Timestamp sq = new java.sql.Timestamp(utilDate.getTime());
System.out.println(sa); //this will print the milliseconds as the toString() has been written in that format

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(timestamp)); //this will print without ms

#4


2  

I suggest using DateUtils from apache.commons library.

我建议使用apache.commons图书馆的数据集。

long millis = DateUtils.truncate(utilDate, Calendar.MILISECONDS).getTime();
java.sql.Timestamp sq = new java.sql.Timestamp(millis );

#5


1  

java.util.Date utilDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
System.out.println("utilDate:" + utilDate);
System.out.println("sqlDate:" + sqlDate);

This gives me the following output:

这给了我以下输出:

 utilDate:Fri Apr 04 12:07:37 MSK 2014
 sqlDate:2014-04-04