1

I'm trying to create a Date from a String I receive from the server. The String is:

2018-05-23T06:39:37+0000

So the correct format should be:

yyyy-MM-dd'T'HH:mm:ss.SSSZ

Here is my code:

String createdDate = comment.getCreatedDateTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
try {
    Date parsedDate = simpleDateFormat.parse(createdDate);
    createdDate = parsedDate.toString();
} catch (ParseException ex) {
    ex.printStackTrace();
}
mCommentDate.setText(createdDate);

I don't know if there is any way to do this, because after that I would like to parse again to the next format:

dd/MM/yyyy hh:mm

I've tried to parse the original String using this last format directly but I'm getting the same exception.

Any suggestion?

Óscar
  • 1,143
  • 1
  • 19
  • 38
  • Create another `SimpleDateFormat` instance with new format and call 'format' method with the date you parsed already. – RaviH May 23 '18 at 08:51
  • 2
    Why do you have the `S`s in `yyyy-MM-dd'T'HH:mm:ss.SSSZ`? your string doesn't contain milis – Jan Ossowski May 23 '18 at 08:55
  • Sorry, it was not `hh/MM/yyyy`, it is `dd/MM/yyyy`. My mistake. – Óscar May 23 '18 at 09:06
  • 1
    What I understand is that you're trying to create a Date object out of a String date, right? For the SDF to consider parsing the String to an appropriate Date object you need to specify the correct pattern-format, which in your case is slightly wrong! If that gets rectified you can correctly obtain your Date object... – robot_alien May 23 '18 at 09:10
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. `OffsetDateTime.parse("2018-05-23T06:39:37+0000", DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssxx"))`. – Ole V.V. May 23 '18 at 09:23

3 Answers3

2

I see you've solved your own problem with a little help from the comments, however I would suggest you seriously consider LocalDate, as the older Date classes are quite troublesome at times.

In fact, as your incoming value has a TimeZone, you'll need to use ZonedDateTime to parse your input.

String createdDate = "2018-05-23T06:39:37+0000";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
ZonedDateTime localDate = ZonedDateTime.parse(createdDate, formatter);
System.out.println(localDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")));

Output:

23/05/2018 06:39

achAmháin
  • 4,176
  • 4
  • 17
  • 40
  • 1
    Android API 26 is needed to use `ZonedDateTime`, but I'll keep for other projects. Thanks for the suggestion. – Óscar May 23 '18 at 09:55
  • The same functionality is available for lower-level Android since the classes of `java.time` have been backported. What you need is [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP), the Android edition of the backport. More in [this question: How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. May 23 '18 at 10:18
  • The variable name `localDate` seems unfortunate here. A left-over from an early version of the code? – Ole V.V. May 23 '18 at 10:20
  • @OleV.V. yes - feel free to change the variable name - it was from similar code I had already. – achAmháin May 23 '18 at 10:22
1

The given input date String format

2018-05-23T06:39:37+0000

is incorrect so that you are getting ParseException since millisecond(SSS) part is missing from your date format yyyy-MM-dd'T'HH:mm:ss.SSSZ

So please try with

2018-05-23T06:39:37.235-0530

so below code should work

String createdDate = comment.getCreatedDateTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
    try {
        Date parsedDate = simpleDateFormat.parse(createdDate);
        createdDate = parsedDate.toString();
        System.out.println(parsedDate.toString());
    } catch (ParseException ex) {
        ex.printStackTrace();
    }
mCommentDate.setText(createdDate);
Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74
0

Ok, the first mistake (as you've pointed) is I didn't have milliseconds on the original String.

After removing "SSS" from the simpleDateFormat it works like a charm. So this is the final code:

String createdDate = comment.getCreatedDateTime();
SimpleDateFormat defaultDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.getDefault());
try {
    Date parsedDate = defaultDateFormat.parse(createdDate);
    SimpleDateFormat finalDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm", Locale.getDefault());
    createdDate = finalDateFormat.format(parsedDate);
} catch (ParseException ex) {
    ex.printStackTrace();
}
mCommentDate.setText(createdDate);
Óscar
  • 1,143
  • 1
  • 19
  • 38