//获取今天的日期publicvoidgetCurrentDate(){
LocalDate today = LocalDate.now();
System.out.println("Today's Local date : "+ today);//这个是作为对比
Date date =newDate();
System.out.println(date);}
2、获取年、月、日信息
//获取年、月、日信息publicvoidgetDetailDate(){
LocalDate today = LocalDate.now();int year = today.getYear();int month = today.getMonthValue();int day = today.getDayOfMonth();
System.out.printf("Year : %d Month : %d day : %d t %n", year, month, day);}
3、处理特定日期
//处理特定日期publicvoidhandleSpecilDate(){
LocalDate dateOfBirth = LocalDate.of(2018,01,21);
System.out.println("The specil date is : "+ dateOfBirth);}
4、判断两个日期是否相等
//判断两个日期是否相等publicvoidcompareDate(){
LocalDate today = LocalDate.now();
LocalDate date1 = LocalDate.of(2018,01,21);if(date1.equals(today)){
System.out.printf("TODAY %s and DATE1 %s are same date %n", today, date1);}}
5、检查像生日这种周期性事件
//处理周期性的日期publicvoidcycleDate(){
LocalDate today = LocalDate.now();
LocalDate dateOfBirth = LocalDate.of(2018,01,21);
MonthDay birthday = MonthDay.of(dateOfBirth.getMonth(), dateOfBirth.getDayOfMonth());
MonthDay currentMonthDay = MonthDay.from(today);if(currentMonthDay.equals(birthday)){
System.out.println("Many Many happy returns of the day !!");}else{
System.out.println("Sorry, today is not your birthday");}}
6、获取当前时间
//获取当前时间publicvoidgetCurrentTime(){
LocalTime time = LocalTime.now();
System.out.println("local time now : "+ time);}
7、在现有的时间上增加小时
//增加小时publicvoidplusHours(){
LocalTime time = LocalTime.now();
LocalTime newTime = time.plusHours(2);// 增加两小时
System.out.println("Time after 2 hours : "+ newTime);}
//如何用Java判断日期是早于还是晚于另一个日期publicvoidisBeforeOrIsAfter(){
LocalDate today = LocalDate.now();
LocalDate tomorrow = LocalDate.of(2018,1,29);if(tomorrow.isAfter(today)){
System.out.println("Tomorrow comes after today");}//减去一天
LocalDate yesterday = today.minus(1, ChronoUnit.DAYS);if(yesterday.isBefore(today)){
System.out.println("Yesterday is day before today");}}
12、处理时区
//获取特定时区下面的时间publicvoidgetZoneTime(){//设置时区
ZoneId america = ZoneId.of("America/New_York");
LocalDateTime localtDateAndTime = LocalDateTime.now();
ZonedDateTime dateAndTimeInNewYork = ZonedDateTime.of(localtDateAndTime, america );
System.out.println("现在的日期和时间在特定的时区 : "+ dateAndTimeInNewYork);}
13、如何体现出固定日期
//使用 YearMonth类处理特定的日期publicvoidcheckCardExpiry(){
YearMonth currentYearMonth = YearMonth.now();
System.out.printf("Days in month year %s: %d%n", currentYearMonth, currentYearMonth.lengthOfMonth());
YearMonth creditCardExpiry = YearMonth.of(2028, Month.FEBRUARY);
System.out.printf("Your credit card expires on %s %n", creditCardExpiry);}
14、检查闰年
//检查闰年publicvoidisLeapYear(){
LocalDate today = LocalDate.now();if(today.isLeapYear()){
System.out.println("This year is Leap year");}else{
System.out.println("2018 is not a Leap year");}}
15、计算两个日期之间的天数和月数
//计算两个日期之间的天数和月数publicvoidcalcDateDays(){
LocalDate today = LocalDate.now();
LocalDate java8Release = LocalDate.of(2018, Month.MAY,14);
Period periodToNextJavaRelease = Period.between(today, java8Release);
System.out.println("Months left between today and Java 8 release : "+ periodToNextJavaRelease.getMonths());}
16、包含时差信息的日期和时间
publicvoidZoneOffset(){
LocalDateTime datetime = LocalDateTime.of(2018, Month.FEBRUARY,14,19,30);
ZoneOffset offset = ZoneOffset.of("+05:30");
OffsetDateTime date = OffsetDateTime.of(datetime, offset);
System.out.println("Date and Time with timezone offset in Java : "+ date);}
17、获取当前的时间戳
publicvoidgetTimestamp(){
Instant timestamp = Instant.now();
System.out.println("What is value of this instant "+ timestamp);}
18、使用预定义的格式化工具解析或格式化日期
publicvoidgetTimestamp(){
Instant timestamp = Instant.now();
System.out.println("What is value of this instant "+ timestamp);}
1、获取当前的日期//获取今天的日期public void getCurrentDate(){ LocalDate today = LocalDate.now(); System.out.println("Today's Local date : " + today); //这个是作为对比 Date date = new Date(); System.out.println(date);}2、获取年、月、日信息//获取年、月、日信息public voi