Java SQL Date class is part of java.sql package. java.sql.Date is a sub class of java.util.Date class.

Java SQL Date类是java.sql包的一部分。 java.sql.Date是java.util.Date类的子类。

Java SQL日期 (Java SQL Date)

Java SQL Date object can be obtained by wrapping around a millisecond value that allows JDBC to identify this as an SQL DATE value. A milliseconds value represents the number of milliseconds that have passed.

可以通过包装毫秒值来获得Java SQL Date对象,该值允许JDBC将其标识为SQL DATE值。 毫秒值表示已过去的毫秒数。

The difference between java.sql.Date and java.util.Date is that java.sql.Date does not represent time value; it keeps the value of date only.

之间的差java.sql.Datejava.util.Datejava.sql.Date不代表时间值; 它仅保留日期值。

java.sql.Date构造函数 (java.sql.Date Constructors)

  1. Date(int year, int month, int day) (Deprecated): Creates an SQL Date object using specified values of year, month and day.Date(int year, int month, int day) (Deprecated) :使用年,月和日的指定值创建一个SQL Date对象。
  2. Date(long date): Creates an SQL Date object using specified milliseconds time value.Date(long date) :使用指定的毫秒时间值创建一个SQL Date对象。

初始化SQL日期对象 (Initialize SQL Date Object)

Date object can be initialized using the given milliseconds time value.

可以使用给定的毫秒时间值初始化Date对象。

Let’s have look at the below example program.

让我们看下面的示例程序。

package com.journaldev.examples;import java.sql.Date;/*** Java initialize Sql Date object* * @author pankaj**/
public class SqlDateInitialization {public static void main(String[] args) {long d = System.currentTimeMillis();Date date = new Date(d);System.out.println(date);}
}

Java SQL日期方法 (Java SQL Date Methods)

Let’s have a look at the below methods of SQL Date class with examples.

让我们用示例看一下下面SQL Date类的方法。

  1. getHours()(Deprecated): This method returns the hour represented by this date.getHours()(Deprecated) :此方法返回此日期表示的小时。
  2. getMinutes()(Deprecated): This method returns the number of minutes past the hour represented by this date.getMinutes()(Deprecated) :此方法返回此日期所表示的小时之后的分钟数。
  3. getSeconds()(Deprecated): This method returns the number of seconds past the minute represented by this date.getSeconds()(Deprecated) :此方法返回此日期表示的分钟之后的秒数。
  4. setHours(int i)(Deprecated): This method sets the hours value of this date by using specified value of int.setHours(int i)(Deprecated) :此方法使用指定的int值设置该日期的小时值。
  5. setMinutes(int i)(Deprecated): This method sets the minutes value of this date by using specified value of int.setMinutes(int i)(Deprecated) :此方法使用指定的int值设置此日期的分钟值。
  6. setSeconds(int i)(Deprecated): This method sets the seconds value of this date by using specified value of int.setSeconds(int i)(Deprecated) :此方法使用指定的int值设置此日期的秒值。
  7. setTime(long date): This method sets an existing Date object using the given milliseconds time value. If the given milliseconds’ value contains time information, the driver will set the time components to the time in the default time zone (the time zone of the Java virtual machine running the application) that corresponds to zero GMT.setTime(long date) :此方法使用给定的毫秒时间值设置现有的Date对象。 如果给定的毫秒值包含时间信息,则驱动程序会将时间分量设置为默认时区(运行应用程序的Java虚拟机的时区)中与GMT零相对应的时间。
package com.journaldev.examples;import java.sql.Date;/*** Java setTime in sql Date* * @author pankaj**/
public class SqlDateSetTime {public static void main(String[] args) {Date date = new Date(0);System.out.println("Before setTime: "+date);long d = System.currentTimeMillis();date.setTime(d);System.out.println("After setTime: "+date);}
}

Output:

输出:

Before setTime: 1970-01-01
After setTime: 2018-07-22
  1. toString(): This method formats a date in the date escape format yyyy-mm-dd and returns the string in yyyy-mm-dd.toString() :此方法以日期转义格式yyyy-mm-dd格式化日期,并以yyyy-mm-dd返回字符串。
package com.journaldev.examples;import java.sql.Date;/*** Java toString method example* * @author pankaj**/
public class SqlDateToStringExample {public static void main(String[] args) {long d = System.currentTimeMillis();Date date = new Date(d);String stringDate = date.toString();System.out.println(stringDate);}
}

Output: 2018-07-22

产出: 2018-07-22

  1. valueOf(String s): This method converts a string in JDBC date escape format to a Date value using specified value of s- a String object representing a date in the format “yyyy-[m]m-[d]d”. The leading zero for mm and dd may also be omitted.valueOf(String s) :此方法使用s的指定值将JDBC日期转义格式的字符串转换为Date值-一个字符串对象,表示格式为“ yyyy- [m] m- [d] d”的日期。 mm和dd的前导零也可以省略。
package com.journaldev.examples;import java.sql.Date;/*** Java valueOf method example* * @author pankaj**/
public class SqlDateValueOfMethodExample {public static void main(String[] args) {String str = "2018-7-22";System.out.println(Date.valueOf(str));}
}

将java.util.Date转换为java.sql.Date (Convert java.util.Date to java.sql.Date)

package com.journaldev.examples;import java.sql.Date;/*** Java convert java.util.Date to java.sql.Date* * @author pankaj**/
public class UtilDatetoSQLDateExample {public static void main(String[] args) {java.util.Date utilDate = new java.util.Date();Date sqlDate = new Date(utilDate.getTime());System.out.println("Util Date: "+utilDate);System.out.println("SQL Date: "+sqlDate);}
}

Output:

输出:

Util Date: Sun Jul 22 18:10:58 IST 2018
SQL Date: 2018-07-22

