【问题标题】:Result of parsing date is wrong by 16 hours 2 minutes解析日期的结果错误了 16 小时 2 分钟
【发布时间】:2017-03-19 09:27:59
【问题描述】:

我有一个字符串07/Dec/2016:07:38:59 1000。 我想将其解析为日期。 结果我该怎么做才能成为约会07/Dec/2016 07:38:59 1000? 现在它打印出Wed Dec 07 23:40:59 CET 2016

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeParse {
    public static void main(String[] args) throws ParseException {
        String time ="07/Dec/2016:07:38:59 1000";
        SimpleDateFormat format = new SimpleDateFormat("dd/MMM/yyyy:hh:mm:ss");

        String dateString = format.format( new Date()   );
        Date   date       = format.parse ( "07/Dec/2016:07:38:59 1000" );

        System.out.println(date);

    }
}

【问题讨论】:

  • 你的字符串末尾多了一个 `1000`。那是为了什么?当我强烈怀疑您需要 24 小时解析 (HH) 时,您还在使用 12 小时解析 (hh)。
  • tet 是什么意思?
  • 额外的 1000 在字符串中。
  • 1000 是什么意思?毫秒时区,究竟是什么?
  • 在我的电脑上你的程序打印Wed Dec 07 07:38:59 CET 2016,这似乎是你所期望的结果。我不知道为什么您的计算机上的结果是 16 小时 2 分钟错误。对不起。

标签: java date parsing


【解决方案1】:

我假设 1000 应该是毫秒?如果是这样,那是不正确的。就像写 13:60:60

这也意味着您输入的时间字符串格式缺少毫秒值。 应该是"dd/MMM/yyyy:hh:mm:ss SSS" 毫秒值不应超过 3 位

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多