12:xx在SimpleDateFormat.format(“ hh:mm:ss”)中显示为00:xx

sthor69

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

----根据要求重构代码当dtold.parse(...)中的字符串为in时,示例中的输出小时为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);
        }


  }
}
奥莱VV

首先是几个像您这样的格式化程序,仅使用DateTimeFormatterfrom java.time,现代的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)在00到23之间的一天中的小时中dtfOld使用大写字母HH,在hh01到12的AM或PM中使用小时的小写字母(在这种情况下,相同的格式模式字母适用于SimpleDateFormatDateTimeFormatter;尽管有所不同)。现在,其余的内容很无聊,只比您的代码简单:

    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 / PM标记时,默认使用AM。而12:33:33 AM表示比午夜多半小时,并在24小时制上显示为00:33:33。

从13:00到23:59的时间?它们在AM中不存在。显然,SimpleDateFormat它不在乎,只是从01到11的时间进行推断,因此恰好能给您所需的时间。有一个技巧可以告诉它不要;但我不想打扰,我宁愿完全不使用该类。

链接

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

DateFormat.format以HH:mm:ss格式返回01:00:00

在Java中以HH:mm或HH:mm:ss表示的逗号分隔时间的正则表达式,例如12:00,13:03:21

如何控制SimpleDateFormat解析为19xx或20xx?

Java SimpleDateFormat(“ yyyy-MM-dd'T'HH:mm:ss'Z'”)给出时区为IST

如何从包含HH:mm:ss的SimpleDateFormat中将Calendar对象设置为当前日期但时间

将“ XX:XX:00”更改为“ 00:XX:XX”

SimpleDateFormat("dd/MM/yy kk:mm") 将时间显示为 24:30 而不是 00:30

Java SimpleDateFormat 无法解析“yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSX”格式模式中的日期

在Azure.Storage.Blobs v12.xx中ContentHash为空

Java-SimpleDateFormat将12:19:00解析为00:19:00

How to transfer seconds to Time format (YYYY:MM:DD HH:mm:ss) using moment.js

用于管理 SimpleDateFormat 的 CodenameOne 实现的 X、XX、XXX(ISO 时区)格式案例的 Java 代码

使用String.Format将秒转换为HH:MM:SS

像java.text.SimpleDateFormat一样无法识别android.text.format.DateFormat“ HH”

SSRS-hh:mm:ss tt显示为“ hh:mm:ss tt”,而不是实际时间

如何在JavaScript中将Nov 18 2015 12:00:00 GMT + 0530(斯里兰卡标准时间)转换为(yyyy-MM-dd HH-mm-ss)格式

熊猫加时间(dd-mm-yyyy XX:XX AM / PM)和整数(以分钟为单位)

在 Angular 中动态创建从 00:00:00 到 23:30:00(时间格式为 HH::mm:ss 的半小时差)的时间

-XX:+ StressLCM,-XX:为JVM + StressGCM选项

如何检查数字是否为 xx.xx.xx 格式

如何在Azure dll的Azure.Storage.Blobs v12.xx中处理乐观并发

为什么使用SimpleDateFormat(“ hh:mm aa”)解析“ 23:00 PM”会返回上午11点?

如何更改在 VBA 中显示为 yyyy/mm/dd hh:mm:ss 的单元格的日期格式

导入没有日期的时间序列时(仅 hh:mm:ss)R 决定将所有时间戳设置为 1899-12-31

将格式为1899年12月30日星期六HH:MM:SS的JavaScript时间戳转换为今天?

为什么结果不是 00:00:XX?

java.text.ParseException:无法解析的日期“ yyyy-MM-dd'T'HH:mm:ss.SSSZ”-SimpleDateFormat

java.text.ParseException:无法解析的日期“ yyyy-MM-dd'T'HH:mm:ss.SSSZ”-SimpleDateFormat

读取“ yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZZZZ”时,java.text.SimpleDateFormat的输出错误