Jodatime日期格式 [英] Jodatime date format

查看:171
本文介绍了Jodatime日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以格式化JodaTime Date。

Is it possible to format JodaTime Date.

这是代码:

   private static LocalDate priorDay(LocalDate date1) {
      do {
         date1 = date1.plusDays(-1);
      } while (date1.getDayOfWeek() == DateTimeConstants.SUNDAY ||
             date1.getDayOfWeek() == DateTimeConstants.SATURDAY); 
      //System.out.print(date1);
      return date1;
   }

此处date1返回为: 2013-07-02 但我希望 02-JUL-13

Here date1 returns as: 2013-07-02 but i would like as 02-JUL-13

提前感谢

推荐答案


是否可以格式化JodaTime日期

Is it possible to format JodaTime Date

是的。您希望 DateTimeFormatter

Yes. You want DateTimeFormatter.

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MMM-yy")
    .withLocale(Locale.US); // Make sure we use English month names
String text = formatter.format(date1);

这将给予02-Jul-13,但您可以随时大写。

That will give 02-Jul-13, but you can always upper-case it.

请参阅输入和输出部分用户指南更多信息。

See the Input and Output part of the user guide for more information.

编辑:或者,根据Rohit的建议:

Alternatively, as suggested by Rohit:

String text = date1.toString("dd-MMM-yy", Locale.US);

我个人喜欢一次创建格式化程序,作为一个常量,并在需要的地方重复使用它,但它取决于你。

Personally I'd prefer to create the formatter once, as a constant, and reuse it everywhere you need it, but it's up to you.

这篇关于Jodatime日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