java.util.Date
和java.util.Calendar
不是線程安全的類SimpleDateFormat
也是非線程安全的類Calendar
獲取月份時(shí)需要計(jì)算,因?yàn)樵路輳?開(kāi)始LocalDate
與LocalTime
站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到浮山網(wǎng)站設(shè)計(jì)與浮山網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、主機(jī)域名、虛擬主機(jī)、企業(yè)郵箱。業(yè)務(wù)覆蓋浮山地區(qū)。
LocalDate
類表示一個(gè)具體的日期,但不包含具體時(shí)間,也不包含時(shí)區(qū)信息,LocalTime
表示一個(gè)具體的時(shí)間
LocalDate localDate = LocalDate.of(2017, 1, 4); // 初始化一個(gè)日期:2017-01-04
LocalTime localTime = LocalTime.of(17, 23, 52); // 初始化一個(gè)時(shí)間:17:23:52
LocalDateTime
上述兩個(gè)類的結(jié)合體
LocalDateTime ldt1 = LocalDateTime.of(2017, Month.JANUARY, 4, 17, 23, 52);
Instant
Instant表示一個(gè)時(shí)間戳,精確到納秒
Instant instant = Instant.ofEpochSecond(120, 100000);
//參數(shù):seconds表示從1970-01-01 00:00:00開(kāi)始到現(xiàn)在的秒數(shù),nanos表示納秒部分(nanos的值不會(huì)超過(guò)999,999,999)
Duration
Duration表示一個(gè)時(shí)間段,精確到納秒
LocalDateTime from = LocalDateTime.of(2017, Month.JANUARY, 5, 10, 7, 0); // 2017-01-05 10:07:00
LocalDateTime to = LocalDateTime.of(2017, Month.FEBRUARY, 5, 10, 7, 0); // 2017-02-05 10:07:00
Duration duration = Duration.between(from, to); // 表示從 2017-01-05 10:07:00 到 2017-02-05 10:07:00 這段時(shí)間
DateTimeFormatter
類處理日期格式化java.time.ZoneId
類處理時(shí)區(qū)LocalTime now = LocalTime.now();
Time time = Time.valueOf(now);
//Get SQL time instance
Time sqlTime = new Time(new Date().getTime());
//Get LocalTime from SQL time
LocalTime localtime = sqlTime.toLocalTime();
// 01. java.util.Date --> java.time.LocalDateTime
public void UDateToLocalDateTime() {
java.util.Date date = new java.util.Date();
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
}
// 02. java.util.Date --> java.time.LocalDate
public void UDateToLocalDate() {
java.util.Date date = new java.util.Date();
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
LocalDate localDate = localDateTime.toLocalDate();
}
// 03. java.util.Date --> java.time.LocalTime
public void UDateToLocalTime() {
java.util.Date date = new java.util.Date();
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
LocalTime localTime = localDateTime.toLocalTime();
}
// 04. java.time.LocalDateTime --> java.util.Date
public void LocalDateTimeToUdate() {
LocalDateTime localDateTime = LocalDateTime.now();
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
java.util.Date date = Date.from(instant);
}
// 05. java.time.LocalDate --> java.util.Date
public void LocalDateToUdate() {
LocalDate localDate = LocalDate.now();
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
java.util.Date date = Date.from(instant);
}
// 06. java.time.LocalTime --> java.util.Date
public void LocalTimeToUdate() {
LocalTime localTime = LocalTime.now();
LocalDate localDate = LocalDate.now();
LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
java.util.Date date = Date.from(instant);
}
String zoneStr = "Asia/Shanghai";
LocalDateTime dt = LocalDateTime.now();
ZoneId zoneId = ZoneId.of(zoneStr);
ZoneDateTime zdt = dt.atZone(zoneId);
ZoneOffset offset = zdt.getOffset();
System.out.println(offset);
//output: +08:00
ZoneId australia = ZoneId.of("Australia/Sydney");
String str = "2018-08-31 17:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime localtDateAndTime = LocalDateTime.parse(str, formatter);
ZonedDateTime dateAndTimeInSydney = ZonedDateTime.of(localtDateAndTime, australia);
System.out.println("Current date and time in a particular timezone : " + dateAndTimeInSydney);
ZonedDateTime utcDate = dateAndTimeInSydney.withZoneSameInstant(ZoneOffset.UTC);
System.out.println("Current date and time in UTC : " + utcDate);
String input = "2018-08-31 17:00";
ZoneId zone = ZoneId.of("Australia/Sydney");
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").withZone(zone);
ZonedDateTime utc = ZonedDateTime.parse(input, fmt).withZoneSameInstant(UTC);
關(guān)于Java8的時(shí)間處理就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的參考價(jià)值,可以學(xué)以致用。如果喜歡本篇文章,不妨把它分享出去讓更多的人看到。