Skip to content

Commit 61cccd9

Browse files
committed
1. java 8 time 案例
#bysocket
1 parent c3b9dc8 commit 61cccd9

File tree

6 files changed

+123
-1
lines changed

6 files changed

+123
-1
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55

66
<groupId>org.javacore</groupId>
7-
<artifactId>javacore</artifactId>
7+
<artifactId>java-core-learning-example</artifactId>
88
<version>0.0.1-SNAPSHOT</version>
99
<build>
1010
<plugins>
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.javacore.time;
2+
3+
import java.time.Duration;
4+
import java.time.Instant;
5+
import java.util.concurrent.TimeUnit;
6+
7+
/**
8+
* 持续时间类 Duration
9+
*
10+
* Created by bysocket on 16/8/23.
11+
*/
12+
public class DurationTest {
13+
public static void main(String[] args) throws InterruptedException {
14+
Instant start = Instant.now();
15+
TimeUnit.SECONDS.sleep(3);
16+
Instant end = Instant.now();
17+
18+
// 获取持续时间
19+
Duration timeElapsed = Duration.between(start,end);
20+
System.out.println(timeElapsed.toMillis());// 毫秒
21+
System.out.println(timeElapsed.toNanos());// 纳
22+
23+
Instant start1 = Instant.now();
24+
TimeUnit.SECONDS.sleep(2);
25+
Instant end1 = Instant.now();
26+
27+
// 获取持续时间
28+
Duration timeElapsed1 = Duration.between(start1,end1);
29+
30+
// 添加操作
31+
Duration all = timeElapsed.plus(timeElapsed1);
32+
System.out.println(all.toMillis());// 毫秒
33+
}
34+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.javacore.time;
2+
3+
import java.time.Instant;
4+
5+
/**
6+
* 瞬间类 Instant
7+
*
8+
* Created by bysocket on 16/7/12.
9+
*/
10+
public class InstantTest {
11+
public static void main(String[] args) {
12+
// 获取现在的时间
13+
Instant now = Instant.now();
14+
System.out.println(now);
15+
16+
// 1000000000 年 12月 31日
17+
Instant max = Instant.MAX;
18+
System.out.println(max);
19+
20+
// 10亿年前
21+
Instant min = Instant.MIN;
22+
System.out.println(min);
23+
}
24+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.javacore.time;
2+
3+
import java.time.LocalDate;
4+
5+
/**
6+
* Created by bysocket on 16/8/23.
7+
*/
8+
public class LocalDateTest {
9+
public static void main(String[] args) {
10+
// 今天的日期
11+
LocalDate localDate = LocalDate.now();
12+
13+
System.out.println("今天:" + localDate);
14+
15+
// 年
16+
System.out.println("年:" + localDate.getYear());
17+
// 月
18+
System.out.println("月:" + localDate.getMonth());
19+
System.out.println("月:" + localDate.getMonth());
20+
// 星期
21+
System.out.println("今天是星期" + localDate.getDayOfWeek());
22+
23+
// 距离
24+
// 年
25+
System.out.println("今天是今年的第" + localDate.getDayOfYear() + "天");
26+
// 月
27+
System.out.println("今天是这个月的第" + localDate.getDayOfMonth() + "天");
28+
}
29+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.javacore.time;
2+
3+
import java.time.LocalDate;
4+
import java.util.concurrent.TimeUnit;
5+
6+
/**
7+
* Created by bysocket on 16/8/23.
8+
*/
9+
public class LocalDateTest1 {
10+
public static void main(String[] args) throws InterruptedException {
11+
LocalDate start = LocalDate.now();
12+
TimeUnit.SECONDS.sleep(3);
13+
LocalDate end = LocalDate.now();
14+
15+
System.out.println(start.isAfter(end));
16+
System.out.println(start.isBefore(end));
17+
}
18+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.javacore.time;
2+
3+
import java.time.LocalDate;
4+
5+
/**
6+
* 计算BYSocket的生日是今年的第几天
7+
*
8+
* Created by bysocket on 16/8/23.
9+
*/
10+
public class LocalDateTest2 {
11+
public static void main(String[] args) {
12+
LocalDate birthdayDate = LocalDate.of(2016,5,2);
13+
System.out.println("BYSocket的生日是今年的第" + birthdayDate.getDayOfYear() + "天");
14+
// 明年的生日
15+
System.out.println(birthdayDate.plusYears(1));
16+
}
17+
}

0 commit comments

Comments
 (0)