-4

I have a String variable that contains a date. I just want to switch the format.

Here's my Code:

String date = jobItems.get(position).get(job_deadline);
SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy",Locale.getDefault());
String newDate = null;
newDate = df.format(Date.parse(date));

date has = 2015-08-17

and I want to change it to = 17.08.2015

My example doesn't work. Also the method Date.parse seems to be deprecated.

How can I change the format on a simple way?

Kind Regards!

EDIT:

Thats not a duplicate of that Thread because I'm not using .NET or VB

lainio
  • 53
  • 1
  • 6

1 Answers1

0

You first need to parse the date into a Date object with the given format, then you can change the SimpleDateFormat and format it back into the desired String. Like so:

private String reFormatDate(String dateIn) throws ParseException {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
    Date date = simpleDateFormat.parse(dateIn);
    simpleDateFormat = new SimpleDateFormat("dd-mm-yyyy");
    return simpleDateFormat.format(date);
}
vguzzi
  • 2,420
  • 2
  • 15
  • 19