无法在java中将时间戳格式化为YYYY-MM-DD HH:MM:ss
我有一个时间戳2018年12月13日19:37:18,我需要将其转换为2018-12-13 19:37:18,我按照以下步骤操作,但给出的时间戳不正确无法在java中将时间戳格式化为YYYY-MM-DD HH:MM:ss,java,timestamp,simpledateformat,Java,Timestamp,Simpledateformat,我有一个时间戳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 = input
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);
有关更多详细信息,请查看javadocD
是“一年中的一天”;不是你真正需要的“月中日”,所以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太好了,谢谢。。。它按预期工作…太好了,谢谢。。。它按预期工作。我建议您避免使用
SimpleDateFormat
类。它不仅早已过时,而且还出了名的麻烦。今天,我们在以下方面做得更好:LocalDateTime.parse(“2018年12月13日19:37:18”,DateTimeFormatter.ofPattern(“d MMM u H:mm:ss”,Locale.ENGLISH)).format(DateTimeFormatter.ofPattern(“uuuu-mm dd HH:mm:ss”)
产生2018-12-13 19:37:18
(请按照您的问题分成可管理的部分)。我建议您避免使用SimpleDateFormat
类。它不仅早已过时,而且还出了名的麻烦。今天,我们在以下方面做得更好:LocalDateTime.parse(“2018年12月13日19:37:18”,DateTimeFormatter.ofPattern(“d MMM u H:mm:ss”,Locale.ENGLISH)).format(DateTimeFormatter.ofPattern(“uuuu-mm dd HH:mm:ss”)
产生2018-12-13 19:37:18
(请按照您的问题将其分解为可管理的部分)。