JAVA中转换为特定日期格式时出现的问题

JAVA中转换为特定日期格式时出现的问题,java,string,date,date-conversion,Java,String,Date,Date Conversion,我收到以下字符串形式的日期:“Wed Feb 06 2019 16:07:03 PM”,我需要将其转换为“02/06/2019美国东部时间下午4:17” 请告知您的问题有一个可能的解决方案:首先,将字符串解析为日期对象。然后使用所需的新格式格式化日期对象。这将为您提供:2019年6月2日04:07下午。ET应该附加在末尾,它不能通过格式化接收(尽管您可以接收GMT、PST等时区-请参阅simpleDataFormat的链接)。您可以使用SimpleDateFormat查找有关日期格式的更多信息

我收到以下字符串形式的日期:“Wed Feb 06 2019 16:07:03 PM”,我需要将其转换为“02/06/2019美国东部时间下午4:17”


请告知

您的问题有一个可能的解决方案:首先,将字符串解析为日期对象。然后使用所需的新格式格式化日期对象。这将为您提供:
2019年6月2日04:07下午
ET
应该附加在末尾,它不能通过格式化接收(尽管您可以接收GMT、PST等时区-请参阅
simpleDataFormat
的链接)。您可以使用
SimpleDateFormat
查找有关日期格式的更多信息

我看到您希望在输出中使用“at”字,但不确定这对您有多重要。但如果是,一种可能的解决方案是简单地获取新字符串,按空格分割并根据需要输出:

String newDate = newFormat.format(date);
String[] split = newDate.split(" ");
System.out.println(split[0] + " at " + split[1] + " " + split[2] + " ET"); // 02/06/2019 at 04:07 PM ET

在此处添加Ole V.V.格式的注释作为替代:

    DateTimeFormatter receivedFormatter = DateTimeFormatter
            .ofPattern("EEE MMM dd uuuu H:mm:ss a", Locale.ENGLISH);
    DateTimeFormatter desiredFormatter = DateTimeFormatter
            .ofPattern("MM/dd/uuuu 'at' hh:mm a v", Locale.ENGLISH);

    ZonedDateTime dateTimeEastern = LocalDateTime
            .parse("Wed Feb 06 2019 16:07:03 PM", receivedFormatter)
            .atZone(ZoneId.of("America/New_York"));
    System.out.println(dateTimeEastern.format(desiredFormatter));
2019年6月2日美国东部时间下午4:07


此代码使用的是现代java.time API

16:07:03
如何变成
04:17
?为什么24小时格式需要
am/pm
?在发布前彻底搜索堆栈溢出。您是否总是得到
16:07:03 pm
,即24小时时钟和am/pm标记上的小时数?那么,后者是多余的,但当然可以接受。请不要教年轻人使用过时且臭名昭著的
SimpleDateFormat
类。至少不是第一个选择。而且不是毫无保留的。今天,我们在及其
DateTimeFormatter
@OleV.V中有了更好的功能。谢谢你提出这些问题,我已经在我的答案中添加了你的评论,如果你愿意,请随意编辑。
    DateTimeFormatter receivedFormatter = DateTimeFormatter
            .ofPattern("EEE MMM dd uuuu H:mm:ss a", Locale.ENGLISH);
    DateTimeFormatter desiredFormatter = DateTimeFormatter
            .ofPattern("MM/dd/uuuu 'at' hh:mm a v", Locale.ENGLISH);

    ZonedDateTime dateTimeEastern = LocalDateTime
            .parse("Wed Feb 06 2019 16:07:03 PM", receivedFormatter)
            .atZone(ZoneId.of("America/New_York"));
    System.out.println(dateTimeEastern.format(desiredFormatter));