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

时间:2015-12-29 03:48:58

标签: java date date-formatting

我有以下代码,其中日期是字符串类型,我必须以美国格式设置它 所以下面我已经展示了它

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内的美国格式。

3 个答案:

答案 0 :(得分:3)

Silambarasan Poonguti的accepted Answer和Ramesh Ponnada的other Answer都是正确的。但两者都已过时,使用与最早版本的Java捆绑在一起的旧日期时间类。这些课程被证明是麻烦,混乱和有缺陷的。

java.time

Java 8及更高版本中内置的java.time框架取代了旧的java.util.Date/.Calendar类。新课程的灵感来自非常成功的Joda-Time框架,旨在作为其继承者,在概念上类似但重新设计。由JSR 310定义。由ThreeTen-Extra项目扩展。请参阅Oracle Tutorial

LocalDate

这些新类包括LocalDate,用于处理没有时间或时区的仅限日期的值。虽然LocalDate不包含时区,但请注意,时区对于确定日期(例如'今天')至关重要。在任何一个时刻,世界各地的日期都不一样,因为东方早些时候开始新的一天。

LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) ) ;

解析字符串

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

请注意,我们使用三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 );
  

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。 InstantUTC中时间轴上的一个时刻。

Instant instant = myJUDate.toInstant();

指定您想要形成日期的时区,产生ZonedDateTime

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

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

LocalDate localDate = zdt.toLocalDate();

答案 1 :(得分:2)

试试这个......

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

答案 2 :(得分: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.

Link有很多关于如何在Java中使用日期转换的例子