Jodatime date format

久未见 提交于 2019-12-05 17:10:54

Is it possible to format JodaTime Date

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

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.

EDIT: 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.

Check out the Joda DateTimeFormatter.

You probably want to use it via something like:

 DateTime dt = new DateTime();
 DateTimeFormatter fmt = DateTimeFormat.forPattern("dd-MMM-yy");
 String str = fmt.print(dt);

This is a much better solution than the existing SimpleDateFormat class. The Joda variant is thread-safe. The old Java variant is (counterintuitively) not thread-safe!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!