1

I need to convert some string date to another format string date

String current = "Tue Apr 16 10:59:11 EDT 2019";

I want to get result date String in format ISO-8601 in accordance with the pattern - "yyyy-MM-dd'T'HH:mm:ss.SSSXX"

Could you please help me to implement this?

MS90
  • 1,219
  • 1
  • 8
  • 17
liotur
  • 809
  • 2
  • 17
  • 36
  • Is EDT Australian Eastern Daylight Time, North American Eastern Daylight Time or something else? Yes, I know that summer time ended in most of Australia on April 7, but I wanted to point out that three letter time zone abbreviations are often ambiguous, so you shouldn’t rely in them. Also it looks like you’ve got the output from `Date.toString()`. Better if you could convert the `Date` object itself. – Ole V.V. Apr 16 '19 at 09:54
  • liotur, I am not claiming that this question is an exact duplicate of each of the questions I have linked to. Rather if you combine information from two or more of the linked questions, I believe that you should be able to make the conversion that you want. You can also search, of course, and you will certainly find more. – Ole V.V. Apr 16 '19 at 10:27

3 Answers3

2

Using Java8 DateTime API you can do something like following:

     //your input
     String date = "Tue Apr 16 10:59:11 EDT 2019";
     //create new formatter for parsing your input
     DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss zzz yyyy", Locale.ENGLISH);
     //create new zonedDateTime from parsing an input with provided formatter
     ZonedDateTime zonedDateTime = ZonedDateTime.parse(date,formatter);

Get it in the format of your own:

     //format your zonedDateTime with a new provided pattern
     String test = zonedDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXX"));
     //print it
     System.out.println(test);
MS90
  • 1,219
  • 1
  • 8
  • 17
1

There is a SimpleDateFormat in java, and there is a parse method in there

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

String dateString = format.format( new Date()   );
Date   date       = format.parse ( "2009-12-31" );      
Starmixcraft
  • 389
  • 1
  • 14
  • 1
    your format string in SimpleDateFormat constructor doesn't match @liotur date format – Michal Horvath Apr 16 '19 at 08:53
  • There is a parse, and a format method. OP will need two date formats, one to parse their String into a datetime, one to format that datetime to another String. – kumesana Apr 16 '19 at 08:57
  • 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. Apr 16 '19 at 09:38
1

None of Java's time classes fully implement ISO 8601 but you can try with Java 8:

ZonedDateTime zp = ZonedDateTime.parse(string);
Date date = Date.from(zp.toInstant());
SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXX");
System.out.println(dt1.format(date));
vanste
  • 11
  • 1
  • 3
    Why do you mix java.util.\* time classes with java.time.\* classes? They're not designed to help when mixed. Well, there is a reason why java.time.\* classes were invented despite the previous and unsatisfying existence of the others. – kumesana Apr 16 '19 at 08:56
  • @kumesana Of course you can use LocalDateTime instead Date if that suite needs. – vanste Apr 16 '19 at 09:10
  • 1
    It's not about *can*, it's about *should*. Use either java.util.\* time classes, or java.time.\* classes. That works well and fine. Don't mix them. It forces useless weirdness and encourages to do things that don't work as the programmer imagined it would. Though, better to not use java.util.\* time classes at all, considering how better java.time.\* is. – kumesana Apr 16 '19 at 09:15
  • Also this won’t work with the string from the question (agree that in Java 8 you should stay far away from `SimpleDateFormat` and the other old and troublesome classes). – Ole V.V. Apr 16 '19 at 09:37