Java8新特性 - LocalTime 时间类的基本使用

1.说明

LocalTime 是 Java8 中新增的 时间类,主要包含了 小时、分钟、秒、纳秒 四个属性。
LocalTime类中提供了丰富的API,帮助我们更加简便的操作时间对象。

2.常用API

2.1 创建LocalTime类对象

 * 1.创建时间类对象
 *    1.1 获取当前时间
 *      LocalTime.now() : 获取默认时区下的系统时钟的时间
 *      LocalTime.now(Clock clock) : 获取指定时钟的时间
 *      LocalTime.now(ZoneId zone) : 获取指定时区的、默认系统时钟的时间
 *      【补充:获取所有时区信息的方式 : ZoneId.getAvailableZoneIds()】
 *    1.2 获取指定时间
 *      * 小时的取值范围 : [0,23]
 *      * 分钟的取值范围 : [0,59]
 *      * 秒  的取值范围 : [0,59]
 *      * 纳秒的取值范围 : [0,999,999,999]
 *
 *      LocalTime.of(int hour, int minute) : 指定小时和分钟,秒和纳秒为0
 *      LocalTime.of(int hour, int minute, int second) : 指定小时、分钟、秒,纳秒为0
 *      LocalTime.of(int hour, int minute, int second, int nanoOfSecond) : 指定小时、分钟、秒、纳秒
 *      LocalTime.ofSecondOfDay(long secondOfDay) : 指定一天当中的多少秒,纳秒将被置为0
 *      LocalTime.ofNanoOfDay(long nanoOfDay) : 指定一天当中的多少纳秒
 *

代码

package com.northcastle.K_Date;

import java.time.Clock;
import java.time.Duration;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjuster;

public class LocalTimeTest {
    public static void main(String[] args) {
        //1.1 获取当前时间
        LocalTime now01 = LocalTime.now(); // 我们是中国北京,东八区
        System.out.println("now01 = " + now01);
        LocalTime now02 = LocalTime.now(Clock.systemUTC()); // UTC/GMT 标准时区时间,零时区/中时区
        System.out.println("now02 = " + now02);
        LocalTime now03 = LocalTime.now(ZoneId.of("America/Panama"));  // Asia/Shanghai 东八区 ;America/Panama 西五区
        System.out.println("now03 = " + now03);
        System.out.println("===========================");

        //1.2 获取指定时间
        LocalTime localTime01 = LocalTime.of(18, 10);
        System.out.println("localTime01 = " + localTime01);
        LocalTime localTime02 = LocalTime.of(19, 44, 56);
        System.out.println("localTime02 = " + localTime02);
        LocalTime localTime03 = LocalTime.of(19, 45, 12, 126);
        System.out.println("localTime03 = " + localTime03);
        LocalTime localTime04 = LocalTime.ofSecondOfDay(56);
        System.out.println("localTime04 = " + localTime04);
        LocalTime localTime05 = LocalTime.ofNanoOfDay(100);
        System.out.println("localTime05 = " + localTime05);
        System.out.println("===========================");

		//获取所有可用的时区
		//        List zoneIds = ZoneId.getAvailableZoneIds().stream()
		//                .filter(s -> s.startsWith("Ame"))
		//                .collect(Collectors.toList());
		//        System.out.println(zoneIds);
    }
}

运行结果

Java8新特性 - LocalTime 时间类的基本使用_第1张图片

2.2 获取LocalTime对象的时分秒

 * 2.获取时间对象的 时、分、秒、纳秒、信息
 *      getHour() : 获取小时信息
 *      getMinute() : 获取分钟信息
 *      getSecond() : 获取秒
 *      getNano() :  获取纳秒
 *

代码

package com.northcastle.K_Date;

import java.time.Clock;
import java.time.Duration;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjuster;

public class LocalTimeTest {
    public static void main(String[] args) {
       //2.获取时间对象的 时、分、秒、纳秒
        LocalTime now = LocalTime.now();
        System.out.println("now = " + now);
        System.out.println("小时 : " + now.getHour());
        System.out.println("分钟 : " + now.getMinute());
        System.out.println("秒   : " + now.getSecond());
        System.out.println("纳秒 : " + now.getNano());

        System.out.println("===========================");
    }
}

运行结果

Java8新特性 - LocalTime 时间类的基本使用_第2张图片

