java.time.LocalDate:
日期--年月日
package com.wanmait.test;
import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
import java.time.format.DateTimeFormatter;
public class Test {
    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        //获取当前系统日期对象
        System.out.println("当前日期:"+now);
        //plusDays plusWeeks   plusMonths   plusYears
        //当前日期20天之后
        LocalDate nowAfter20 = now.plusDays(20);
        System.out.println("20天之后的日期:"+nowAfter20);
        //minusDays   minusWeeks  minusMonths   minusYears
        //当前日期20天之前
        LocalDate nowBefore20 = now.minusDays(20);
        System.out.println("20天之前的日期:"+nowBefore20);
        //获得当前时间是这个月的第几天
        int daysOfMonth = now.getDayOfMonth();
        System.out.println("今天是本月的第"+daysOfMonth+"天");
        //获得当前时间是今天的第几天
        int daysOfYear = now.getDayOfYear();
        System.out.println("今天是本年第"+daysOfYear+"天");
        //getYear  getMonth
        //获得月份
        Month month = now.getMonth();
        System.out.println("本月:"+month.getValue());
        //判断是不是闰年
        boolean flagLeap = now.isLeapYear();
        System.out.println("是否闰年:"+flagLeap);
        LocalDate yesterday = LocalDate.of(2021,11,12);
        //获得指定日期的对象
        System.out.println("指定昨天日期:"+yesterday);
        //比较两个日期早晚
        boolean flag = now.isAfter(yesterday);
        System.out.println("yesterday是否在now之后:"+flag);
        //计算now和nowBefore20之间的时间差
        Period period = now.until(nowBefore20);
        System.out.println("now和nowBefore20相差:"+period.getDays()+"天");
        //字符串转换成日期
        LocalDate date = LocalDate.parse("2021-12-07",DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println(date);
    }
}now()获得当前的系统日期
parse()字符串转换成LocalDate对象,需要利用DateTimeFormatter设置日期的格式
util()计算日期之间的差
plus...()日期增加
minus...()日期减少

java.time.LocalTime:
时间--小时分秒纳秒
package com.wanmait.test;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
public class TestTime {
    public static void main(String[] args) {
        //获得当前系统时间对象
        LocalTime now = LocalTime.now();
        System.out.println("现在:"+now);
        //plusSeconds()  plusHours()  plusMinutes()
        //now时间增加
        LocalTime nowAfter58 = now.plusMinutes(58);
        System.out.println("58分钟之后:"+nowAfter58);
        //minusHours()  minusSeconds()   minusMinutes()
        //now时间减少
        LocalTime nowBefore30 = now.minusMinutes(30);
        System.out.println("30分钟之前:"+nowBefore30);
        //getHour() getMinute()  getSecond()
        long second = now.getSecond();
        System.out.println("当前时间的秒数:"+second);
        //NANOS  HOURS小时  MINUTES分  SECONDS秒
        //计算两个时间差
        long seconds = now.until(nowBefore30, ChronoUnit.SECONDS);
        System.out.println("now和nowBefore30相差秒数:"+seconds);
        //字符串转换成时间
        LocalTime time = LocalTime.parse("10:11:12");
        System.out.println(time);
    }
}now()当前系统时间
minus...()时间减少
plus...()时间增加
parse...()字符串转换成LocalTime对象
util()计算时间的差

java.time.LocalDateTime:
时间--年月日小时分秒
package com.wanmait.test;
import java.time.LocalDateTime;
public class TestDateTime {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
    }
}
 
					
						
									
					
					
						
									
					
					
						
									
					
						
									
					
						
									
					
						
									
					
						
									
					
						
									
					
						
									
					
						
									
					
0条评论
点击登录参与评论