首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我正在使用SimpleDateFormat将字符串更改为日期格式,并在UTC中使用serverTimezone,但日期是提前一天保存的

问我正在使用SimpleDateFormat将字符串更改为日期格式,并在UTC中使用serverTimezone,但日期是提前一天保存的
EN

Stack Overflow用户
提问于 2020-05-11 17:19:32
回答 1查看 31关注 0票数 0

我正在使用SimpleDateFormat将字符串更改为日期格式,并在UTC中更改serverTimezone,但日期是提前一天保存的:

String date="2020-05-05";

代码语言:javascript
复制
public static Date getDateByStringDateYYYYMMDD(String date) {
        try {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            return format.parse(date);
        } catch (Exception ex) {
            throw new GenricException(ex.getMessage());
        }
    }

网址: jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=utf8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC&createDatabaseIfNotExist=true

mysql中保存日期如2020-05-04

EN

回答 1

Stack Overflow用户

发布于 2020-05-11 17:49:55

使用此代码

代码语言:javascript
复制
public static Date getDateByStringDateYYYYMMDD(String date) {
        try {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            format.setTimeZone(TimeZone.getTimeZone("GMT"));
            return format.parse(date);
        } catch (Exception ex) {
            throw new GenricException(ex.getMessage());
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61726247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档