博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java LocalTime
阅读量:2532 次
发布时间:2019-05-11

本文共 4691 字,大约阅读时间需要 15 分钟。

Java LocalTime class belongs to the .

Java LocalTime类属于 。

Java LocalTime (Java LocalTime)

  • LocalTime provides time without any time zone information. It is very much similar to observing the time from a wall clock which just shown the time and not the time zone information.

    LocalTime提供的时间没有任何时区信息。 这与从墙上时钟观察时间非常相似,后者只是显示时间而不是时区信息。
  • The assumption from this API is that all the calendar system uses the same way of representing the time.

    该API的假设是所有日历系统都使用相同的时间表示方式。
  • It is a value-based class, so use of reference equality (==), identity hash code or synchronization on instances of LocalTime may have unexpected results and is highly advised to be avoided. The equals method should be used for comparisons.

    这是一个基于值的类,因此在本地时间实例上使用引用相等(==),标识哈希码或同步可能会产生意外结果,因此强烈建议避免使用。 equals方法应用于比较。
  • The LocalTime class is that mean any operation on the object would result in a new instance of LocalTime reference.

    LocalTime类是 ,这意味着对该对象进行的任何操作都会导致LocalTime引用的新实例。

如何创建LocalTime对象 (How to Create LocalTime Object)

LocalTime objects can be created using the below mentioned ways.

可以使用以下提到的方式创建LocalTime对象。

  1. LocalTime instance can be created using the now() method of LocalTime class. There are two other overloaded now() methods that takes argument as Clock and ZoneId.
    LocalTime lt = LocalTime.now();System.out.println(lt); //15:43:43.212787LocalTime lt1 = LocalTime.now(Clock.systemDefaultZone());System.out.println(lt1); //15:43:43.213454LocalTime lt2 = LocalTime.now(ZoneId.systemDefault());System.out.println(lt2); //15:43:43.213542

    可以使用LocalTime类的now()方法创建LocalTime实例。 还有另外两个重载的now()方法,它们的参数为ClockZoneId
  2. We can create LocalTime instance using of() method. There are multiple overloaded methods for different arguments for hour, minute, second and nanosecond.
    LocalTime lt = LocalTime.of(10, 30);System.out.println(lt); //10:30LocalTime lt1 = LocalTime.of(10, 30, 45);System.out.println(lt1); //10:30:45LocalTime lt2 = LocalTime.of(10, 30, 45, 12345);System.out.println(lt2); //10:30:45.000012345 notice the left paddingLocalTime lt3 = LocalTime.ofInstant(Instant.now(), ZoneId.systemDefault());System.out.println(lt3); //15:48:58.358195

    我们可以使用of()方法创建LocalTime实例。 对于小时,分钟,秒和纳秒级的不同参数,有多种重载方法。
  3. We can use LocalTime parse() method to convert String to instance of LocalTime.
    LocalTime lt = LocalTime.parse("10:30");System.out.println(lt); //10:30LocalTime lt1 = LocalTime.parse("10:30:45.1234");System.out.println(lt1); //10:30:45.123400LocalTime lt2 = LocalTime.parse("10.3.45.1234", DateTimeFormatter.ofPattern("H.m.s.n"));System.out.println(lt2); //10:03:45.000001234

    我们可以使用LocalTime parse()方法将String转换为LocalTime的实例。

Java LocalTime方法 (Java LocalTime Methods)

There are methods in LocalTime class for different purposes. We have divided it into following categories.

LocalTime类中有一些用于不同目的的方法。 我们将其分为以下几类。

  • Getting time from LocalTime – getHour(), getMinute(), getSecond() and getNano().

    从LocalTime获取时间– getHour()getMinute()getSecond()getNano()
  • Time manipulation – plusHours(), minusHours(), plusMinutes(), minusMinutes(), plusSeconds(), minusSeconds(), plusNanos() and minusNanos().

    时间操纵– plusHours()minusHours()plusMinutes()minusMinutes()plusSeconds()minusSeconds()plusNanos()minusNanos()
  • Comparison in LocalTime – isAfter() and isBefore() to check if this LocalTime is after/before the specified time. We can use these methods to compare two local times.

    isAfter()比较– isAfter()isBefore()以检查此isBefore()是否在指定时间之后/之前。 我们可以使用这些方法来比较两个当地时间。

Java LocalTime示例 (Java LocalTime Example)

Let’s look at java LocalTime example program to explore all the above mentioned methods.

让我们看一下Java LocalTime示例程序,以探索上述所有方法。

package com.journaldev.java;import java.time.LocalTime;public class LocalTimeExample {	public static void main(String[] args) {		LocalTime lt = LocalTime.now();		System.out.println("Current Time: " + lt);		System.out.println("\nHour: " + lt.getHour());		System.out.println("Minute: " + lt.getMinute());		System.out.println("Second: " + lt.getSecond());		System.out.println("Nanosecond: " + lt.getNano());		// add 2 hours, 10 minutes, 30 seconds and 1000 nanoseconds		lt = lt.plusHours(2);		lt = lt.plusMinutes(10);		lt = lt.plusSeconds(30);		lt = lt.plusNanos(1000);		System.out.println("\nUpdated Time: " + lt);		// minus 2 hours, 10 minutes, 30 seconds and 1000 nanoseconds		lt = lt.minusHours(2);		lt = lt.minusMinutes(10);		lt = lt.minusSeconds(30);		lt = lt.minusNanos(1000);		System.out.println("\nUpdated Time: " + lt);		LocalTime ct = LocalTime.now();		System.out.println("\nlt before ct ? " + lt.isBefore(ct));		System.out.println("lt after ct ? " + lt.isAfter(ct));	}}

That’s all for Java LocalTime example programs.

Java LocalTime示例程序就这些了。

Reference:

参考:

翻译自:

转载地址:http://iumzd.baihongyu.com/

你可能感兴趣的文章
小D课堂 - 新版本微服务springcloud+Docker教程_6-03 高级篇幅之zuul常用问题分析
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-08 断路器监控仪表参数
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-02 springcloud网关组件zuul
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-1.快速搭建SpringBoot项目,采用Eclipse...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-4.在线教育后台数据库设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-3.热部署在Eclipse和IDE里面的使用...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-3.在线教育站点需求分析和架构设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-4.后端项目分层分包及资源文件处理...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-2.快速搭建SpringBoot项目,采用IDEA...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-5.PageHelper分页插件使用
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-6.微信扫码登录回调本地域名映射工具Ngrock...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-8.用户模块开发之保存微信用户信息...
查看>>
Linux下Nginx安装
查看>>
LVM扩容之xfs文件系统
查看>>
Hbase记录-client访问zookeeper大量断开以及参数调优分析(转载)
查看>>
代码片段收集
查看>>
vue-cli3创建项目时报错
查看>>
输入1-53周,输出1-53周的开始时间和结束时间
查看>>
实验二
查看>>
shell——按指定列排序
查看>>