以 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
Get the current date in java.sql.Date format
提问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.Date
object with the same format?
有没有办法获得java.sql.Date
相同格式的对象?
采纳答案by rgettman
A java.util.Date
is not a java.sql.Date
. It's the other way around. A java.sql.Date
is 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.Date
by using the constructor that takes a long
that a java.util.Date
can supply.
您需要java.sql.Date
使用带long
ajava.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.Date
and java.sql.Date
.
现代方法使用java.time类来取代麻烦的旧遗留类,例如java.util.Date
和java.sql.Date
。
For a date-only value, use LocalDate
. The LocalDate
class 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 类?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android, the ThreeTenABPproject adapts ThreeTen-Backport(mentioned above). See How to use ThreeTenABP….
- Java SE 8、Java SE 9及更高版本
- 内置。
- 具有捆绑实现的标准 Java API 的一部分。
- Java 9 添加了一些小功能和修复。
- Java SE 6和Java SE 7
- 多的java.time功能后移植到Java 6和7在ThreeTen-反向移植。
- 安卓
- 更高版本的 Android 捆绑实现 java.time 类。
- 对于早期的 Android,ThreeTenABP项目采用了ThreeTen-Backport(上面提到过)。请参阅如何使用ThreeTenABP ...。
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 的试验场。你可能在这里找到一些有用的类,比如Interval
,YearWeek
,YearQuarter
,和更多。
回答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.Date
method valueOf(LocalDate)
.
为了获得“当前日期”(如今天的日期),您可以使用LocalDate.now()
并将其传递到java.sql.Date
方法中valueOf(LocalDate)
。
import java.sql.Date;
...
Date date = Date.valueOf(LocalDate.now());