2.3 指定LocalTime对象的时分秒信息

 * 3.指定时间对象的 时、分、秒、纳秒
 *      withHour(int) : 指定小时
 *      withMinute(int) : 指定分钟
 *      withSecond(int) : 指定秒
 *      withNano(int) : 指定毫秒
 *
 *      with(TemporalAdjuster) : 时间矫正器
 *

代码

package com.northcastle.K_Date;

import java.time.Clock;
import java.time.Duration;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjuster;

public class LocalTimeTest {
    public static void main(String[] args) {
       //3.指定时、分、秒、纳秒
        LocalTime nowWith = LocalTime.now();
        System.out.println("nowWith = " + nowWith);
        System.out.println("指定小时6点 = " + nowWith.withHour(6));
        System.out.println("指定分钟5分钟 = " + nowWith.withMinute(50));
        System.out.println("指定秒5秒 = " + nowWith.withSecond(5));
        System.out.println("指定纳秒123000000 = " + nowWith.withNano(123000000));

        System.out.println();

        //时间矫正器
        TemporalAdjuster temporalAdjuster = (temporal)->{
            System.out.println("temporal = " + temporal);
            LocalTime localTime = (LocalTime) temporal;
            localTime = localTime.withHour(localTime.getHour()+1)
                        .withMinute(30)
                        .withSecond(30)
                        .withNano(300000000);
            System.out.println("localTime = " + localTime);
            return localTime;
        };
        LocalTime withTemporalAdjuster01 = nowWith.with(temporalAdjuster);
        System.out.println("withTemporalAdjuster01 = " + withTemporalAdjuster01);

        System.out.println("===========================");
    }
}

运行结果

Java8新特性 - LocalTime 时间类的基本使用_第3张图片

2.4 增加/减少 LocalTime 对象的时分秒信息

 * 4.加上 或者 减去 时、分、秒、纳秒
 *     plusHours(long) : 加几个小时
 *     plusMinutes(long) : 加几分钟
 *     plusSeconds(long) : 加几秒钟
 *     plusNanos(long) : 加几个纳秒
 *
 *     minusHours(long) : 减几个小时
 *     minusMinutes(long) : 减几分钟
 *     minusSeconds(long) : 减几秒
 *     minusNanos(long) : 减几个纳秒
 *

代码

package com.northcastle.K_Date;

import java.time.Clock;
import java.time.Duration;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjuster;

public class LocalTimeTest {
    public static void main(String[] args) {
       //4.加上 或者 减去 小时、分钟、秒、纳秒
        LocalTime nowPlusOrMin = LocalTime.now();
        System.out.println("nowPlusOrMin = " + nowPlusOrMin);
        System.out.println("加两小时 = " + nowPlusOrMin.plusHours(2));
        System.out.println("加20分钟 = " + nowPlusOrMin.plusMinutes(20));
        System.out.println("加20秒 = " + nowPlusOrMin.plusSeconds(20));
        System.out.println("加1纳秒 = " + nowPlusOrMin.plusNanos(1));
        
        System.out.println();
        
        System.out.println("减两小时 = " + nowPlusOrMin.minusHours(2));
        System.out.println("减20分钟 = " + nowPlusOrMin.minusMinutes(20));
        System.out.println("减20秒 = " + nowPlusOrMin.minusSeconds(20));
        System.out.println("减1纳秒 = " + nowPlusOrMin.minusNanos(1));

        System.out.println("===========================");
    }
}

运行结果

Java8新特性 - LocalTime 时间类的基本使用_第4张图片

2.5 LocalTime 对象的大小比较

 *  5.比较时间对象的大小
 *      time01.isAfter(time02) : time01 是否比 time02 晚
 *      time01.isBefore(time02) : time01 是否比 time02 早
 *

代码

package com.northcastle.K_Date;

import java.time.Clock;
import java.time.Duration;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjuster;

public class LocalTimeTest {
    public static void main(String[] args) {
      
		//5.比较两个时间对象的大小
        LocalTime time01 = LocalTime.now();
        LocalTime time02 = LocalTime.of(21, 28, 30);
        System.out.println("time01 = " + time01);
        System.out.println("time02 = " + time02);
        System.out.println("time01是否比time02 晚 : " + time01.isAfter(time02));
        System.out.println("time01是否比time02 早 : " + time01.isBefore(time02));

        System.out.println("===========================");
    }
}

运行结果

Java8新特性 - LocalTime 时间类的基本使用_第5张图片

