Java DateFormat 转换自动将小时增加 1

Java DateFormat conversion automatically increases hour by 1

我正在尝试获取字符串中的日期及其输入格式字符串,并将日期转换为输出格式。但是,在转换为日期后,java 代码会将小时数增加一小时。我无法理解导致错误的原因。

我的代码:

try {
    DateFormat outputFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");

    DateFormat inputFormat = new SimpleDateFormat(format);

    Date date = inputFormat.parse(parameterValue);
    parameterValue = outputFormat.format(date);
    return parameterValue;
} catch (ParseException ex) {
    // take action
}

格式字符串:ddMMMyyyy / hh:mm z

输入日期:07DEC2015 / 10:02 GMT

输出日期:07/12/2015 11:02:00

outputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

已解决。

如果你不想使用时区,在java 8你可以使用LocalDate/LocalTime/LocalDateTime:

LocalDateTime localDateTimeInstance = LocalDateTime.parse(dateToBeConverted, DateTimeFormatter.ofPattern(formatOfDateToBeConverted));
return localDateTimeInstance.format("dd/MM/yyyy hh:mm:ss");


/*
  Also check out ZoneDate, ZoneTime (for timezone)
  Checkout - LocalDate, LocalTime
*/