0

Let's say I have a date like Fri Jul 16 12:04:35 EAT 2021

I want to change it to 16-JUL-2021. How do I go about it?

R. Mseti
  • 57
  • 8

3 Answers3

4
ZonedDateTime zdt = ZonedDateTime.parse("Fri Jul 16 12:04:35 EAT 2021", DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss z yyyy
"));
String newDate = zdt.format(DateTimeFormatter.ofPattern("dd-MMM-yyyy")).toUpperCase();
g00se
  • 3,207
  • 2
  • 5
  • 9
1

You can use SimpleDateFormat:

import java.text.SimpleDateFormat;
//...
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd-LLL-yyyy");
String text = sdf.format(date);
yuri777
  • 377
  • 3
  • 7
  • 1
    Yea ... but `Date` and `SimpleDateFormat` are legacy classes. For Java 8 and later, you should be using the `java.time` classes. – Stephen C Jul 16 '21 at 10:07
-1
    String startDateInput = "Fri Jul 16 12:04:35 EAT 2021";
    String date = startDateInput.substring(8, 10) + "-" +  
                  startDateInput.substring(4, 7) + "-"
                  + startDateInput.substring(startDateInput.length() - 4);
tushar
  • 69
  • 5