1

I have JsonObject in this dob key contain date format but i don't want this format how I convert it...

String dobDate=String.valueOf(gridData.get("DOB"));

it returning string is: 2006-12-31T18:30:00.000Z

But I want to convert this date to "dd-MM-yyyy" how I can d this.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
123Gaurav
  • 19
  • 1
  • SimpleDateFormat f= new SimpleDateFormat("dd-MM-yyyy"); String date = f.format(dobDate); System.out.println(date); – Madhan Varadhodiyil Jul 26 '18 at 12:18
  • 2
    its better to use `Instant instant = Instant.parse("2006-12-31T18:30:00.000Z");` from [**java.time**](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) – Youcef LAIDANI Jul 26 '18 at 12:22
  • In which time zone do you want the date? If you’re in India, for example, you string is equal to 2007-01-01T00:00 local time. It is never the same date everywhere on earth. – Ole V.V. Jul 26 '18 at 12:27

2 Answers2

2

I am pretty sure this question is a duplicate!
But here is the answer:
This uses the old way with the Date object

SimpleDateFormat format  = new SimpleStringFormat("yyyy-MM-ddTHH:mm:ss.zzZ");
Date d = format.parse("Your date String return thing");

Then you have a Date Object,
(I am not completely sure if the pattern is right) If you need it as String:

To get a String out of it use:
String newDateFormat = format.format(d);

The way above is working. If you interested in doing it in a "modern" way,
take a look at this classes:


I think time handling is a complex topic and you should investigate a bit if you work with it!

Niton
  • 189
  • 4
  • 16
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 26 '18 at 12:25
  • Thanks for the edit. `Calendar` and `GregorianCalendar` are hardly modern, even though they did try to solve some of the design problems in `Date`. In any case, java.time has fully replaced them. `Instant` and `LocalDate` that you mention are the recommended classes to use. – Ole V.V. Jul 26 '18 at 19:26
1

If you have java.util.Date you can use this

SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
String formatted = df.format(gridData.get("DOB"));
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 26 '18 at 12:25
  • 1
    when i using this it not working as accepted Date dobDate=sdf.parse(String.valueOf(gridData.get("DOB"))); String dobMap=sdf.format(dobDate); – 123Gaurav Jul 26 '18 at 13:08
  • you used the wrong pattern – Niton Jul 26 '18 at 23:42
  • its more like this yyyy-MM-ddTHH:mm:ss.zzZ I dont know if it is valid but it seems to be a more suitable solution – Niton Jul 26 '18 at 23:42