Time using java class

 
<div style="background-color:black">
jshell> LocalDate.now()
$46 ==> 2018-07-07

jshell> LocalDate.of(2018, 3, 30)
$47 ==> 2018-03-30

jshell> LocalTime.now()
$48 ==> 00:32:06.883656

jshell> LocalTime.of(12,43,12,33333);
$49 ==> 12:43:12.000033333

jshell> LocalDateTime.now()
$50 ==> 2018-07-07T00:32:30.335562400

jshell> LocalDateTime.of(2018, 12, 30, 12,33)
$51 ==> 2018-12-30T12:33

jshell> LocalDateTime.of(LocalDate.now(), LocalTime.now())
$52 ==> 2018-07-07T00:40:38.198318200

</div>
<div style = "background-color : pink">  

calculate date and time 
Next we look at how to create out of date and time settings and calculations: 

jshell > LocalDate d = LocalDate.now () 
d ==> 2018- 07-07 

jshell > d.plusDays (12 is ) 
$ 54 is ==> 2018-07-19 

jshell > D 
D ==> 2018-07-07 

jshell > d.plusMonths (2 ) 
$ 56 is ==> 2018-09- 07 

jshell > d.minusDays (. 5 ) 
$ 57 is ==> 2018-07-02 

jshell > d.minusWeeks (. 4 ) 
$ 58 ==> 2018-06-09 


</ div>
<div style = "background-color : black"> 
Comparing dates and times 
when we want to know at a given time or date in another time / before or after the date, we can use isBefore () and isAfter () method as follows: 

jshell > var LocalDate.of D1 = (2018,. 7,. 6 ) 
D1 ==> 2018-07-06 

jshell > var LocalDate.of D2 = (2018,. 7,. 7 ) 
D2 ==> 2018- 07-07 

jshell > d1.isAfter (D2) 
$ 64 ==> to false 

jshell > d1.isBefore (D2) 
$ 65 ==> to true 

jshell > = var DT1 LocalDateTime.of (2018,. 7,. 7, 12 is, 30 ) 
dt1 ==> 2018-07-07T12: 30 

jshell> var dt2 = LocalDateTime.of(2018, 7, 7, 14, 30)
dt2 ==> 2018-07-07T14:30

jshell> dt1.isBefore(dt2)
$68 ==> true

jshell> dt1.isAfter(dt2)
$69 ==> false


</div>
<div style = "background-color : pink"> 
in String and date / time between conversion 
a requirement we often is the character string is converted into String type of date or time, at the same time, we also need the date and time into string string for display. When previous use of java.util.Date, we generally use java.text.SimpleDateFormat to complete date / time and string conversion, date and time in the new API, we use the new java.time.format.DateTimeFormatter. 

If you follow the ISO standard date / conversion between time and string, then this thing will become very easy, because in java.time.format.DateTimeFormatter has been built ISO standard format. Let's look at the code: 

jshell > Import java.time *. 

Jshell > the LocalDateTime LDT = LocalDateTime.now () 
LDT ==> 2018-09-26T22: 58: 32.229096300 

jshell > Import java.time.format *. 

Jshell > ldt.format(DateTimeFormatter.ISO_DATE)
$4 ==> "2018-09-26"

jshell> ldt.format(DateTimeFormatter.ISO_DATE_TIME)
$5 ==> "2018-09-26T22:58:32.2290963"

jshell> ldt.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
$9 ==> "2018-09-26T22:58:32.2290963"

jshell> ldt.format(DateTimeFormatter.BASIC_ISO_DATE)
$10 ==> "20180926"

</div>

 

 

Guess you like

Origin www.cnblogs.com/xqhv587/p/11887826.html