Java1.8新特性(Time类)

1:LocalDate(日期类):

public class TimeDemo {
    public static void main(String[] args) {
        // 01:获取当前时间
        LocalDate now = LocalDate.now();
        System.out.println("当前时间:" + now);

        // 02:自定义当前时间
        LocalDate data = LocalDate.of(1997, 02, 21);
        System.out.println("自定义当前时间:" + data);

        // 03:修改时间的格式
        String dataWithString = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println("当前时间的String格式:" + dataWithString);

        // 04:解析当前时间
        LocalDate parseTime = LocalDate.parse("1997-02-21");
        System.out.println("解析当前的时间" + parseTime);

        // 05:获取今天是本月的第几天
        int dayOfMonth = LocalDate.now().getDayOfMonth();
        System.out.println("dateOfMonth:" + dayOfMonth);

        // 06:获取今天是今年的第多少天
        int dateOfYear = LocalDate.now().getDayOfYear();
        System.out.println("dateOfYear:" + dateOfYear);

        // 07:获取年
        int year = LocalDate.now().getYear();
        System.out.println("year:" + year);

        // 08:获取星期
        DayOfWeek week = LocalDate.now().getDayOfWeek();
        System.out.println(week.getValue());

        // 09:获取当前时间并给它加一定的天数
        LocalDate localDate = LocalDate.now().plusDays(5);
        System.out.println("添加一定天数后的时间:" + localDate);

        // 10:获取当前时间并给它减一定的天数
        LocalDate minusDate = LocalDate.now().minusDays(9);
        System.out.println("减去一定天数后的时间:" + minusDate);

        // 11:计算两个日期间的天数
        long dates = localDate.until(minusDate, ChronoUnit.DAYS);
        System.out.println("相差的天数:" + dates);

        // 12:计算两个日期间的天数的周数
        long until = localDate.until(minusDate, ChronoUnit.WEEKS);
        System.out.println("相差的周数:" + until);
    }
}

运行结果:

可以看到LocalDate的until方法获取的还能是负数,所以在获取相差的天数时需要注意。

LocalTime类(时间类)

        // 01:获取带毫秒值的当前时间
        LocalTime now = LocalTime.now();
        System.out.println("当前时间:" + now);

        // 02:获取不带毫秒值的当前时间
        LocalTime nowWithNoNano = LocalTime.now().withNano(0);
        System.out.println("不带毫秒值的当前时间:" + nowWithNoNano);

        // 03:获取不带毫秒值不带分钟的时间
        LocalTime nowWithNoSeconde = LocalTime.now().withNano(0).withSecond(0);
        System.out.println("不带毫秒不带分钟的时间:" + nowWithNoSeconde);

        // 04:获取不带毫秒值不带分钟的时间还可以这样做(格式化处理)
        String nowTime = LocalTime.now().format(DateTimeFormatter.ofPattern("hh:mm"));
        System.out.println("nowTime:" + nowTime);

        // 05:获取制定的时间
        LocalTime localTime = LocalTime.parse("06:00");
        System.out.println("指定时间:" + localTime);

        // 06:获取指定时间还可以这样做
        LocalTime localTime1 = LocalTime.of(6,00);
        System.out.println("localTime1:" + localTime1);

运行结果:

还有很多api,用到的时候去查就可以。基本用法对着ide的api能写出来知道啥意思就行了。

LocalDateTime类(时间日期类)

        // 01:获取当前时间
        LocalDateTime now = LocalDateTime.now();
        System.out.println("now: " + now);

        // 02:格式化一下当前时间
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss E"));
        System.out.println("格式化处理后的时间: " + time);

        // 03:使用with开头的方法可以设置对应的年月日等
        LocalDateTime localDateTime = LocalDateTime.now().withYear(1997);
        System.out.println("localDateTime: " + localDateTime);
        
        // 04:使用get开头的方法可以获取对应的年月日等
        int dayOfMonth = LocalDateTime.now().getDayOfMonth();
        System.out.println("dayOfMonth: " + dayOfMonth);

运些结果:


TemporalAdjusters类:

   是一个用来计算的类,提供了各种各样的计算方法。比如获取某个月的第一天,某个月的最后一天,某一年的第一天,某一年的第几天等各种计算方法;

运行结果

   

猜你喜欢

转载自blog.csdn.net/qq_36957587/article/details/84564556