概述
package da.test3.java8;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Set;
import org.junit.Test;
public class TestLocalDateTime {
// 6.ZonedDate、ZonedTime、ZonedDateTime : 带时区的时间或日期
@Test
public void test8() {
LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
System.out.println(ldt);
LocalDateTime ldt2 = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
ZonedDateTime zone = ldt2.atZone(ZoneId.of("Asia/Shanghai"));
System.out.println(zone);
}
@Test
public void test7() {
Set<String> zoneIds = ZoneId.getAvailableZoneIds();
zoneIds.forEach(System.out::println);
}
// 5. DateTimeFormatter : 解析和格式化日期或时间
@Test
public void test6() {
DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;
LocalDateTime ldt = LocalDateTime.now();
String str = ldt.format(dtf);
System.out.println(str);
System.out.println("-----------------------");
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String newDate = dtf2.format(ldt);
System.out.println(newDate);
LocalDateTime ldt2 = LocalDateTime.parse(newDate, dtf2);
System.out.println(ldt2);
}
// 4. TemporalAdjuster : 时间校正器
@Test
public void test5() {
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
LocalDateTime ldt2 = ldt.withDayOfMonth(10);
System.out.println(ldt2);
LocalDateTime ldt3 = ldt.with(TemporalAdjusters.next(DayOfWeek.SATURDAY));
System.out.println(ldt3);
// 自定义:下一个工作日
LocalDate ld = LocalDate.now().with((l) -> {
LocalDate ld1 = (LocalDate) l;
DayOfWeek dow = ld1.getDayOfWeek();
if (dow.equals(DayOfWeek.FRIDAY)) {
return ld1.plusDays(3);
} else if (dow.equals(DayOfWeek.SATURDAY)) {
return ld1.plusDays(2);
} else {
return ld1.plusDays(1);
}
});
System.out.println("下一个工作日是:" + ld);
}
// 3.
// Duration : 用于计算两个“时间”间隔
// Period : 用于计算两个“日期”间隔
@Test
public void test4() {
LocalDate ld1 = LocalDate.now();
LocalDate ld2 = LocalDate.of(2014, 12, 21);
Period period = Period.between(ld2, ld1);
System.out.println(period);
System.out.println(period.getYears() + "-" + period.getMonths() + "-" + period.getDays());
}
@Test
public void test3() {
Instant ins1 = Instant.now();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Instant ins2 = Instant.now();
Duration between = Duration.between(ins1, ins2);
System.out.println(between.getSeconds());
LocalTime lt1 = LocalTime.now();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
LocalTime lt2 = LocalTime.now();
System.out.println(Duration.between(lt1, lt2).toMillis());
}
// 2. Instant : 时间戳。 (使用 Unix 元年 1970年1月1日 00:00:00 所经历的毫秒值)
@Test
public void test2() {
Instant it = Instant.now();// 默认使用 UTC 时区
System.out.println(it);
OffsetDateTime odt = it.atOffset(ZoneOffset.ofHours(8));
System.out.println(odt);
System.out.println(it.toEpochMilli());
Instant it2 = Instant.ofEpochSecond(1);
System.out.println(it2);
}
// 1. LocalDate、LocalTime、LocalDateTime
@Test
public void test1() {
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
LocalDateTime ldt2 = LocalDateTime.of(2016, 11, 11, 13, 25, 14);
System.out.println(ldt2);
LocalDateTime ldt3 = ldt.plusYears(2);
System.out.println(ldt3);
LocalDateTime ldt4 = ldt.minusMonths(2);
System.out.println(ldt4);
System.out.println(ldt.getYear());
System.out.println(ldt.getMonthValue());
System.out.println(ldt.getDayOfMonth());
System.out.println(ldt.getHour());
System.out.println(ldt.getMinute());
System.out.println(ldt.getSecond());
}
}
最后
以上就是忧虑火为你收集整理的JAVA8 新时间类的操作的全部内容,希望文章能够帮你解决JAVA8 新时间类的操作所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复