以 java.sql.Date 格式获取当前日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18257648/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:46:56  来源:igfitidea点击:

Get the current date in java.sql.Date format

javajdbcjava.util.date

提问by giozh

I need to add the current date into a prepared statement of a JDBC call. I need to add the date in a format like yyyy/MM/dd.

我需要将当前日期添加到 JDBC 调用的准备好的语句中。我需要以类似的格式添加日期yyyy/MM/dd

I've try with

我试过

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Date date = new Date();
pstm.setDate(6, (java.sql.Date) date);

but I have this error:

但我有这个错误:

threw exception
java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date

Is there a way to obtain a java.sql.Dateobject with the same format?

有没有办法获得java.sql.Date相同格式的对象?

采纳答案by rgettman

A java.util.Dateis not a java.sql.Date. It's the other way around. A java.sql.Dateis a java.util.Date.

Ajava.util.Date不是java.sql.Date。正好相反。Ajava.sql.Date是一个java.util.Date

You'll need to convert it to a java.sql.Dateby using the constructor that takes a longthat a java.util.Datecan supply.

您需要java.sql.Date使用longajava.util.Date可以提供的构造函数将其转换为 a 。

java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

回答by kelevra88

Simply in one line:

只需在一行中:

java.sql.Date date = new java.sql.Date(Calendar.getInstance().getTime().getTime());

回答by Amala

new java.sql.Date(Calendar.getInstance().getTimeInMillis());

回答by duggu

You can achieve you goal with below ways :-

您可以通过以下方式实现您的目标:-

long millis=System.currentTimeMillis();  
java.sql.Date date=new java.sql.Date(millis);  

or

或者

// create a java calendar instance
Calendar calendar = Calendar.getInstance();

// get a java date (java.util.Date) from the Calendar instance.
// this java date will represent the current date, or "now".
java.util.Date currentDate = calendar.getTime();

// now, create a java.sql.Date from the java.util.Date
java.sql.Date date = new java.sql.Date(currentDate.getTime());

回答by Basil Bourque

tl;dr

tl;博士

myPreparedStatement.setObject(   // Directly exchange java.time objects with database without the troublesome old java.sql.* classes.
    … ,                                   
    LocalDate.parse(             // Parse string as a `LocalDate` date-only value.
        "2018-01-23"             // Input string that complies with standard ISO 8601 formatting.
    ) 
)

java.time

时间

The modern approach uses the java.timeclasses that supplant the troublesome old legacy classes such as java.util.Dateand java.sql.Date.

现代方法使用java.time类来取代麻烦的旧遗留类,例如java.util.Datejava.sql.Date

For a date-only value, use LocalDate. The LocalDateclass represents a date-only value without time-of-day and without time zone.

对于仅限日期的值,请使用LocalDate. 该LocalDate级表示没有时间一天和不同时区的日期,唯一的价值。

The java.time classes use standard formats when parsing/generating strings. So no need to specify a formatting pattern.

java.time 类在解析/生成字符串时使用标准格式。所以不需要指定格式模式。

LocalDate ld = LocalDate.parse( input ) ;

You can directly exchange java.timeobjects with your database using a JDBC drivercompliant with JDBC 4.2or later. You can forget about transforming in and out of java.sql.* classes.

您可以使用符合JDBC 4.2或更高版本的JDBC 驱动程序直接与您的数据库交换java.time对象。您可以忘记转换进出 java.sql.* 类。

myPreparedStatement.setObject( … , ld ) ;

Retrieval:

恢复:

LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;


About java.time

关于java.time

The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.

现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310

Where to obtain the java.time classes?

从哪里获得 java.time 类?

The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多

回答by muhammed aslam

all you have to do is this

你所要做的就是这个

    Calendar currenttime = Calendar.getInstance();               //creates the Calendar object of the current time
    Date sqldate = new Date((currenttime.getTime()).getTime());  //creates the sql Date of the above created object
    pstm.setDate(6, (java.sql.Date) date);              //assign it to the prepared statement (pstm in this case)

回答by Andrey Lebedenko

Will do: new Date(Instant.now().toEpochMilli())

会做: new Date(Instant.now().toEpochMilli())

回答by Ethan Conner

These are all too long.

这些都太长了。

Just use:

只需使用:

new Date(System.currentTimeMillis())

回答by pamcevoy

In order to get "the current date" (as in today's date), you can use LocalDate.now()and pass that into the java.sql.Datemethod valueOf(LocalDate).

为了获得“当前日期”(如今天的日期),您可以使用LocalDate.now()并将其传递到java.sql.Date方法中valueOf(LocalDate)

import java.sql.Date;
...
Date date = Date.valueOf(LocalDate.now());