java.sql.Date类解析java13
Posted 香水炸弹熊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java.sql.Date类解析java13相关的知识,希望对你有一定的参考价值。
/*
* Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*继承链
java.lang.Object
java.util.Date
java.sql.Date
java.sql.Date提供了毫秒值的薄层封装,对应于数据库中的date类型。其毫秒值相对于19700101 00:00:00.000 GMT而言。
*
*
*/
package java.sql;
import java.time.Instant;
import java.time.LocalDate;
/**
* <P>A thin wrapper around a millisecond value that allows
* JDBC to identify this as an SQL <code>DATE</code> value. A
* milliseconds value represents the number of milliseconds that
* have passed since January 1, 1970 00:00:00.000 GMT.
* <p>
* To conform with the definition of SQL <code>DATE</code>, the
* millisecond values wrapped by a <code>java.sql.Date</code> instance
* must be ‘normalized‘ by setting the
* hours, minutes, seconds, and milliseconds to zero in the particular
* time zone with which the instance is associated.
*
* @since 1.1
*/
public class Date extends java.util.Date {
/**
* Constructs a <code>Date</code> object initialized with the given
* year, month, and day.
* <P>
* The result is undefined if a given argument is out of bounds.
*
* @param year the year minus 1900; must be 0 to 8099. (Note that
* 8099 is 9999 minus 1900.)
* @param month 0 to 11
* @param day 1 to 31
* @deprecated instead use the constructor <code>Date(long date)</code>
*/
@Deprecated(since="1.2")//从1.2开始弃用
public Date(int year, int month, int day) {
super(year, month, day);
}
/**
* Constructs a <code>Date</code> 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.
*
* @param date milliseconds since January 1, 1970, 00:00:00 GMT not
* to exceed the milliseconds representation for the year 8099.
* A negative number indicates the number of milliseconds
* before January 1, 1970, 00:00:00 GMT.
参数为long类型 毫秒值,代表从19700101 00:00:00.000毫秒之后或之前过了多长时间,负值表示之前。得到的时间不包含时分秒毫秒。
*/
public Date(long date) {
// If the millisecond date value contains time info, mask it out.
super(date);
}
/**
* Sets an existing <code>Date</code> 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.
*
* @param date milliseconds since January 1, 1970, 00:00:00 GMT not
* to exceed the milliseconds representation for the year 8099.
* A negative number indicates the number of milliseconds
* before January 1, 1970, 00:00:00 GMT.
赋值,只有日期部分,不含时间部分
*/
public void setTime(long date) {
// If the millisecond date value contains time info, mask it out.
super.setTime(date);
}
/**
* Converts a string in JDBC date escape format to
* a <code>Date</code> value.
*
* @param s a <code>String</code> object representing a date in
* in the format "yyyy-[m]m-[d]d". The leading zero for <code>mm</code>
* and <code>dd</code> may also be omitted.
* @return a <code>java.sql.Date</code> object representing the
* given date
* @throws IllegalArgumentException if the date given is not in the
* JDBC date escape format (yyyy-[m]m-[d]d)
将格式为yyyy-mm-dd的字符串转为date型,字符串的格式必须为yyyy-mm-dd其中月和日可以为一位,即2019-03-06,可为2019-3-6,
非此格式抛出异常IllegalArgumentException
*/
public static Date valueOf(String s) {
if (s == null) {
throw new java.lang.IllegalArgumentException();
}
final int YEAR_LENGTH = 4;//定义日期中年月日的长度及最大长度
final int MONTH_LENGTH = 2;
final int DAY_LENGTH = 2;
final int MAX_MONTH = 12;
final int MAX_DAY = 31;
Date d = null;
int firstDash = s.indexOf(‘-‘);//分隔年月日
int secondDash = s.indexOf(‘-‘, firstDash + 1);
int len = s.length();
if ((firstDash > 0) && (secondDash > 0) && (secondDash < len - 1)) {
if (firstDash == YEAR_LENGTH &&
(secondDash - firstDash > 1 && secondDash - firstDash <= MONTH_LENGTH + 1) &&
(len - secondDash > 1 && len - secondDash <= DAY_LENGTH + 1)) {
int year = Integer.parseInt(s, 0, firstDash, 10);//先转换为整数
int month = Integer.parseInt(s, firstDash + 1, secondDash, 10);
int day = Integer.parseInt(s, secondDash + 1, len, 10);
if ((month >= 1 && month <= MAX_MONTH) && (day >= 1 && day <= MAX_DAY)) {
d = new Date(year - 1900, month - 1, day);//转换为日期格式。
}
}
}
if (d == null) {
throw new java.lang.IllegalArgumentException();
}
return d;
}
/**
* Formats a date in the date escape format yyyy-mm-dd.
*
* @return a String in yyyy-mm-dd format
将日期型转换为yyyy-mm-dd格式的字符串
*/
@SuppressWarnings("deprecation")
public String toString () {
int year = super.getYear() + 1900;//java.util.Date的getYear()得到的日期为减掉1900的值,此处需要加上已获得真实事件
int month = super.getMonth() + 1;//同上
int day = super.getDate();
char buf[] = new char[10];//转为数值型在年月日中间加上横杠,再转为字符串
formatDecimalInt(year, buf, 0, 4);
buf[4] = ‘-‘;
Date.formatDecimalInt(month, buf, 5, 2);
buf[7] = ‘-‘;
Date.formatDecimalInt(day, buf, 8, 2);
return new String(buf);
}
/**
* Formats an unsigned integer into a char array in decimal output format.
* Numbers will be zero-padded or truncated if the string representation
* of the integer is smaller than or exceeds len, respectively.
*
* Should consider moving this to Integer and expose it through
* JavaLangAccess similar to Integer::formatUnsignedInt
* @param val Value to convert
* @param buf Array containing converted value
* @param offset Starting pos in buf
* @param len length of output value
将无符号整型值val放到字符数组buf中,位数不足时补零,超出时截断
*/
static void formatDecimalInt(int val, char[] buf, int offset, int len) {//buf是数组行,形参值改变,实参值也改变(基本类型或者String,实参值不变,因为传的是值,对象集合数组传的是引用,实参值会变。)
int charPos = offset + len;
do {
buf[--charPos] = (char)(‘0‘ + (val % 10));//从最后一位开始取余数,转为字符放到字符数组中,如2019,则buf[3]=9再buf[2]=1...
val /= 10;
} while (charPos > offset);
}
// Override all the time operations inherited from java.util.Date;
/**
* This method is deprecated and should not be used because SQL Date
* values do not have a time component.
*
* @deprecated
* @exception java.lang.IllegalArgumentException if this method is invoked
* @see #setHours
永远抛出异常,不能使用。java.sql.Date值没有时间部分。弃用
*/
@Deprecated(since="1.2")
public int getHours() {
throw new java.lang.IllegalArgumentException();
}
/**
* This method is deprecated and should not be used because SQL Date
* values do not have a time component.
*
* @deprecated
* @exception java.lang.IllegalArgumentException if this method is invoked
* @see #setMinutes
永远抛出异常,不能使用。java.sql.Date值没有时间部分。弃用
*/
@Deprecated(since="1.2")
public int getMinutes() {
throw new java.lang.IllegalArgumentException();
}
/**
* This method is deprecated and should not be used because SQL Date
* values do not have a time component.
*
* @deprecated
* @exception java.lang.IllegalArgumentException if this method is invoked
* @see #setSeconds
永远抛出异常,不能使用。java.sql.Date值没有时间部分。弃用
*/
@Deprecated(since="1.2")
public int getSeconds() {
throw new java.lang.IllegalArgumentException();
}
/**
* This method is deprecated and should not be used because SQL Date
* values do not have a time component.
*
* @deprecated
* @exception java.lang.IllegalArgumentException if this method is invoked
* @see #getHours
永远抛出异常,不能使用。java.sql.Date值没有时间部分。弃用
*/
@Deprecated(since="1.2")
public void setHours(int i) {
throw new java.lang.IllegalArgumentException();
}
/**
* This method is deprecated and should not be used because SQL Date
* values do not have a time component.
*
* @deprecated
* @exception java.lang.IllegalArgumentException if this method is invoked
* @see #getMinutes
永远抛出异常,不能使用。java.sql.Date值没有时间部分。弃用
*/
@Deprecated(since="1.2")
public void setMinutes(int i) {
throw new java.lang.IllegalArgumentException();
}
/**
* This method is deprecated and should not be used because SQL Date
* values do not have a time component.
*
* @deprecated
* @exception java.lang.IllegalArgumentException if this method is invoked
* @see #getSeconds
永远抛出异常,不能使用。java.sql.Date值没有时间部分。弃用
*/
@Deprecated(since="1.2")
public void setSeconds(int i) {
throw new java.lang.IllegalArgumentException();
}
/**
* Private serial version unique ID to ensure serialization
* compatibility.
*/
static final long serialVersionUID = 1511598038487230103L;
/**
* Obtains an instance of {@code Date} from a {@link LocalDate} object
* with the same year, month and day of month value as the given
* {@code LocalDate}.
* <p>
* The provided {@code LocalDate} is interpreted as the local date
* in the local time zone.
*
* @param date a {@code LocalDate} to convert
* @return a {@code Date} object
* @exception NullPointerException if {@code date} is null
* @since 1.8
LocalDate型转换为Date型,弃用
*/
@SuppressWarnings("deprecation")
public static Date valueOf(LocalDate date) {
return new Date(date.getYear() - 1900, date.getMonthValue() -1,
date.getDayOfMonth());
}
/**
* Creates a {@code LocalDate} instance using the year, month and day
* from this {@code Date} object.
* @return a {@code LocalDate} object representing the same date value
*
* @since 1.8
Date型转为LocalDate型,弃用
*/
@SuppressWarnings("deprecation")
public LocalDate toLocalDate() {
return LocalDate.of(getYear() + 1900, getMonth() + 1, getDate());
}
/**
* This method always throws an UnsupportedOperationException and should
* not be used because SQL {@code Date} values do not have a time
* component.
*
* @exception java.lang.UnsupportedOperationException if this method is invoked
永远抛出异常,java.sql.Date值没有时间部分。
*/
@Override
public Instant toInstant() {
throw new java.lang.UnsupportedOperationException();
}
}
以上是关于java.sql.Date类解析java13的主要内容,如果未能解决你的问题,请参考以下文章
java获取时间的几种方式Date VS LocalDate
java.util.Date和java.sql.Date的区别和相互转化(转)