0
votes

I am using SimpleDateFormat to change string to date format and serverTimezone in UTC but date is saved 1 day earlier:

String date="2020-05-05";

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());
        }
    }

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

date is save in mysql is like 2020-05-04

1

1 Answers

1
votes

Use this code

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());
        }
    }