Java中的时间问题(待补充)

Java中的时间问题(待补充)

数据库mysql

框架 微服务

问题一:

1.mysl中的时间类型存的为datetime,起初实体类中接收参数用的是LocalDateTime

 @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
 private LocalDateTimebeginTime;

这样返回给前端是没有问题的,
但是,当我在代码中需要用到这个时间且与当前时间进行比较时,就出现了以下问题:
1.从数据库中取出的数据为

 2020-08-26T 00:00
处理的方法:  将实体类的接收参数改为Date
  1. 引出的问题:时间格式改变为如下,且时间比数据库取出的多了8个小时
Sat Aug 29 11:12:49 CST 2020

处理方法如下:

//格式转换
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
//转换时区
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
String formatB = formatter.format(beginTime);

2.与当前时间进行比较的完整代码:

public static  Boolean dateTime( Date beginTime){
        //当前时间及转换格式
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
//        System.out.println("dateString = " + dateString);
        //数据库中的时间及转换格式+时区
        formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
        String formatB = formatter.format(beginTime);
//        System.out.println("format = " + formatB);
        int i = formatB.compareTo(dateString);
//        System.out.println("i = " + i);
        if (i>=0){
            return true;
        }else {
            return false;
        }
    }
ps: 如果有不足的地方希望大佬们多多指正,共同进步。0.0!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值