LocalDate、LocalTime、LocalDateTime的一些基本用法
阿新 • • 發佈:2021-02-02
技術標籤:JAVA基礎LocalDateTimeLocalDateLocalTimejava.time
LocalDate、LocalTime、LocalDateTime的一些基本使用者
- 1.LocalDate的基本用法
- 2.LocalTime基本用法
- 3.LocalDateTime基本用法
- 4.格式化
- 5.日期時間的簡單計算
- 6.LocalDate轉化為Date
- 7.Date轉化為LocalDate
- 8.LocalDateTime轉化為Date
- 9.Date轉化為LocalDateTime
- 10.LocalDateTime與資料庫中datetime型別的使用
- 11.示例程式碼
1.LocalDate的基本用法
@Test
public void testLocalDate() {
//獲取當前日期
LocalDate localDate = LocalDate.now();
System.out.println("獲取當前日期:" + localDate);
//自行構造日期
LocalDate localDate1 = LocalDate.of(2020, 12, 9);
System.out.println("自行構造日期:" + localDate1);
//獲取某日期的年
System.out.println("獲取某日期的年:" + localDate.getYear());
//獲取某日期的月(int)
System.out.println("獲取某日期的月(int):" + localDate.getMonthValue());
//獲取某日期的月(字串)
System.out.println("獲取某日期的月(字串):" + localDate.getMonth());
//獲取某日期的在月份中的天
System. out.println("獲取某日期在月份中的天:" + localDate.getDayOfMonth());
//獲取某日期的在週中的天(字串)
System.out.println("獲取某日期的在週中的天(字串):" + localDate.getDayOfWeek());
//獲取某日期的在週中的天int
System.out.println("獲取某日期的在週中的天(int):" + localDate.get(ChronoField.DAY_OF_WEEK));
//獲取某日期的在年中的第?天
System.out.println("獲取某日期的在年中的第?天:" + localDate1.getDayOfYear());
//獲取某年的第一天
LocalDate localDate2 = localDate.with(TemporalAdjusters.firstDayOfYear());
System.out.println("獲取某年的第一天:" + localDate2);
//獲取某年的下一年的第一天
LocalDate localDate3 = localDate.with(TemporalAdjusters.firstDayOfNextYear());
System.out.println("獲取某年的下一年的第一天:" + localDate3);
//獲取某月的第一個週五
LocalDate localDate4 = localDate.with(TemporalAdjusters.firstInMonth(DayOfWeek.FRIDAY));
System.out.println(localDate4);
//獲取某月的第一天
LocalDate localDate5 = localDate.with(TemporalAdjusters.firstDayOfMonth());
System.out.println("獲取某月的第一天:" + localDate5);
//獲取某月的下個的第一天
LocalDate localDate6 = localDate.with(TemporalAdjusters.firstDayOfNextMonth());
System.out.println("獲取某月的下個的第一天:" + localDate6);
}
執行結果:
獲取當前日期:2021-01-21
自行構造日期:2020-12-09
獲取某日期的年:2021
獲取某日期的月(int):1
獲取某日期的月(字串):JANUARY
獲取某日期在月份中的天:21
獲取某日期的在週中的天(字串):THURSDAY
獲取某日期的在週中的天(int):4
獲取某日期的在年中的第?天:344
獲取某年的第一天:2021-01-01
獲取某年的下一年的第一天:2022-01-01
2021-01-01
獲取某月的第一天:2021-01-01
獲取某月的下個的第一天:2021-02-01
2.LocalTime基本用法
@Test
public void testLocalTime() {
//獲取當前時分秒毫秒
LocalTime localTime = LocalTime.now();
System.out.println("獲取當前時分秒毫秒:" + localTime);
//自定義時分秒
LocalTime localTime1 = LocalTime.of(12, 12, 11);
System.out.println("自定義時分秒:" + localTime1);
//自定義時分
LocalTime localTime2 = LocalTime.of(12, 12);
System.out.println("自定義時分:" + localTime2);
//獲取小時
int hour = localTime.getHour();
int hour1 = localTime.get(ChronoField.HOUR_OF_DAY);
System.out.println("獲取小時:" + hour + ":::" + hour1);
//獲取分
int minute = localTime.getMinute();
int minute1 = localTime.get(ChronoField.MINUTE_OF_HOUR);
System.out.println("獲取分:" + minute + ":::" + minute1);
//獲取秒
int second = localTime.getSecond();
int second1 = localTime.get(ChronoField.SECOND_OF_MINUTE);
System.out.println("獲取秒:" + second + ":::" + second1);
}
結果:
獲取當前時分秒毫秒:10:20:46.178
自定義時分秒:12:12:11
自定義時分:12:12
獲取小時:10:::10
獲取分:20:::20
獲取秒:46:::46
3.LocalDateTime基本用法
@Test
public void testLocalDateTime() {
//獲取當前日期和時間
LocalDateTime now = LocalDateTime.now();
System.out.println("獲取當前日期和時間:" + now);
//自定義日期和時間
LocalDateTime localDateTime = LocalDateTime.of(LocalDate.of(2022, 11, 3), LocalTime.now());
System.out.println(localDateTime);
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime1 = localDate.atTime(localTime);
System.out.println("localDateTime1:" + localDateTime1);
LocalDateTime localDateTime2 = localTime.atDate(localDate);
System.out.println("localDateTime2:" + localDateTime2);
LocalDate localDate1 = now.toLocalDate();
System.out.println("localDate1:" + localDate1);
LocalTime localTime1 = now.toLocalTime();
System.out.println("localTime1:" + localTime1);
Instant instant = Instant.now();
System.out.println("instant:" + instant);
long epochSecond = instant.getEpochSecond();
System.out.println("epochSecond:" + epochSecond);
long epochMilli = instant.toEpochMilli();
System.out.println("epochMilli:" + epochMilli + ":::" + System.currentTimeMillis());
//當前日期時間加1年
LocalDateTime localDateTime3 = now.plusYears(1);
System.out.println("localDateTime3:" + localDateTime3);
LocalDateTime localDateTime6 = now.plus(2, ChronoUnit.YEARS);
System.out.println("localDateTime6:" + localDateTime3);
//當前日期時間減1年
LocalDateTime localDateTime4 = now.minusYears(1);
System.out.println("localDateTime4:" + localDateTime4);
LocalDateTime localDateTime5 = now.withYear(1999);
System.out.println("localDateTime5:" + localDateTime5);
}
結果:
BASIC_ISO_DATE:20210121
ISO_LOCAL_DATE:2021-01-21
ISO_DATE:2021-01-21
dateTimeFormatter1:2021/01/21
dateTimeFormatter2:2021/01/21 10:22:08
dateTimeFormatter3:2021-01-21 10:22:08
dateTimeFormatter4:2021.01.21 10:22:08
localDate1:1997-12-11
localDateTime1:1997-12-11T23:12:09
format8:1997-12-11 23:12:09
format9:2021年1月21日 上午10時22分08秒
format10:2021-1-21 10:22:08
format11:21-1-21 上午10:22
4.格式化
@Test
public void testLocalDateFormat() {
LocalDateTime localDateTime = LocalDateTime.now();
LocalDate localDate = localDateTime.toLocalDate();
LocalTime localTime = localDateTime.toLocalTime();
String format1 = localDate.format(DateTimeFormatter.BASIC_ISO_DATE);
System.out.println("BASIC_ISO_DATE:" + format1);
String format2 = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println("ISO_LOCAL_DATE:" + format2);
String format3 = localDate.format(DateTimeFormatter.ISO_DATE);
System.out.println("ISO_DATE:" + format3);
DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
String format4 = localDate.format(dateTimeFormatter1);
System.out.println("dateTimeFormatter1:" + format4);
DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
String format5 = localDateTime.format(dateTimeFormatter2);
System.out.println("dateTimeFormatter2:" + format5);
DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format6 = localDateTime.format(dateTimeFormatter3);
System.out.println("dateTimeFormatter3:" + format6);
DateTimeFormatter dateTimeFormatter4 = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss");
String format7 = localDateTime.format(dateTimeFormatter4);
System.out.println("dateTimeFormatter4:" + format7);
LocalDate localDate1 = LocalDate.parse("1997-12-11", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
System.out.println("localDate1:" + localDate1);
//解析日期時間
LocalDateTime localDateTime1 = LocalDateTime.parse("1997-12-11 23:12:09", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println("localDateTime1:" + localDateTime1);
String format8 = localDateTime1.format(dateTimeFormatter3);
System.out.println("format8:" + format8);
//日期本地化
String format9 = localDateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG));
System.out.println("format9:" + format9);
//日期本地化
String format10 = localDateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
System.out.println("format10:" + format10);
//日期本地化
String format11 = localDateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT));
System.out.println("format11:" + format11);
}
結果:
BASIC_ISO_DATE:20210121
ISO_LOCAL_DATE:2021-01-21
ISO_DATE:2021-01-21
dateTimeFormatter1:2021/01/21
dateTimeFormatter2:2021/01/21 10:25:24
dateTimeFormatter3:2021-01-21 10:25:24
dateTimeFormatter4:2021.01.21 10:25:24
localDate1:1997-12-11
localDateTime1:1997-12-11T23:12:09
format8:1997-12-11 23:12:09
format9:2021年1月21日 上午10時25分24秒
format10:2021-1-21 10:25:24
format11:21-1-21 上午10:25
5.日期時間的簡單計算
@Test
public void testLocalDateCalc() {
//帶時區的日期與時間
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("帶時區的日期與時間:" + zonedDateTime);
//系統化預設時區
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("系統化預設時區:" + zoneId);
//當前時間
LocalDateTime now = LocalDateTime.now();
LocalDateTime localDateTime = LocalDateTime.of(2021, 1, 21, 13, 45, 53);
System.out.println("now:" + now);
//兩個日期時間的間隔
//注意這裡會向下取整 例如:間隔是1.8小時,那麼間隔的day結果是1
//其他同理
Duration duration = Duration.between(now, localDateTime);
System.out.println("兩個日期的間隔秒:" + duration.getSeconds());
System.out.println("兩個日期的間隔天數:" + duration.toDays());
System.out.println("兩個日期的間隔小時:" + duration.toHours());
//減去1年
LocalDateTime localDateTime1 = now.minusYears(1);
System.out.println("減去1年:" + localDateTime1.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//減去1月
LocalDateTime localDateTime2 = now.minusMonths(1);
System.out.println("減去1月:" + localDateTime2.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//減去1天
LocalDateTime localDateTime3 = now.minusDays(1);
LocalDateTime localDateTime4 = now.minus(Duration.ofDays(1));
System.out.println("減去1天:" + localDateTime3.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
System.out.println("減去1天localDateTime4:" + localDateTime4.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
結果:
帶時區的日期與時間:2021-01-21T10:27:30.157+08:00[Asia/Shanghai]
系統化預設時區:Asia/Shanghai
now:2021-01-21T10:27:30.157
兩個日期的間隔秒:11902
兩個日期的間隔天數:0
兩個日期的間隔小時:3
減去1年:2020-01-21 10:27:30
減去1月:2020-12-21 10:27:30
減去1天:2021-01-20 10:27:30
減去1天localDateTime4:2021-01-20 10:27:30
6.LocalDate轉化為Date
@Test
public void localDateToDate() {
LocalDate now = LocalDate.now();
ZonedDateTime zonedDateTime = now.atStartOfDay(ZoneId.systemDefault());
Date date = Date.from(zonedDateTime.toInstant());
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
}
結果:
2021-01-21
7.Date轉化為LocalDate
@Test
public void dateToLocalDate() {
Date date = new Date();
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
System.out.println(localDate.format(DateTimeFormatter.ISO_LOCAL_DATE));
}
結果:
2021-01-21
8.LocalDateTime轉化為Date
@Test
public void localDateTimeToDate() {
LocalDateTime now = LocalDateTime.now();
ZonedDateTime zdt = now.atZone(ZoneId.systemDefault());
Date date = Date.from(zdt.toInstant());
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
}
結果:
2021-01-21 10:32:22
9.Date轉化為LocalDateTime
@Test
public void dateToLocalDateTime() {
Date date = new Date();
LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
System.out.println(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
結果:
2021-01-21 10:34:17
10.LocalDateTime與資料庫中datetime型別的使用
這裡我使用springboot2.4.2+mybatis+mysql
資料庫結構和資料如下:
CREATE TABLE `r_test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`create_time` datetime DEFAULT NULL COMMENT '建立時間',
`update_time` datetime DEFAULT NULL COMMENT '修改時間',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='測試時間';
bean如下:
package com.example.domain;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import java.time.LocalDateTime;
import java.util.Date;
/**
* 測試時間表 r_test
*
* @author
* @date 2021-01-20
*/
public class Test {
private static final long serialVersionUID = 1L;
/**
*
*/
private Integer id;
/**
* 建立時間
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
/**
* 建立時間
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updateTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public LocalDateTime getCreateTime() {
return createTime;
}
public void setCreateTime(LocalDateTime createTime) {
this.createTime = createTime;
}
public LocalDateTime getUpdateTime() {
return updateTime;
}
public void setUpdateTime(LocalDateTime updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
.append("id", getId())
.append("createTime", getCreateTime())
.append("updateTime", getUpdateTime())
.toString();
}
}
使用controller訪問:
package com.example.controller;
import com.example.domain.Test;
import com.example.service.ITestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author
* @Description TODO
* @Date 2021/1/20 18:37
* @Version V1.0
*/
@RestController
@RequestMapping()
public class TestController {
@Autowired
private ITestService testService;
@RequestMapping("test")
public Test test() {
Test test = testService.selectTestById(1);
return test;
}
}
訪問結果:
11.示例程式碼
資源下載地址:
https://download.csdn.net/download/lvxinchun/14900851