java.sql.Date – Java SQL日期

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

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: java.util.DateJava提供的日期和时间的类,它包含了日期和时间的信息。它可以表示从1970年1月1日00:00:00以来经过的毫秒数。由于java.util.Date是不可变的类,因此它的操作非常有限。 而java.sql.Datejava.util.Date的一个子类,它是专门用于操作数据库中日期类型数据的类。java.sql.Date继承了java.util.Date的大部分方法,但是它只保存年月日的信息,不包含具体的时分秒。java.sql.Date可以通过java.sql包提供的接口和类与数据库交互。 因为java.sql.Date是专为数据库而设计的,它有很多特定的用法。比如可以将java.sql.Date从数据库中读取出来,并用作Java程序中的日期类型。此外,它还可以将Java程序中的日期类型转换为数据库中的日期类型,方便进行数据库操作。 总的来说,java.util.DateJava中常用的日期和时间类型,而java.sql.Date是专为数据库设计的日期类型,在与数据库交互时非常有用。 ### 回答2: java.util.DateJava中的一个类,它表示一个特定的时间点,包括年、月、日、时、分、秒等信息。它用于表示一个精确到毫秒的时间点,并且它没有与时区相关的信息。在使用java.util.Date时,我们可以通过构造方法设置指定的年月日时分秒,也可以使用现有的时间点创建对象。 java.sql.Datejava.util.Date的一个子类,用于在Java程序和数据库之间传递日期数据。它继承了java.util.Date的基本功能,同时还提供了一些用于处理数据库日期的特定方法。在Java中,java.sql.Date可以通过使用java.util.Date进行实例化,或者通过调用java.sql.Date的构造方法指定年月日。 java.util.Datejava.sql.Date在功能上有着一些区别。首先,java.util.Date中包含的时间信息更加详细,可以表示年、月、日、时、分、秒、毫秒等。而java.sql.Date只包含年、月、日的信息,没有时分秒。其次,java.util.Date可以用于处理任意的时间点,而java.sql.Date只能用于处理日期,时间部分会被忽略。另外,由于java.sql.Date主要用于数据库交互,它具有特定的格式,可以与数据库中的日期字段进行交互。 在实际应用中,我们通常使用java.util.Date来表示一个具体的时间点,比如记录日志的时间、定时任务的执行时间等。而在与数据库交互时,我们可以使用java.sql.Date来传递、存储和检索日期数据。 ### 回答3: java.util.Datejava.sql.Date都是Java中用于表示日期和时间的类,但在使用上有一些区别。 java.util.DateJava中最基本的日期和时间类,它包含了日期和时间的信息,可以表示从1970年1月1日00:00:00开始的时间戳。它可以表示精确到毫秒级别的时间,但是在处理日期和时间的各种操作时,它的功能相对较弱,需要借助其他类库进行处理。在Java 8之后,java.util.Date已经被废弃,推荐使用新的日期和时间API(java.time包)。 java.sql.Datejava.util.Date的一个子类,它用于在Java和数据库之间传递日期。它的底层实现是基于long类型的时间戳,但它对应的数据库类型是SQL中的DATE类型,只包含日期部分,不包含具体的时间信息。它的使用场景通常是在与数据库交互的过程中,需要将日期信息保存到数据库或从数据库中获取日期信息。 在实际使用中,如果我们需要进行日期和时间的大部分操作,推荐使用新的日期和时间API(java.time包),例如LocalDate、LocalTime和LocalDateTime等类,它们提供了更多的功能和操作,而且更易于使用和理解。而对于与数据库交互时,如果数据库的数据类型是DATE类型,我们可以使用java.sql.Date来表示和传递日期信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值