0

How to properly format a "01/01/2014" format date into "dd MM yyyy", please ?

I've tried to surround this via this instruction:

String dateStr = new SimpleDateFormat("dd MM yyyy").parse("01/01/2014").toString();

But, an error message triggers on this line saying :

FATAL EXCEPTION: main java.lang.NullPointerException at com.mypharmacy.app.DrugAdd$3.onClick

How to deal with this?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
user3797031
  • 51
  • 1
  • 7
  • 1
    Use a debugger to see where the exception is being thrown – Reimeus Sep 14 '14 at 16:29
  • 1
    Additionally, show us the `getString` method. There isn't enough context here... although it looks like you're calling `toString()` on `Date`, which isn't going to work. If you're trying to convert from one string to another, you need to parse with one `SimpleDateFormat` and then format with a different one. You can't do both with a single `SimpleDateFormat`, because you've got two formats... – Jon Skeet Sep 14 '14 at 16:30
  • Show code, show log. Your code is throwing compilation error. – VenomVendor Sep 14 '14 at 16:31
  • 1
    See http://stackoverflow.com/questions/7882025 for example. – Jon Skeet Sep 14 '14 at 16:31
  • possible duplicate of [Convert string to date then format the date](http://stackoverflow.com/questions/7882025/convert-string-to-date-then-format-the-date) – Basil Bourque Sep 14 '14 at 19:09

1 Answers1

1

You've got to do the parsing in one step:

Date date = new SimpleDateFormat("MM/dd/yyyy").parse("01/01/2014");

And then the formatting in a different step:

String formattedDate = new SimpleDateFormat("dd MM yyyy").format(date);
Denise
  • 1,947
  • 2
  • 17
  • 29