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

问题描述 投票:1回答:2

我正在尝试以字符串及其输入格式的字符串作为日期,并将日期转换为输出格式。但是,在转换为Date之后,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

java parsing datetime date-format
2个回答
1
投票

outputFormat.setTimeZone(TimeZone.getTimeZone(“ GMT”));

已解决。


0
投票

如果您不想使用时区,则在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
*/
© www.soinside.com 2019 - 2024. All rights reserved.