Java 8 SQL日期方法 (Java 8 SQL Date Methods)

Let’s have a look at the below Java 8 methods of SQL Date class with examples.

让我们用示例看看下面的Java 8 SQL Date类的方法。

  1. toLocalDate(): This method Converts this Date object to a LocalDate. The conversion creates a LocalDate that represents the same date value as this Date in local time zone and returns a LocalDate object representing the same date value.toLocalDate() :此方法将此Date对象转换为LocalDate 。 转换将创建一个LocalDate,该LocalDate表示与该Date在本地时区相同的日期值,并返回一个LocalDate对象,该对象表示同一日期值。
  2. valueOf(LocalDate date): This method Obtains an instance of Date from a LocalDate object with the same year, month and day of month value as the given LocalDate. The provided LocalDate is interpreted as the local date in the local time zone.valueOf(LocalDate date) :此方法从LocalDate对象获取Date的实例,该实例的月,月和日的值与给定的LocalDate相同。 提供的LocalDate解释为本地时区中的本地日期。
package com.journaldev.examples;import java.sql.Date;
import java.time.LocalDate;/*** Java 8 SQL Date methods example* * @author pankaj**/
public class SQLDateJava8MethodsExample {public static void main(String[] args) {Date date = new Date(System.currentTimeMillis());LocalDate localDate = date.toLocalDate();Date sqldate = Date.valueOf(localDate);System.out.println("Local Date: "+localDate);System.out.println("SQL Date: "+sqldate);}
}

Output:

输出:

Local Date: 2018-07-22
SQL Date: 2018-07-22

Also, check Java 8 Date for more about Date in java.

另外,请查看Java 8 Date以获取有关Java中日期的更多信息。

That’s all for Java SQL Date, I hope nothing important got missed here.

Java SQL Date就这些了,我希望这里没有重要的事情。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/22374/java-sql-date

java.sql.Date – Java SQL日期相关推荐

  1. java string转sql date_Java中的util.Date,sql.Date,sql.Time,String类型转换

    今天总结一下工具类中Date类型. java.sql.Date,java.sql.Time,和java.sql.Timestamp(时间戳记)都是java.util.Date的子类. java.sql ...

  2. java.sql.Date引发的日期格式转换错误

    记一次springboot开发中使用jackson进行时间格式化时,由于实体类定义的时间类型为java.sql.Date导致格式化转换错误 前端获取到的结果: 数据库存储的时间: 分析: java.s ...

  3. java.util.Date java.sql.Date SimpleDateFormat String 转DATE

    涉及知识点: 1.SQL server  中   datetime   对应  java  中的  timeStamp  类型 PreparedStatement   |  CallableState ...

  4. java.util.Date java.sql.Date

    java.sql.Date dateSql = new java.sql.Date(new java.util.Date().getTime());System.out.println(dateSql ...

  5. JDK8时间与java.util.Date,java.sql.Date,Timestamp等的相互转换(时间转换)

  6. java 8 date time,Java8 日期/时间(Date Time)API指南

    示例方法的详解都包含在注释内,当我们运行程序时,可以得到以下输出: Current Date=2014-04-28 Specific Date=2014-01-01 Current Date in I ...

  7. 取java.sql.date日期_JAVA 处理时间 - java.sql.Date、java.util.Date与数据库中的Date字段的转换方法[转]...

    1.如何将java.util.Date转化为java.sql.Date? 转化: java.sql.Date sd; java.util.Date ud; //initialize the ud su ...

  8. java sql date 加减_Java对日期Date类进行加减运算,年份加减,月份加减

    Date d=new Date(); SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd"); System.out.prin ...

  9. java中日期的数据类型是啥_用于存储日期和时间的最合适的SQL和Java数据类型

    使用以下格式处理日期和时间的最合适的MySQL和Java数据类型是什么:yyyy.MM.dd hh:mm:ss 在持久层(jdbc类型)中使用的相应Java类型是java.sql.Timestamp. ...

最新文章

  1. Sql结果导出为excel文件
  2. 天合公司 TRW Inc.
  3. 做向量召回 All You Need is 双塔
  4. 【C语言】复合逻辑运算
  5. 如何在SAP Spartacus里增添自定义的配置条目
  6. 有关西电的课程学分相关问题:必修课、选修课、补考、重修、学分
  7. ping cat.flag.php,关于2020年强网杯-强网先锋-主动的赛题解析
  8. socket epoll网络编程实例
  9. 7-23 哥尼斯堡的“七桥问题”(25 分)
  10. mysql批量插入之提高插入效率
  11. linux malloc和free解析
  12. Bootstrap图片中加播放按钮
  13. 找不到ADO.NET Entity Data Model模板或 sql server database project模板
  14. 74cms v4.2.1-v4.2.129-后台getshell漏洞 复现
  15. 伪原创文章生成器-自媒体洗稿工具-关键词文章生成工具免费
  16. BERT模型深度解析
  17. PHP中将Word文件转换为PDF
  18. IO---缓冲流、字符集、转换流、序列化和反序列化
  19. 自定义控件 TextView 歌词 Lrc
  20. 车载BlueTooth通话机制原理及开发

热门文章

  1. free、vmstat监视内存使用情况
  2. Unity的状态机设计
  3. 工作经验总结:百万数据引发的性能瓶颈问题
  4. [转载] python自定义异常类型和raise抛出异常
  5. Django rest framework 序列化组件
  6. LockSupport HotSpot里park/unpark的实现
  7. [NOIP2003普及组]麦森数(快速幂+高精度)
  8. 使用struts中的DisPatchAction的时候需要用到的jar包
  9. 那些必须要知道的Javascript
  10. 使用doxygen查看文件包含关系图