java 接口开发时间_基于Java8开发接口时,处理Java8中的日期

前言:在java8之前我们都是使用Date时间类,但是java8中提供了更好用的日期时间API,但是我们习惯了日期的格式为: yyyy-MM-dd HH:mm:ss,那在java8我们如何处理这样的日期格式呢?

处理前端传过来的日期时间(转换为LocalDateTime/LocalDate/LocalTime)

上代码:

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.ControllerAdvice;

import org.springframework.web.bind.annotation.InitBinder;

import java.beans.PropertyEditorSupport;

import java.time.LocalDate;

import java.time.LocalDateTime;

import java.time.LocalTime;

import java.time.format.DateTimeFormatter;

import static org.slf4j.LoggerFactory.*;

/**

* @Classname GlobalExceptionHandler

* @Description 处理Java8的日期与时间

*/

@ControllerAdvice

public class GlobalExceptionHandler {

private final static Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

@InitBinder

protected void initBinder(WebDataBinder binder) {

binder.registerCustomEditor(LocalDate.class, new PropertyEditorSupport() {

@Override

public void setAsText(String text) throws IllegalArgumentException {

setValue(LocalDate.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd")));

}

});

binder.registerCustomEditor(LocalDateTime.class, new PropertyEditorSupport() {

@Override

public void setAsText(String text) throws IllegalArgumentException {

log.info("=====initBinder=====的参数为:" + text);

setValue(LocalDateTime.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

}

});

binder.registerCustomEditor(LocalTime.class, new PropertyEditorSupport() {

@Override

public void setAsText(String text) throws IllegalArgumentException {

setValue(LocalTime.parse(text, DateTimeFormatter.ofPattern("HH:mm:ss")));

}

});

}

}

将API中的时间以固定的格式返回

@Configuration

public class JacksonConfiguration {

@Bean(name = "objectMapper")

public ObjectMapper getObjectMapper() {

ObjectMapper om = new ObjectMapper();

om.registerModule(javaTimeModule()).registerModule(problemModule())

.registerModule(constraintViolationProblemModule());

return om;

}

@Bean

public JavaTimeModule javaTimeModule() {

JavaTimeModule javaTimeModule = new JavaTimeModule();

javaTimeModule.addSerializer(LocalDateTime.class,

new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

javaTimeModule.addSerializer(LocalDate.class,

new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));

javaTimeModule.addDeserializer(LocalDateTime.class,

new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

javaTimeModule.addDeserializer(LocalDate.class,

new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

javaTimeModule.addDeserializer(LocalTime.class,

new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));

return javaTimeModule;

}

/* Jackson Afterburner module to speed up serialization/deserialization */

@Bean

public AfterburnerModule afterburnerModule() {

return new AfterburnerModule();

}

/* Module for serialization/deserialization of RFC7807 Problem. */

@Bean

ProblemModule problemModule() {

return new ProblemModule();

}

/*

* Module for serialization/deserialization of ConstraintViolationProblem.

*/

@Bean

ConstraintViolationProblemModule constraintViolationProblemModule() {

return new ConstraintViolationProblemModule();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 8日期时间相关的类被重新设计,以提供更好的性能和更简单的API。以下是处理Java 8日期的一些方法: 1. LocalDate类:它表示一个ISO日期,例如2019-09-23。它不包含时间区信息。 ```java LocalDate today = LocalDate.now(); LocalDate yesterday = today.minusDays(1); LocalDate tomorrow = today.plusDays(1); ``` 2. LocalTime类:它表示一个ISO时间,例如10:15:30。它不包含日期区信息。 ```java LocalTime now = LocalTime.now(); LocalTime later = now.plusHours(1); LocalTime earlier = now.minusMinutes(30); ``` 3. LocalDateTime类:它表示一个ISO日期时间,例如2019-09-23T10:15:30。它不包含区信息。 ```java LocalDateTime dateTime = LocalDateTime.now(); LocalDateTime tomorrowMorning = dateTime.plusDays(1).withHour(9).withMinute(0).withSecond(0); ``` 4. ZonedDateTime类:它表示一个日期时间区,例如2019-09-23T10:15:30+08:00[Asia/Shanghai]。 ```java ZoneId zoneId = ZoneId.of("Asia/Shanghai"); ZonedDateTime zonedDateTime = ZonedDateTime.of(LocalDateTime.now(), zoneId); ``` 5. DateTimeFormatter类:它用于格式化和解析日期时间字符串。 ```java LocalDateTime dateTime = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = dateTime.format(formatter); LocalDateTime parsedDateTime = LocalDateTime.parse(formattedDateTime, formatter); ``` 这些类和方法可以帮助我们轻松地处理Java 8日期时间
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值