在Java中将字符串日期转换为美国格式

3

我有以下代码,其中日期以字符串类型表示,我需要将其设置为美国格式,下面显示了如何操作:

private static final SimpleDateFormat usOutputDate =  new SimpleDateFormat("MM/dd/yyyy");

在一个方法中,我编写了下面的代码,在 dealDateString 中的值为 02-Mar-2015

// dealDateString = 02-Mar-2015 
java.util.Date  dealDate = extractDate(dealDateString); 
//here its value get converted in US format that is 03/02/2015
String dd = usOutputDate.format(dealDate); 

DateFormat format = new SimpleDateFormat("MM/dd/yyyy", Locale.US); 
// ***issue comes here as it get back converted in US format ***
java.util.Date date =  format.parse(dd);
brokerInvoiceLineItem.setDealDate(new Date(date));

如上所示,上述代码中String dd中的值为03/02/2015,但问题出现在格式变量上,它的值被转换回英国格式,而我不想要这个,请指导如何将其转换为英国格式,因为之前存储在String dd中的值已经转换为美国格式。


你的情况是什么?能否提供更清晰的信息? - Naman
所以您想转换成美国格式或英国..请告知如何将其转换为英国格式,因为它已经以美国格式转换并存储在字符串dd中,但您的问题标题说“在Java中将字符串日期转换为美国格式”? - Hemang
在格式内,该值是英国格式,即02/03/2015,但应该是美国格式,即03/02/2015,请建议如何解决。在字符串dd中,它已经是03/02/2015的格式。 - naresh saxena
@Hemang,是我道歉,但现在问题是在字符串dd中它是完美的,即03/02/2015,但当我格式化它时,即DateFormat format = new SimpleDateFormat("MM/dd/yyyy", Locale.US);..问题就出现了。 - naresh saxena
你想将其格式化回来,因为你想要它显示为“Mar-02-2015”吗? - Hemang
不,我想将其存储为Feb-03-2015,即MM/dd/yyyy格式,并且在字符串变量dd中,它的值为03/02/2015。我希望获得相同的值,但是当我使用DateFormat时,它会更改其值。 - naresh saxena
3个回答

4

由Silambarasan Poonguti提供的被接受的答案和Ramesh Ponnada提供的其他答案都是正确的。但是两者都已过时,使用了早期版本Java中捆绑的旧日期时间类。这些类已被证明存在问题,令人困惑且存在缺陷。

java.time

Java 8及以上版本内置java.time框架,取代旧的java.util.Date/.Calendar类。新类受到高度成功的Joda-Time框架启发,旨在成为其继承者,概念类似但重新架构。由JSR 310定义。由ThreeTen-Extra项目扩展。请参见Oracle教程

LocalDate

这些新类包括LocalDate,用于处理仅包含日期值而不包含时间和时区的情况。尽管LocalDate不包含时区,但请注意时区对于确定诸如“今天”之类的日期至关重要。在任何时刻,世界上的日期并不相同,因为新的一天在东部早些时候开始。
LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) ) ;

解析字符串

要解析一个字符串,需要指定一个编码模式。该模式类似于旧的java.text.SimpleTextFormat中使用的模式,但并不完全相同。因此,请务必仔细研究java.time.format.DateTimeFormatter文档。

请注意,我们使用三个M来指定我们期望得到一周中某天名称的缩写。这是解决问题的关键。

还要注意,我们指定了一个Locale,告诉java.time我们期望的缩写星期几的人类语言。

String input = "02-Mar-2015";
Locale locale = Locale.US;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "dd-MMM-yyyy" ).withLocale ( locale );
LocalDate localDate = LocalDate.parse ( input , formatter );

在控制台中输出。默认情况下,java.time 中的 toString 方法使用标准的ISO 8601格式。

System.out.println ( "localDate: " + localDate );

本地日期:2015-03-02

生成字符串

当生成日期时间值的字符串表示时,通常最好让java.time为您进行本地化,而不是假定特定格式。 java.time.format 包有这样的工作类。

请注意对 withLocale 的调用。Locale 指定了两个元素,预期格式的文化规范和用于星期几和月份名称的人类语言。如果未指定 Locale,则隐式应用JVM的当前默认Locale。最好明确指出,因为默认值随时可能更改。

DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate ( FormatStyle.SHORT ).withLocale ( Locale.US );
String output = today.format ( formatter );

将数据输出到控制台。

System.out.println ( "output: " + output );

输出结果: 12/29/15

如果您坚持要指定格式,请指定一个编码模式。

DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "MM/dd/yyyy" );
String output = today.format ( formatter );

输出: 2015年12月29日

转换

如果您手头有一个java.util.Date,请转换为java.time。Instant是UTC时间线上的一个时刻。

Instant instant = myJUDate.toInstant();

指定所需时区,创建一个日期并返回 ZonedDateTime 对象。

ZoneId zoneId = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId ) ;

然后请求生成一个LocalDate,其值从ZonedDateTime中提取。

LocalDate localDate = zdt.toLocalDate();

3

试一下这个...

  try {
        //parsing date format
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");

        String dealDateString = "02-Mar-2015";
        Date date = formatter.parse(dealDateString);
        TimeZone tz = TimeZone.getDefault();

        //converting date format for US
        SimpleDateFormat sdfAmerica = new SimpleDateFormat("MM/dd/yyyy");
        TimeZone tzInAmerica = TimeZone.getTimeZone("America/New_York");
        sdfAmerica.setTimeZone(tzInAmerica);

        String sDateInAmerica = sdfAmerica.format(date); // Convert to String first
        System.out.println("Date (String) : " + sDateInAmerica);

    } catch (Exception bug) {
        bug.printStackTrace();
    }

0

你能试一下下面的代码吗:

SimpleDateFormat ukDateFormat =  new SimpleDateFormat("dd-MMM-yyyy");
SimpleDateFormat usDateFormat = new SimpleDateFormat("MMM-dd-yyyy", Locale.US);

java.util.Date  dealDate = ukDateFormat.parse("02-Mar-2015");
String dateInUSFormat = usDateFormat.format(dealDate);
System.out.println(dd); // Here you have date String in US format.

该链接有许多关于如何在Java中处理日期转换的示例


真正的问题不在于此,因为您可以在字符串dateInUSFormat中看到正确的值为03/02/2015,但是如果您在此之后添加DateFormat format = new SimpleDateFormat("MM/dd/yyyy", Locale.US);,问题就会出现。 - naresh saxena
Naresh,你的问题不够清晰,请详细说明再次应用格式时发生了什么,以及哪一行被转换回英国格式。我相信你的代码没有显示后面的部分。 - Ramesh Ponnada
当我在字符串dd上应用格式时,它再次转换为英国格式,这就是问题所在。 - naresh saxena
上面接受的答案给出了那个特定时间的日期(UTC没有指定源TZ)。因此,按照这种方式转换,2015年2月3日00:00小时变为2015年2月2日20:00小时(取决于夏令时)。这是你想要的吗? - Ramesh Ponnada
是的,现在你明白了,请指导我如何实现相同的功能。 - naresh saxena
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接