Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
167 lines (137 loc) · 5.55 KB

File metadata and controls

167 lines (137 loc) · 5.55 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package java8;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
/**
* Created by yahier on 2018/1/22.
* 测试api 新的日期和时间
*/
public class TimeTest {
public static void main(String[] args) {
test1();
test2();
test3();
test4();
test5();
test6();
practice1();
practice4();
}
//时间线Instant 和时间间隔 Duration
static void test1() {
Instant instant = Instant.now();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Instant instant2 = Instant.now();
Duration duration = Duration.between(instant, instant2);
println("test1", duration.getSeconds() + "");
Duration duration2 = duration.multipliedBy(10);
println("test1", duration2.getSeconds() + "");
}
//本地日期 LocalDate
static void test2() {
LocalDate dateNow = LocalDate.now();
LocalDate date = LocalDate.of(2018, 1, 1);
date = date.plusDays(100);
int year = date.getYear();
int month = date.getMonthValue();
int day = date.getDayOfMonth();
int weekDay = date.getDayOfWeek().getValue();//星期一调用 返回1
println("test2", year + "-" + month + "-" + day + " 星期" + weekDay);
if (date.isBefore(dateNow)) {
println("test2", "date.isBefore(dateNow)");
}
}
//本地时间 LocalTime
static void test3() {
LocalTime time = LocalTime.of(8, 0);
//LocalTime time = LocalTime.now();
int hour = time.getHour();
int minutes = time.getMinute();
int second = time.getSecond();
println("test3", hour + ":" + minutes + ":" + second);
}
//ZonedDateTime
static void test4() {
ZonedDateTime dateTime = ZonedDateTime.of(LocalDate.now(), LocalTime.now(), ZoneId.of("Asia/Shanghai"));
int year = dateTime.getYear();
int month = dateTime.getMonthValue();
int day = dateTime.getDayOfMonth();
int hour = dateTime.getHour();
int minutes = dateTime.getMinute();
println("test4", year + "-" + month + "-" + day + " " + hour + ":" + minutes);
}
//格式化日期
static void test5() {
//DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;//2018-01-23
//DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;//2018-01-23
//DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_TIME;//09:09:24.254
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//2018-01-23 09:12:05
LocalDateTime dateTime = LocalDateTime.now();
String format1 = dateTime.format(formatter);
println("test5", format1);
//将格式化的String还原回日期
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime localDateTime = LocalDateTime.parse("2018-01-23 15:02", formatter2);
println("test5", "hour:" + localDateTime.getHour());
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy MM dd");
LocalDate date = LocalDate.parse("2017 06 17", formatter3);
println("test5", "date:" + formatter3.format(date));
}
//与旧有类交互
static void test6() {
Date date = new Date();
Instant instant = date.toInstant();
Calendar calendar = Calendar.getInstance();
Date date2 = calendar.getTime();
println("test6", date.getTime() + ":" + date2.getTime());
LocalDate localDate = LocalDate.now();
//date calendar怎么与LocalDate LocalTime转换是不知道了。
ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("Asia/Shanghai"));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//2018-01-23 09:12:05
String formatTime2 = zonedDateTime.format(formatter);
println("test6", formatTime2);
}
//计算今年的程序员节的日期(每年的第256天
static void practice1() {
LocalDate date = LocalDate.of(2018, 1, 1);
date = date.plusDays(255);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
println("practice1", date.format(formatter));
//不使用plus方法的话
}
//计算活了多少天
static void practice4() {
LocalDate dateNow = LocalDate.now();
LocalDate dateBirth = LocalDate.of(1990, 1, 11);
Period period = Period.between(dateBirth, dateNow);
int years = period.getYears();
int months = period.getMonths();
int days = period.getDays();
println("practice4", years + "," + months + "," + days);
//以上的算法太麻烦了
//最简单的计算
long daysDiff = ChronoUnit.DAYS.between(dateBirth, dateNow);
println("practice4", "daysDiff: " + daysDiff);//good
LocalTime time1 = LocalTime.of(14, 5);
LocalTime time2 = LocalTime.of(16, 23);
long timeDiff = ChronoUnit.MINUTES.between(time1, time2);
println("practice4", "timeDiff: " + timeDiff);//good
}
private static void println(String tag, String value) {
System.out.println(tag + "," + value);
System.out.println();
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.