首页 > 编程语言 >Java中的日期

Java中的日期

时间:2022-11-13 15:58:58浏览次数:67  
标签:Java time parse 日期 LocalDateTime date LocalDate LocalTime

  • https://www.baeldung.com/java-8-date-time-intro
  • Issues With the Existing Date/Time APIS:
    • Thread safety
    • API design and ease of understanding
    • ZonedDate and Time
  • Working With LocalDate
    The LocalDate represents a date in ISO format(yyyy-MM-dd) without time. We can use it to store dates like birthdays and paydays.
    LocalDate localDate = LocalDate.now();
    
    And we can get the localDate representing a specific day, month and year by using of method or the parse method.
    LocalDate.of(2015, 02, 20);
    LocalDate.parse("2015-02-20");
    
    The LocalDate provides various utility methods to obtain a variety of information
    LocalDate tomorrow = LocalDate.now().plusDays(1); // get tomorrow date
    
    subtracts one month. Note how it accepts an enum as the time unit:
    LocalDate previousMonthSameDay = LocalDate.now().minus(1, ChronoUnit.MONTHS);
    
    the relationship of a date to another can be determined to occur before or after date:
    boolean notBefore = LocalDate.parse("2016-06-12").isBefore(LocalDate.parse("2016-06-11"));
    boolean isAfter = LocalDate.parse("2016-06-12").isAfter(LocalDate.parse("2016-06-11"));
    
  • Working With LocalTime
    The LocalTime represent time without a date.
    Similar to LocalDate, we can create an instance of LocalTime from the system clock or by using parse and of method.
    LocalTime now = LocalTime.now();
    LocalTime sixThirty = LocalTime.parse("06:30");
    LocalTime sixThirty1 = LocalTime.of(6, 30);
    
  • Working With LocalDateTime
    LocalDateTime is used to represent a combination of date and time. This is the most commonly used class when we need a combination of date and time.
    LocalDateTime.now();
    LocalDateTime.of(2015, Month.FEBRUARY, 20, 06, 30); // 2015-02-20 06:30
    
    // addition and subtration of specific units of time like days, months, years and minutes
    localDateTime.plusDays(1);
    localDateTime.minusHours(2);
    
    // Getter methods are also available to extract specific units similar to the date and times
    localDateTime.getMonth();
    
  • Using Period and Duration
    The Period class represents a quantity of time in terms of years, months and days, and the Duration class represents a quantity of time in terms of seconds and nanoseconds.
    LocalDate today = LocalDate.now();
    LocalDate finalDate = today.plus(Period.ofDays(5));
    long five = ChronoUnit.DAYS.between(today, finalDate);
    
  • Compatibility With Date and Calendar
    Java 8 has added the toInstant() method, which helps to convert existing Date and Calendar instance to new Date and Time API:
    https://stackoverflow.com/questions/19431234/converting-between-java-time-localdatetime-and-java-util-date
    // date to LocalDateTime
    LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    LocalDateTime ldt = LocalDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault());
    
    // LocalDateTime to date
    Date out = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
    
  • Backport and Alternate Options
    For organizations the are on the path of moving to Java 8 from Java 7 or Java 6 and that want to use date and time API, use Joda-Time library
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.9.4</version>
    </dependency>
    

标签:Java,time,parse,日期,LocalDateTime,date,LocalDate,LocalTime
From: https://www.cnblogs.com/zhuliang2222/p/16886080.html

相关文章

  • Sql 中常用日期转换Convert(Datetime)
    CONVERT(data_type,expression[,style]) convert(varchar(10),字段名,转换格式)说明:此样式一般在时间类型(datetime,smalldatetime)与字符串类型(nchar,nvarchar,char,v......
  • Idea编辑器debug java代码时,怎么能进去JDK源码?
    方式1:强制进入:alt+shift+F7方式2:mac电脑上,先点击Preferences找到[Build,Execution,Deployment]=>找到Stepping......
  • JavaScript闭包详细讨论
    functiont1(){ varage=20; functiont2(){ alert(age); } returnt2;}在js中,t1执行过程中,又生成了t2,而从作用域上来说t2能访问到age=20,返回t2时,于是"a......
  • IDEA快捷键,访问修饰符---JAVA
    一、快捷键,包的使用       newScanner().var回车自动分配变量名模板快捷键      包:本质是创建不同的文件夹来保存类文件  例子:不同......
  • Java中的抽象类详解
    概述由来父类中的方法,被它的子类们重写,子类各自的实现都不尽相同。那么父类的方法声明和方法主体,只有声明还有意义,而方法主体则没有存在的意义了。我们把没有方法主体的方......
  • Java中的super和this关键字详解
    父类空间优先于子类对象产生在每次创建子类对象时,先初始化父类空间,再创建其子类对象本身。目的在于子类对象中包含了其对应的父类空间,便可以包含其父类的成员,如果父类成员......
  • 【面经分享】阿里 Java 面试问题大全
    自取食用:https://url03.ctfile.com/f/24333903-723159415-ff4c26?p=5831【访问密码和解压密码:5831】,进入下载页面,选择【普通下载】内容如下(首页截图):......
  • JavaScript中的几种for循环效率对比
    JavaScript(下文简称JS)中最常用的数据结构有两种,即数组(下文用Array表示)和对象(下文用Object表示)。须要注意的是,本质上,数组也是一种对象,只不过是特殊的对象。遍历Array和Obje......
  • java——继承与多态——接口001
    接口概述与生活举例:                            接口的定义基本格式:            ......
  • Java:自定义排序与sort()函数
    自定义排序与Arrays.sort()本篇题目来源:2022/11/13Leetcode每日一题:https://leetcode.cn/problems/custom-sort-string给定两个字符串order和s。order的所有单词都......