Jodatime Date Format

Is it possible to format a JodaTime date.

Here is the code:

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

Here, date1 is returned as: 2013-07-02 , but I would like 02-JUL-13

Thank you in advance

+4
java date jodatime date-format
Jul 03 '13 at 14:34
source share
2 answers

Can I format a JodaTime date

Yes. You want a DateTimeFormatter .

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

This will give 02-Jul-13, but you can always uppercase.

For more information, see Login and Logout in the User Guide.

EDIT: Alternatively, as Rohit suggests:

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

Personally, I would prefer to create the formatter once as a constant, and reuse it wherever you need it, but it is up to you.

+13
Jul 03 '13 at 14:37
source share
— -

Check out the Joda DateTimeFormatter .

You probably want to use it through 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 version (counterintuitively) is not thread safe!

+9
Jul 03 '13 at 14:37
source share



All Articles