Summary of common methods of Date class

Date class

There are two types of Date in Java: Date class under java.util package, Date class under java.sql package

The difference and connection between the two Date classes

java.util.Date is the parent class of java.sql.Date

public class Date extends java.util.Date {
    
    ......}

java.sql.Date is usually used to represent the date of the database. Compared with java.util.Date, it only contains the year, month, and day, but not the specific time.

Outdated method

Before JDK 1.1, the Date class had two additional functions. It allows the date to be interpreted as year, month, day, hour, minute, and second values. It also allows formatting and parsing date strings. Unfortunately, these functional APIs are not suitable for internationalization.
After JDK1.1, these methods for obtaining year, month, day, hour, minute, and second values ​​are marked as obsolete methods

@Deprecated//注释为过时方法,不推荐使用
public int getDay() {
    
    
    return normalize().getDayOfWeek() - BaseCalendar.SUNDAY;
}

java.util.Date

Construction method

After the jdk1.1 version, there are only two types of construction methods recommended (not outdated): Date() and Date(long date)

public Date() {
    
    //调用Date(long date)方法传递的参数为当前系统时间自1970年1月1日08:00:00 GMT(默认时区东八区)以来的经过毫秒数
    this(System.currentTimeMillis());
}
public Date(long date) {
    
    //将Date类自1970年1月1日00:08:00 GMT(默认时区东八区)以来的经过毫秒数设置为date,可以为负数,表示过去的时间
    fastTime = date;
}

Common method

  • setTime(long time) method
    • Set this Date object to represent the time point in time milliseconds after 00:08:00 on January 1, 1970, Greenwich Mean Time.
    • No return value
  • getTime() method
    • Returns the number of milliseconds since 00:08:00 on January 1, 1970 represented by this Date object.
    • The return value is long
  • equals() method
    • This method is to override the equals method inherited from Object
    • Determine whether two Date objects are equal by the number of milliseconds that have passed since Greenwich Mean Time.
    • The return value is boolean
  public boolean equals(Object obj) {
    
    
    return obj instanceof Date && getTime() == ((Date) obj).getTime();
}
  • toString() method
 Fri Oct 02 13:35:08 CST 2020
  • Convert this Date object to a String of the following form: dow mon dd hh:mm:ss zzz yyyy
  • dow: Day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat).
    mon: month (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec).
    dd: The day of the month (01 to 31), with two digits after the decimal point.
    hh: The hour of the day (00 to 23), with two digits after the decimal point.
    mm: The minute within the hour (00 to 59), with two digits after the decimal point.
    ss: The second within the minute (00 to 61), two decimal digits.
    zzz: Time zone , the default is Dongba District, which displays CST, China Standard Time, also known as China Standard Time, which is China's standard time. In terms of time zone division, it belongs to the East Eight District, which is 8 hours earlier than Coordinated Universal Time. If time zone information is not available, zzz is empty-that is, it does not contain any characters at all.
    yyyy: year .

java.sql.Date

Construction method

Note: The Date has no construction method without parameters, and parameters must be passed in during initialization.

public Date(long date) {
    
    //调用父类(java.util.Date)的构造方法设置毫秒数
    // If the millisecond date value contains time info, mask it out.
    super(date);

}

Common method

Since java.sql.Date inherits the java.util.Date class, it has the setTime(long time), getTime(), equals() and other methods in the java.util.Date class and will not be repeated here.

  • toString() method
    • Format the date in the date escape format yyyy-mm-dd.
    • The return value is String, which returns a string in yyyy-mm-dd format.
    • Call the obsolete methods getYear(), getMonth() and getDate() of the parent class Date to get the year, month, and day.
  @SuppressWarnings("deprecation")
public String toString () {
    
    
    int year = super.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);
}
  • valueOf​(String s) method
    • This method is static
    • Convert a string in the JDBC date escape format into a Date value.
    • If the given date is not in the JDBC date escape format (yyyy-[m]m-[d]d), an IllegalArgumentException will be thrown
    • Return a Date object
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();//String总长度

    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);//将0~第一个"-"位置的字符串转换为数字
            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);//用已过时的构造方法Date​(int year, int month, int day)创建对象
            }
        }
    }
    if (d == null) {
    
    
        throw new java.lang.IllegalArgumentException();
    }

    return d;

}
  • valueOf​(LocalDate date)方法

    • This method is static
    • Obtain an instance of Date from the LocalDate object. Date has the same year, month, and day values ​​as the given LocalDate.
      The LocalDate provided is interpreted as the local date in the local time zone.
    • NullPointerException will be thrown if date is empty
    • Return the converted Date object
  • toLocalDate() method

    • Create a LocalDate instance using the year, month, and day in this Date object.
    • Is a new method added in JDK1.8
    • Return a LocalDate object

Guess you like

Origin blog.csdn.net/tuziud233/article/details/108879337