【问题标题】:12:xx shown as 00:xx in SimpleDateFormat.format("hh:mm:ss")12:xx 在 SimpleDateFormat.format("hh:mm:ss") 中显示为 00:xx
【发布时间】:2018-04-07 14:33:54
【问题描述】:

在以下代码中使用 SimpleDateFormatter.format 时,12:00 到 12:59 之间的时间在 startDateText TextView 中显示为 00:00 到 00:59,而从 13:00 开始正确显示为 13:xx , 14:xx 到 23:59。

---- 按要求重构代码 当 dtold.parse(...) 中的字符串是示例时,输出时间是 00:00,当它是“13:00”时,它是正确的“13:00”

import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;


// one class needs to have a main() method
public class HelloWorld
{
  // arguments are passed using the text field below this editor
  public static void main(String[] args)
  {
        SimpleDateFormat dtnew = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        SimpleDateFormat dtold = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

            try {

            Calendar cal = Calendar.getInstance();
            cal.setTime(dtold.parse("2017-03-12 12:33:33"));
            cal.add(Calendar.SECOND, 10);
            System.out.println(dtnew.format(cal.getTime()));

        } catch (Exception e) {

            throw new RuntimeException(e);
        }


  }
}

【问题讨论】:

  • 您能否将您的代码缩减为MCVE?例如不要只使用ZonedDateTime.now()(可以是上午或下午),而是建立一个特定的时间来产生特定的(可能是不正确的)输出并显示您期望的输出。另外,请改用System.out.println(),这样人们就可以在没有TextView的情况下重现您的问题
  • @sthor69 我认为当您执行 dtold.parse(date) 时信息会丢失。事实上,您的代码在 13:00 - 23:59 的时间内工作只是未记录的功能(或错误)
  • @Ole V.V.不,我没有使用安卓。这是一个纯java应用程序
  • @Bohemian 根据要求重构代码
  • @Ole V.V.非常感谢你。我不记得为什么我使用不同的格式(HH 和 hh),但这是@Bohemian 的问题,你是对的。由于我急于尝试解决问题,我使用 StackOverflow 作为调试器系统。我以后会注意不这样做的

标签: java simpledateformat


【解决方案1】:

首先有几个像你这样的格式化程序,只使用来自java.timeDateTimeFormatter,现代Java日期和时间API:

private static DateTimeFormatter dtfOld = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private static DateTimeFormatter dtfNew = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");

需要注意两点:(1) 按逻辑顺序声明格式化程序,即您将要使用它们的顺序。在问题中使用相反的顺序让我感到困惑,我不确定它是否也让你自己感到困惑。 (2) 在dtfOld 中,使用大写HH 表示从00 到23 的时间。小写hh 表示从01 到12 的AM 或PM 中的小时(在这种情况下,相同的格式模式字母适用于@ 987654328@ 和 DateTimeFormatter;虽然有区别)。现在剩下的就很无聊了,只比你的代码简单:

    LocalDateTime parsed = LocalDateTime.parse("2017-03-12 12:33:33", dtfOld);
    System.out.println(parsed);
    LocalDateTime dateTime = parsed.plusSeconds(10);
    System.out.println(dateTime);
    System.out.println(dateTime.format(dtfNew));

输出是:

2017-03-12T12:33:33
2017-03-12T12:33:43
12-03-2017 12:33:43

我推荐java.time。您使用的旧日期和时间类 — SimpleDateFormatCalendarDate — 早已过时。现代类不仅在这种情况下允许更简单的代码,而且很常见。我发现 java.time 通常使用起来更好。

你的代码出了什么问题?

我已经给出了提示:小写 hh 是上午或下午从 01 到 12 的小时。当您不提供和解析 AM/PM 标记时,AM 用作默认值。而 12:33:33 AM 表示午夜过后半小时多一点,在 24 小时制上呈现为 00:33:33。

从 13:00 到 23:59 的时间?它们在 AM 中不存在。显然SimpleDateFormat 不在乎,只是从 01 到 11 的时间推断,因此恰好给了你预期的时间。有一个技巧可以告诉它不要这样做;但我不想打扰,我宁愿根本不使用这个类。

链接

Oracle tutorial: Date Time 解释如何使用java.time

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多