-3

I am attempting to format a desired joda time LocalDate type to a mmddyyy format and I am running into trouble and not sure how to proceed. I've tried a couple different things so far and had no luck.

So I pretty much want to take a date like 04-15-2016 and get a LocalDate type from it in format, 04152016, but it still needs to be in the LocalDate format, not a string.

DateTimeFormatter dtf = DateTimeFormat.forPattern("MMddyyyy");
LocalDate currentDate = LocalDate.parse("04152016", dtf);
System.out.println(currentDate);

The date comes out as 2016-04-15. If anyone could help me I would greatly appreciate it. Perhaps I am missing something fundamental when it comes to the Joda Time library.

THank you.

Tastybrownies
  • 897
  • 3
  • 23
  • 49
  • 3
    I don't understand. Why don't you use your `DateTimeFormatter` to format the `currentDate`? – Savior Apr 22 '16 at 19:32
  • 5
    You need to understand that a `LocalDate` doesn't have a format - that's not part of its state. When you want to convert it to text in a particular form, you use a `DateTimeFormatter`. – Jon Skeet Apr 22 '16 at 19:33

2 Answers2

1

Your currentDate represents a moment in time. You can print that moment out in a way that the French would understand, or someone in India, for example.

Please start by taking a look at the Joda documentation. In particular, see the entry

String b = dt.toString("dd:MM:yy");

You could adapt it by printing

currentTime.toString("MMddyyyy");

Or you could reuse your formatter and adopt Jon Skeet's answer (cited in the comments).

System.out.println(dtf.print(currentDate));
Community
  • 1
  • 1
rajah9
  • 11,645
  • 5
  • 44
  • 57
0

Data types like long, double, Date and LocalDate are values and don't store your preferred format for how you want to display them.

When you want to display them a particular way, you need to use a formatter to turn them into a String, then the String has a format, but the value never does (other than it's default format built in)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130