进阶day01-LocalDate的基本使用(代替Calender)
This commit is contained in:
66
javaSE-day01/src/com/inmind/jdk8_time04/LocalDateDemo12.java
Normal file
66
javaSE-day01/src/com/inmind/jdk8_time04/LocalDateDemo12.java
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
package com.inmind.jdk8_time04;
|
||||||
|
|
||||||
|
import java.time.DayOfWeek;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.Month;
|
||||||
|
|
||||||
|
/*
|
||||||
|
static LocalDate now() 从默认时区的系统时钟获取当前日期。
|
||||||
|
*/
|
||||||
|
public class LocalDateDemo12 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
LocalDate ld = LocalDate.now();
|
||||||
|
System.out.println(ld);//LocalDate类的toString内容打印出来
|
||||||
|
// 1、获取日期对象中的信息
|
||||||
|
int year = ld.getYear();
|
||||||
|
int monthValue = ld.getMonthValue();
|
||||||
|
int day = ld.getDayOfMonth();
|
||||||
|
int dayOfWeek = ld.getDayOfWeek().getValue();
|
||||||
|
System.out.println(year);
|
||||||
|
System.out.println(monthValue);
|
||||||
|
System.out.println(day);
|
||||||
|
System.out.println(dayOfWeek);
|
||||||
|
System.out.println("---------------------------------");
|
||||||
|
// 2、直接修改某个信息: withYear、withMonth、withDayOfMonth、withDayOfYear
|
||||||
|
LocalDate ld1 = ld.withYear(2099);
|
||||||
|
LocalDate ld2 = ld.withMonth(12);
|
||||||
|
LocalDate ld3 = ld.withDayOfMonth(18);
|
||||||
|
|
||||||
|
System.out.println(ld);
|
||||||
|
System.out.println(ld1);
|
||||||
|
System.out.println(ld2);
|
||||||
|
System.out.println(ld3);
|
||||||
|
System.out.println("----------------------------------");
|
||||||
|
// 3、把某个信息加多少: plusYears、plusMonths、plusDays、plusWeeks
|
||||||
|
LocalDate ld4 = ld.plusYears(2);
|
||||||
|
LocalDate ld5 = ld.plusMonths(2);
|
||||||
|
LocalDate ld6 = ld.plusDays(2);
|
||||||
|
System.out.println(ld);
|
||||||
|
System.out.println(ld4);
|
||||||
|
System.out.println(ld5);
|
||||||
|
System.out.println(ld6);
|
||||||
|
System.out.println("---------------------------------------");
|
||||||
|
// 4、把某个信息减多少:minusYears、minusMonths、minusDays、minusWeeks
|
||||||
|
|
||||||
|
LocalDate ld7 = ld.minusYears(2);
|
||||||
|
LocalDate ld8 = ld.minusMonths(2);
|
||||||
|
LocalDate ld9 = ld.minusDays(2);
|
||||||
|
System.out.println(ld);
|
||||||
|
System.out.println(ld7);
|
||||||
|
System.out.println(ld8);
|
||||||
|
System.out.println(ld9);
|
||||||
|
System.out.println("------------------------------");
|
||||||
|
|
||||||
|
// 5、获取指定日期的LocalDate对象: public static LocalDate of(int year, int month, int dayOfMonth)
|
||||||
|
//来一个2099-9-9的LocalDate对象
|
||||||
|
LocalDate ld10 = LocalDate.of(2099, 9, 9);
|
||||||
|
LocalDate ld11 = LocalDate.of(2099, Month.SEPTEMBER, 9);
|
||||||
|
System.out.println(ld10);
|
||||||
|
System.out.println(ld11);
|
||||||
|
System.out.println("--------------------------------------------");
|
||||||
|
// 6、判断2个日期对象,是否相等,在前还是在后: equals isBefore isAfter
|
||||||
|
System.out.println(ld.equals(ld7));//false
|
||||||
|
System.out.println(ld.isBefore(ld7));//false
|
||||||
|
System.out.println(ld.isAfter(ld7));//true
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user