java.sql.Date – Java SQL日期-程序员宅基地

技术标签: python  java  字符串  编程语言  大数据  

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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/cunchi4221/article/details/107475559

智能推荐

Keil3/4/5 For C51安装教程(附Baidu 云链接)_keil4在安装包-程序员宅基地

文章浏览阅读1.5w次。Keil4安装及破解教程(以keil4为例)Keil C51是美国Keil Software公司出品的51系列兼容单片机C语言软件开发系统非常方便的提供了51单片机及arm类型的单片机的c语言开发的平台下面以keil4为例,详细说明keil4的安装及破解过程(破解用于非商业用途,还是要支持正版软件)链接:https://pan.baidu.com/s/1LxUFeIzfXdNn-uYwT..._keil4在安装包

(五)定位误差探究_军用级 民用级 定位精度 csdn-程序员宅基地

文章浏览阅读1.8k次。卫星定位技术,不可避免的会有误差产生。而误差产生的来源,一般可以大致分为三类原因。可以如实说,现在市面上的所宣称的(民用)GPS定位精度在分米级、厘米级的接收终端,统统都是虚假宣传。在环境极其理想的情况下,GPS的定位精度在10米以内。而就目前而言,定位精度最好的应是Glonass定位技术(北半球)、GPS定位技术、北斗定位技术(亚太区域)。至于伽利略卫星导航系统现在还在搞建设呢,就不做考虑。以上_军用级 民用级 定位精度 csdn

tensorflow实现简单RNN_tensorflow rnn-程序员宅基地

文章浏览阅读586次。使用简单RNN预测谷歌的股票实验数据import numpy as npimport tensorflow.keras as kerasimport pandas as pdimport matplotlib.pyplot as pltfrom sklearn.preprocessing import MinMaxScalerdef RNN(x_train, y_train): regressor = keras.Sequential() # add the first R_tensorflow rnn

UE4调试源码正确方式_如何改变ue源码模式-程序员宅基地

文章浏览阅读7.3k次,点赞2次,收藏4次。准备1. 编译好的DevelopmentEditor或DebugEditor版本源码;2. 在对应的源码中生成的C++项目;本文以调试SoftOcclusion源码部分来看看如何执行。错误示范1.直接运行项目代码的Sln文件。2.在对应的相关源码中打上断点,点击调试执行,打开对应的包好UE4 Editor界面的项目。3.设置选项,点击“Play”运行项目,同时试图命..._如何改变ue源码模式

车载总线监控分析及仿真工具- INTEWORK VBA_vehicle bus analyser-程序员宅基地

文章浏览阅读969次。车载总线监控分析及仿真工具- INTEWORK VBA概述INTEWORK-VBA(Vehicle Bus Analyzer)车载总线监控分析及仿真工具,是由恒润自主研发的一款专业、易用的车载总线工具。具备对总线数据的监控与分析、节点仿真、报文发送、负载统计、离线回放、故障诊断、脚本仿真和Panel面板搭建等功能。当前支持CAN、CANFD、LIN、Ethernet总线类型。产品特点• 稳定可靠: 12 路高负载长时间(1 个月)监控测试不丢帧• 简单易用: 10分钟上手使用,配备详细的入门_vehicle bus analyser

JAVA反射机制及其原理实现_java反射机制原理详解-程序员宅基地

文章浏览阅读7.6k次,点赞12次,收藏47次。9.1 概念JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;public、protected、private。OO(面向对象),private私有的,不能访问。这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制。**反射就是把java类中的各种成分映射成一个个的Java对象 例..._java反射机制原理详解

随便推点

C# WinForm 封装自定义组件(控件)Dll_把winfrom用户自定义控件封装成dll-程序员宅基地

文章浏览阅读2w次,点赞6次,收藏22次。封装自定义控件很简单,没什么技术含量,这里通过封装自定义的数字文本框实例简单总结一下:【1】新建自定义控件库 -- Windows Forms Control Library【2】添加自定义组件 -- Component Class【3】继承TextBox,添加KeyPress事件,代码如下:using System;usi_把winfrom用户自定义控件封装成dll

mysql关闭slave_mysql 关闭slave-程序员宅基地

文章浏览阅读619次。mysql版本:5.6.14一、修改 my.cnf 文件,增加skip-slave-start参数即可[mysqld]#主从log-bin=mysql-binserver-id=148skip-slave-start二、重启mysql/etc/init.d/mysql restart三、验证slave是否启动mysql> SHOW SLAVE STATUS\G****************..._mysql disable slave

vue-router或者vue-admin-template中刷新跳转404的解决办法_vue admin 直接跳到404-程序员宅基地

文章浏览阅读5k次。vue或者vue-admin-template中任意页面刷新都跳转404vue在刷新的时候动态添加的router会清空,所以在动态添加的路由页面刷新的时候会因为清空的router而跳转到404.。在/src/premission.js的最下方router.afterEach修改成下面这样router.afterEach(to =>{ sessionStorage.setItem('r..._vue admin 直接跳到404

由于uvc驱动函数缺少return语句而导致内核oops的一例-程序员宅基地

文章浏览阅读282次。一、实验环境1、软件a) Vmware版本:Vmware Workstation 12.5.7b) Ubuntu版本:9.10c) 内核版本:2.6.31.14d) gcc版本:4.4.1e) gdb版本:7.02、摄像头硬件百问网自制uvc摄像头3、排查过程中,使用到的工具a) printkb) objdumpc) straced)gdb二、前言用C语言写程序时,如果定义一个带返回值的..._uvc_video_qbuf失败

Python——实例1:温度转换(Python基本语法元素及框架,代码实现)_温度转换的python程序-程序员宅基地

文章浏览阅读4.3w次,点赞64次,收藏236次。前言Python第一弹!!!Python被称为最简单好上手的语言之一,基于其极强的关联性,对各种库的引用,和资源的关联,使其实现功能非常容易。一些底层逻辑不需过多过深的理解。本篇将通过一个实例——温度转换,通过十行代码的实现,使大家对Python有最初的大体印象,并对一些基础语法和函数有初步的了解。读完本篇,你将了解到:(1)程序的格式框架(代码高亮、缩进、注释使用)(2)命名与保留字(变量、命名及33个保留字)(3)数据类型(整数、字符串、列表)(4)语句与函数(赋值语句、分支语句、函数)_温度转换的python程序

解决FeignException返回基础服务抛出的状态码-程序员宅基地

文章浏览阅读2.9w次。接上篇文章这样,虽然能够很好的处理@Valid出现的异常,但是如果是主动抛出的自定义异常和Assert断言异常,则会进入FeignException.errorStatus处理,查看源码它将message封装了feign抛出的status500以及body的content(即你主动抛出的message信息)意味着,断言异常我们可以通过ErrorDecoder的方式再通过全局异常拦截获取messag..._feignexception

推荐文章

热门文章

相关标签