Java和SpringBoot的String类型转换成Data日期类型

1.Java的String类型转换成Data日期类型

使用import java.text.SimpleDateFormat;

 SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd ");
 String s = "2020-09-09 ";
 Date date = formatter.parse(s);

这里出现了pares报错,Unhandled exception: java.text.ParseException
需要继承 ParseException
在这里插入图片描述

2.SpringbootString类型转换成Data日期类型

2.1注解@JsonFormat(主要是后台到前台的时间格式的转换)

从数据库获取时间传到前端进行展示的时候,我们有时候可能无法得到一个满意的时间格式的时间日期,在数据库中显示的是正确的时间格式,获取出来却变成了很丑的时间戳,@JsonFormat注解很好的解决了这个问题,我们通过使用@JsonFormat可以很好的解决

//设置时区为上海时区,时间格式自己据需求定。
@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")

pattern:是你需要转换的时间日期的格式

timezone:是时间设置为东八区,避免时间在转换中有误差

提示:@JsonFormat注解可以在属性的上方,同样可以在属性对应的get方法上,两种方式没有区别

2.2注解@DateTimeFormat(主要是前后到后台的时间格式的转换)

我们在使用WEB服务的时,可能会需要用到,传入时间给后台,比如注册新用户需要填入出生日期等,这个时候前台传递给后台的时间格式同样是不一致的,而我们的与之对应的便有了另一个注解,@DataTimeFormat

@DateTimeFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
在使用 Spring Data MongoDB 操作 MongoDB 数据库时,可以使用 @Field 注解来指定实体类中的属性与 MongoDB 中的字段之间的映射关系。当实体类中的属性类为 LocalDateTime 时,可以使用 @Field 注解的 converter 属性来指定一个自定义的换器,将 LocalDateTime Date 类。 具体代码如下: ```java import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Field; import org.springframework.data.mongodb.core.convert.converter.Converter; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.util.Date; @Document(collection = "myCollection") public class MyEntity { @Id private String id; @Field(value = "myDate") private LocalDateTime myDate; public String getId() { return id; } public void setId(String id) { this.id = id; } public LocalDateTime getMyDate() { return myDate; } public void setMyDate(LocalDateTime myDate) { this.myDate = myDate; } public static class LocalDateTimeToDateConverter implements Converter<LocalDateTime, Date> { @Override public Date convert(LocalDateTime localDateTime) { return Date.from(localDateTime.toInstant(ZoneOffset.UTC)); } } } ``` 在这个例子中,我们使用 @Field 注解将实体类中的 myDate 属性映射到 MongoDB 中的 myDate 字段,并使用 converter 属性指定 LocalDateTimeToDateConverter 类换器,将 LocalDateTime Date 类。在换器中,我们使用 LocalDateTime 的 toInstant() 方法将其 Instant 类,然后再使用 Date.from() 方法将其 Date 类,最后存储到 MongoDB 中。 注意:需要在 Spring Boot 应用程序启动类中注册该换器,例如: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.data.mongodb.core.convert.MongoCustomConversions; import java.util.Arrays; @SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } @Bean public MongoCustomConversions mongoCustomConversions() { return new MongoCustomConversions(Arrays.asList(new MyEntity.LocalDateTimeToDateConverter())); } } ``` 在启动类中,我们创建一个 MongoCustomConversions 类的 Bean,并将 LocalDateTimeToDateConverter 类换器注册进去。这样 Spring Data MongoDB 就会自动地调用该换器,将 LocalDateTime的数据 Date 类,并存储到 MongoDB 中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值