Java 如何将 SimpleDateFormat 更改为 jodatime?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21136858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:51:08  来源:igfitidea点击:

How to change the SimpleDateFormat to jodatime?

javadatetimejodatime

提问by Sabarish K

I need to change the SimpleDateFormatto some other format which is equivalent in jodatime. Here's the code which needs to be changed.

我需要将 更改SimpleDateFormat为在 jodatime 中等效的其他格式。这是需要更改的代码。

public static String dateFormat(Date date, String format)
{
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    return sdf.format(date);
}

I have tried to use DateTimeFormatter.

我曾尝试使用 DateTimeFormatter。

public static String dateFormat(DateTime date, String format)
{
    DateTimeFormatter dtf = DateTimeFormat.forPattern(format);
    DateTime tempDateTime =  dtf.parseDateTime(date.toString());
    return tempDateTime.toString();
}

But I am getting Error.

但我收到错误。

采纳答案by Meno Hochschild

Well, I look for you in the documentation of Joda Time and SimpleDateFormat. As you can see there, the pattern definitions are unfortunately not the same. If you translate from SimpleDateFormatto Joda-DateTimeFormat, then you have to note following items:

好吧,我在 Joda Time 和SimpleDateFormat. 正如您在那里看到的,不幸的是,模式定义并不相同。如果您将 from 转换SimpleDateFormat为 Joda- DateTimeFormat,那么您必须注意以下项目:

Change Y to x (so called week-year).

将 Y 更改为 x(所谓的周年)。

Change y to Y (year-of-era) and maybe changing the chronology, too (from ISO to Gregorian/Julian)

将 y 更改为 Y(年代),也可能更改年表(从 ISO 到 Gregorian/Julian)

W is not supported in Joda Time (week of month), hence no replacement!

Joda 时间(一个月中的一周)不支持 W,因此没有替代品!

F is not supported in Joda Time (day of week in month), hence no replacement!

Joda Time(月中的星期几)不支持 F,因此没有替代品!

Change u to e (day number of week - ISO order, not localized), available since Java 7.

将 u 更改为 e(星期几 - ISO 顺序,未本地化),自 Java 7 起可用。

The symbol S is handled differently (I suppose in Joda Time better because of correct zero padding).

符号 S 的处理方式不同(我想在 Joda Time 中更好,因为正确的零填充)。

The zone symbol z is in Joda Time not allowed for parsing (maybe this is the current cause of your problems - you have not shown your pattern format or your exception yet).

区域符号 z 在 Joda Time 中不允许解析(这可能是您的问题的当前原因 -您还没有显示您的模式格式或您的异常)。

The zone/offset symbol Z is handled better in Joda Time, for example allows colons in offset etc. If you need latter you can use X in SimpleDateFormat which has the replacement Z in Joda Time.

区域/偏移量符号 Z 在 Joda Time 中处理得更好,例如允许偏移量中的冒号等。如果您需要后者,您可以在 SimpleDateFormat 中使用 X,它在 Joda Time 中替换了 Z。

Some tests added:

添加了一些测试:

Following sample code demonstrates the different handling of format symbol S.

以下示例代码演示了对格式符号 S 的不同处理。

String s = "2014-01-15T14:23:50.026";
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSS");

DateTime instant = dtf.parseDateTime(s);
System.out.println(dtf.print(instant)); // 2014-01-15T14:23:50.0260

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSS");
Date date = sdf.parse(s);
System.out.println(sdf.format(date)); // 2014-01-15T14:23:50.0026 (bad!)

Another test for format symbol z (is that your problem???):

格式符号 z 的另一个测试(是你的问题吗???):

String s = "2014-01-15T14:23:50.026PST";
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz");

DateTime instant = dtf.parseDateTime(s);
System.out.println(dtf.print(instant)); // abort

Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "2014-01-15T14:23:50.026PST" is malformed at "PST"
    at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:866)
    at time.JodaTest8.main(JodaTest8.java:83)

SimpleDateFormat can do this zone name parsing (although at least dangerous sometimes):

SimpleDateFormat 可以做这个区域名称解析(尽管有时至少是危险的):

String s = "2014-01-15T14:23:50.026PST";

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz");
Date date = sdf.parse(s);
System.out.println(sdf.format(date)); // 2014-01-15T14:23:50.026PST