JAVA时间类:
JAVA的Date的使用:
java.util.Date是java.sql包下的各个时间类型的父类,如TimeStamp、Date、Time。因此这些类型之间的转换按照父类与子类之间的转换进行。
若要对时间进行加减,则会用到Calendar类,对时间进行格式化,则会用到SimpleDateFormat。
JAVA的时间转换类为SimpleDateFormat,时间格式中各字母的含义如下:

使用LocalDateTime替换DateTime:
因Calendar使用起来代码量较多且无法确保线程安全,JAVA8使用了LocalDateTime、LocalDate、LocalTime完成对JAVA的Date与DateTime替换。
示例:获取上个月的开始时间与结束时间:
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String beginDate = dateTimeFormatter.format(LocalDateTime.of(LocalDate.from(LocalDateTime.now().minusMonths(1).with(TemporalAdjusters.firstDayOfMonth())), LocalTime.MIN));
String endDate = dateTimeFormatter.format(LocalDateTime.of(LocalDate.from(LocalDateTime.now().with(TemporalAdjusters.lastDayOfMonth())), LocalTime.MIN));
System.out.println("beginDate="+beginDate+" "+"endDate="+endDate);
输出结果为:
beginDate=2021-09-01 endDate=2021-10-01
JAVA多态:
子类继承父类或者子类实现父类接口,并将子类实例赋予父类对象或者父类接口对象是实现了JAVA的多态。多态下,子类实例赋予父类对象的方法指向会转为指向子类重写的方法,但对属性而言则不会转变指向。
示例如下:
实现类:
public class test1 implements test{
int i=2;
//重写接口已实现方法
public int test1(){
return 21;
}
//重写接口未实现方法
@Override
public int test() {
return 21;
}
public static void main(String[] args) {
test1 t1=new test1();
test t=t1;
System.out.println("多态方法与属性调用指向示例:");
System.out.println(t1.i+" "+t1.test1()+" "+t1.test()+" "+t1.test2());
System.out.println(t.i+" "+ t.test1()+" "+ t.test()+" "+t.test2());
}
}
接口:
public interface test {
int i=1;
int test();
default int test1(){
return 1;
}
default int test2(){
return 1;
}
}
main结果显示:

本文介绍Java中日期时间类的使用,包括Date、Calendar、SimpleDateFormat及JAVA8新引入的LocalDateTime等类的使用方法。同时,通过一个具体示例展示了Java多态的概念与应用。

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



