gpt4 book ai didi

java - 无法将时间戳格式设置为 YYYY-MM-DD HH :mm:ss in java

转载 作者:行者123 更新时间:2023-12-02 01:45:15 27 4
gpt4 key购买 nike

我有一个时间戳 2018 年 12 月 13 日 19:37:18,我需要将其转换为 2018-12-13 19:37:18,我正在按照以下步骤操作,但它给出的时间戳不正确

     DateFormat outputFormat = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
DateFormat inputFormat = new SimpleDateFormat("DD MMM YYYY HH:mm:ss");
Date date = inputFormat.parse(updatedStartime);
String NewStartTime = outputFormat.format(date);

我得到的输出为 2018-Jan-01 19:37:18,在格式化输出之前是否需要将 MMM 转换为整数月份值?获得预期输出的正确步骤是什么?

最佳答案

D 是“一年中的某一天”;不是您实际需要的“一个月中的某一天”,所以d

Y 是“周年”;我猜你的意思是正常年份的y

所以总而言之:

    DateFormat outputFormat = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
DateFormat inputFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.US);

有关更多详细信息,请参阅 javadoc https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

关于java - 无法将时间戳格式设置为 YYYY-MM-DD HH :mm:ss in java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53768994/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com