2.6 LocalTiime 对象格式化为字符串

 *  6.时间对象 与 字符串的相互转化
 *    6.1 时间对象 转 字符串 format() + DateTimeFormatter类
 *        String format01 = localTime.format(DateTimeFormatter.ISO_TIME); // 21:56:17.05
 *        String format02 = localTime.format(DateTimeFormatter.ISO_LOCAL_TIME); // 21:56:17.05
 *        String format03 = localTime.format(DateTimeFormatter.ofPattern("HH--:--mm--:--ss")); // 21--:--56--:--17
 *
 *    6.2 字符串 转 时间对象 parse()
 *         LocalTime timeParse01 = LocalTime.parse("21:53:53", DateTimeFormatter.ofPattern("HH:mm:ss"));
 *

代码

package com.northcastle.K_Date;

import java.time.Clock;
import java.time.Duration;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjuster;

public class LocalTimeTest {
    public static void main(String[] args) {
       //6.1 时间对象 转 字符串
        LocalTime timeFormat01 = LocalTime.now();
        System.out.println("timeFormat01 = " + timeFormat01);

        String format01 = timeFormat01.format(DateTimeFormatter.ISO_TIME); // 21:56:17.05
        System.out.println("format01(ISO_TIME) = " + format01);

        String format02 = timeFormat01.format(DateTimeFormatter.ISO_LOCAL_TIME); // 21:56:17.05
        System.out.println("format02(ISO_LOCAL_TIME) = " + format02);

        String format03 = timeFormat01.format(DateTimeFormatter.ofPattern("HH--:--mm--:--ss")); // 21--:--56--:--17
        System.out.println("format03 = " + format03);

        System.out.println();
        //6.2 字符串 转 时间对象
        LocalTime timeParse01 = LocalTime.parse("21:53:53", DateTimeFormatter.ofPattern("HH:mm:ss"));
        System.out.println("timeParse01 = " + timeParse01);

        System.out.println("===========================");

    }
}

运行结果

Java8新特性 - LocalTime 时间类的基本使用_第6张图片

2.7 计算两个时间对象之间的时间差

 *  7.获取两个时间对象之间的差 Duration
 *         Duration between = Duration.between(timeDuration01, timeDuration02);
 *         between.toDays()
 *         between.toHours() // 一共差多少个小时
 *         between.toMinutes() // 一共差多少个分钟
 *         between.toMillis() // 一共差多少毫秒
 *         between.toNanos() // 一共差多少纳秒
 *

代码

package com.northcastle.K_Date;

import java.time.Clock;
import java.time.Duration;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjuster;

public class LocalTimeTest {
    public static void main(String[] args) {
        //7.获取两个时间对象之间的差
        LocalTime timeDuration01 = LocalTime.of(22, 38, 50);
        System.out.println("timeDuration01 = " + timeDuration01);
        LocalTime timeDuration02 = LocalTime.of(22, 56, 32);
        System.out.println("timeDuration02 = " + timeDuration02);

        Duration between = Duration.between(timeDuration01, timeDuration02);
        System.out.println("between.toDays() = " + between.toDays());
        System.out.println("between.toHours() = " + between.toHours()); // 一共差多少个小时
        System.out.println("between.toMinutes() = " + between.toMinutes()); // 一共差多少个分钟
        System.out.println("between.toMillis() = " + between.toMillis()); // 一共差多少毫秒
        System.out.println("between.toNanos() = " + between.toNanos()); // 一共差多少纳秒

        System.out.println();

        System.out.println("ChronoUnit.HOURS.between(timeDuration01,timeDuration02) = " + ChronoUnit.HOURS.between(timeDuration01, timeDuration02));
        System.out.println("ChronoUnit.MINUTES.between(timeDuration01,timeDuration02) = " + ChronoUnit.MINUTES.between(timeDuration01, timeDuration02));
        System.out.println("ChronoUnit.MILLIS.between(timeDuration01,timeDuration02) = " + ChronoUnit.MILLIS.between(timeDuration01, timeDuration02));
        System.out.println("ChronoUnit.NANOS.between(timeDuration01,timeDuration02) = " + ChronoUnit.NANOS.between(timeDuration01, timeDuration02));

        System.out.println("===========================");

    }
}

运行结果

Java8新特性 - LocalTime 时间类的基本使用_第7张图片

3.完成

Congratulations!
You are one step closer to success!

你可能感兴趣的:(JAVA基础篇,java,Java8新特性,LocalTime,时间大小比较,时间格式化)