前几天刚刚写了关于java中的日期的类—util.Date、sql.Date,Calendar
这三个类之间有啥区别与联系?
首先Date是第一批关于时间的类,后来出现了Calendar;
其次 util.Date是sql.Date的父类,
public class Date extends java.util.Date{}
然后是Calendar,他是一个抽象类,也有自己的子类-- GregorianCalendar
public class GregorianCalendar extends Calendar {}
再说一下具体的细节,
util.Date 有无参构造,而sql.Date没有,sql.Date的构造可以接收字符串
然后经过转型可以转换为util.Date类型
Calendar 由于是抽象类,实例化要靠子类,但是还有一个方法可以得到Calendar的实例 getInstance();
一般情况下日期是不允许被修改的,于是乎后来又有了 LocalDate、LocalDateime,等关于日期的类;
今天借此机会捣鼓一下子;
获取当前的日期,时间,日期+时间
package com.Date.Test;
import java.time.LocalDate;
import java.time.LocalDateTime;
/**
* @Auther: GavinLim
* @Date: 2021/7/10 - 07 - 10 - 10:29
* @Description: com.Date.Test
* @version: 1.0
*/
public class Test {
public static void main(String[] args) {
// now()--获取当前的日期,时间,日期+时间
LocalDate localDate=LocalDate.now();
System.out.println(localDate);//2021-07-10
LocalTime localTime= LocalTime.now();
System.out.println(localTime);//10:50:02.481155100
LocalDateTime localDateTime= LocalDateTime.now();
System.out.println(localDateTime);//2021-07-10T10:50:02.482155200
}
}

now()方法源代码—
public static LocalTime now() {
return now(Clock.systemDefaultZone());
}//返回系统默认的时区
systemDefaultZone()
该方法返回一个时钟,该时钟使用最佳可用系统时钟返回时钟的当前时刻,其中返回时钟的Zone是默认时区
public static Clock systemDefaultZone() {
return new SystemClock(ZoneId.systemDefault());
}//返回一个系统时钟的对象,
public static ZoneId systemDefault() {
return TimeZone.getDefault().toZoneId();
}
设置指定的日期,时间,日期+时间

javapublic class Test {
public static void main(String[] args) {
Month month = Month.APRIL;
LocalDate.of(2021, 7, 10);
LocalDate localDate = LocalDate.of(2021, month, 10);
System.out.println(localDate);
LocalTime.of(12, 12, 12, 456);
LocalTime localTime = LocalTime.of(11, 14, 56);
System.out.println(localTime);
LocalDateTime localDateTime = LocalDateTime.of(1999, 12, 4, 12, 45, 56, 456);
System.out.println(localDateTime);
}
}
结果—
2021-04-10
11:14:56
1999-12-04T12:45:56.000000456
of()方法的源码–
public static LocalDate of(int year, int month, int dayOfMonth) {
YEAR.checkValidValue(year);
MONTH_OF_YEAR.checkValidValue(month);
DAY_OF_MONTH.checkValidValue(dayOfMonth);
return create(year, month, dayOfMonth);
}
//返回值为LocalDate,来看一下create方法
private static LocalTime create(int hour, int minute, int second, int nanoOfSecond) {
if ((minute | second | nanoOfSecond) == 0) {
return HOURS[hour];
}
return new LocalTime(hour, minute, second, nanoOfSecond);
}
获取我们想要的数据—下面只列了几种,
public class Test {
public static void main(String[] args) {
Month month = Month.APRIL;
LocalDate.of(2021, 7, 10);
LocalDate localDate = LocalDate.of(2021, month, 10);
System.out.println(localDate);
LocalTime.of(12, 12, 12, 456);
LocalTime localTime = LocalTime.of(11, 14, 56);
System.out.println(localTime);
LocalDateTime localDateTime = LocalDateTime.of(1999, 12, 4, 12, 45, 56, 456);
System.out.println(localDateTime);
System.out.println(localDate.getYear());
System.out.println(localDateTime.getHour());
}
}
本文深入探讨了Java中日期和时间API的演变,从util.Date、sql.Date到Calendar,再到Java 8引入的LocalDate、LocalTime和LocalDateTime。通过示例展示了如何获取当前日期和时间,以及如何设置特定日期和时间。同时,文章还揭示了这些类之间的关系和使用细节,包括构造方法、of()方法的工作原理。

被折叠的 条评论
为什么被折叠?



