-4

I am getting two date fields from JSON as text like this May 22 12:05:41 UTC 2018 and 2018-05-22 12:05:41.512 but I have to change to MM-dd-yyyy HH:mm:ss format.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sun
  • 3,444
  • 7
  • 53
  • 83
  • 1
    What have you tried already? What is not working? – Wim Deblauwe Jun 01 '18 at 08:14
  • See here: https://stackoverflow.com/questions/4216745/java-string-to-date-conversion – Joe Taras Jun 01 '18 at 08:14
  • Hi Sun, it would appear you have enough rep to know the ins and outs of this site and that you should provide an attempt/research first. – achAmháin Jun 01 '18 at 08:17
  • 1
    https://stackoverflow.com/a/20821770/8473028 – DodgyCodeException Jun 01 '18 at 08:20
  • 2
    Please for your own sake search before posting a question. Variants of this question have been asked and answered so many times. – Ole V.V. Jun 01 '18 at 08:44
  • 1
    Your acceptance rate is pretty low, and there are some questions in your posting history where you are not responding to requests for feedback. It is not mandatory to accept answers here, but it is polite to do so. – halfer Jun 02 '18 at 12:55

2 Answers2

4

Just to be clear - date/time objects are format agnostic. They are simply containers for the amount of time which has passed since a fixed point in time (usually the Unix Epoch), so you can't change their format per se.

However, you can, generate a String of a prescribed pattern.

When dealing with date/time in Java you should make use of the date/time APIs introduced in Java 8 (or the ThreeTen back port)

For example...

String date1 = "May 22 12:05:41 UTC 2018";
DateTimeFormatter format1 = DateTimeFormatter.ofPattern("MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
LocalDateTime ldt1 = LocalDateTime.parse(date1, format1);

String date2 = "2018-05-22 12:05:41.512";
DateTimeFormatter format2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);
LocalDateTime ldt2 = LocalDateTime.parse(date2, format2);

DateTimeFormatter format3 = DateTimeFormatter.ofPattern("MM-dd-yyyy HH:mm:ss", Locale.ENGLISH);
System.out.println(ldt1.format(format3));
System.out.println(ldt2.format(format3));

Which outputs...

05-22-2018 12:05:41
05-22-2018 12:05:41

Since one of your inputs has a timezone associated with it, it would be appropriate to take it into consideration

ZonedDateTime zdt = ZonedDateTime.parse(date1, format1);
LocalDateTime ldt1 = zdt.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime();

which outputs (for my current location)

05-22-2018 22:05:41
halfer
  • 19,824
  • 17
  • 99
  • 186
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

You can use this code

DateFormat format = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
DateFormat inputFormat1 = new SimpleDateFormat("MMM dd HH:mm:ss z yyyy");
DateFormat inputFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String date1 = "May 22 12:05:41 UTC 2018";
String date2 = "2018-05-22 12:05:41.512";
System.out.println(format.format(inputFormat1.parse(date1)));
System.out.println(format.format(inputFormat2.parse(date2)));
pkgajulapalli
  • 1,066
  • 3
  • 20
  • 44
  • 3
    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/) and its `DateTimeFormatter`. – Ole V.V. Jun 01 '18 at 08:41