繁体   English   中英

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

[英]converting a string date into US format in java

我有以下代码,其中日期以字符串类型出现,我必须将其设置为美国格式,因此下面显示了它

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但问题出在format变量,其值被转换为UK格式,我不希望这样做,请告知我如何将其转换为UK格式,它已经被转换为以前存储在String dd中的美国格式。

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来指定期望的日期名称的缩写。 这是解决课题中问题的关键。

还要注意,我们指定了一个语言环境,该语言环境将java.time缩写为day的人类语言告诉了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

产生一个字符串

当生成日期时间值的String表示形式时,通常最好让java.time为您本地化它,而不是采用特定的格式。 java.time.format包具有用于此类工作的类。

注意对withLocale的调用。 语言环境指定两个元素,即预期格式的文化规范和用于日期和月份名称的人类语言。 如果未指定语言环境,则将隐式应用JVM当前的默认语言环境。 最好明确一点,因为默认值可以随时更改。

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

转储到控制台。

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

输出:15/12/29

如果您坚持使用特定格式,请指定编码模式。

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

输出:2015/12/29

转变

如果手头有java.util.Date,请转换为java.time。 InstantUTC时间轴上的时刻。

Instant instant = myJUDate.toInstant();

指定要用来形成日期的时区,从而产生ZonedDateTime

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

然后要求生成LocalDate,并从ZonedDateTime中提取其值。

LocalDate localDate = zdt.toLocalDate();

尝试这个...

  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();
    }

您可以尝试以下代码:

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中进行日期转换的示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM