-3

I have two strings that I want to convert into a particular date time format so I can do a comparison. Problem I have is that it errors out in the parse with an exception and I wonder if I am doing something wrong. Wanted to ask what is the best way to convert two different string dates into a single date format

SimpleDateFormat localDateFormat = new SimpleDateFormat("yyyy dd mm - HH:mm:ss");
        String firstDateString= "11 May 2018 21:03:51 GMT";
        String secondDateString= "dataStore.get("2018-05-11T21:03:51Z";

        Date firstDateFormat =localDateFormat.parse(firstDateString);
        Date secondDateFormat =localDateFormat.parse(secondDateString);
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
BruceyBandit
  • 3,978
  • 19
  • 72
  • 144
  • What does the `dataStore.get` operation do? Are you sure it actually gets a string date? Your input seems to be a string date. Also, please avoid using `SimpleDateFormat` and `Date`, and use the appropriate classes from the `java.time` class. – RealSkeptic Sep 15 '19 at 09:19
  • 1
    You seem to explain your question in comments to the given answers, wouldn't it be better if you edited your question instead to make it clearer for everyone what you are asking? – Joakim Danielson Sep 15 '19 at 09:29
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `OffsetDateTime` or `Instant` and `DateTimeFormatter`, all from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Sep 15 '19 at 10:49

3 Answers3

0

Problem I have is that it errors out in the parse with an exception and I wonder if I am doing something wrong.

=> Yes you are doing it actually. You first need to parse the date into it's actual format and then format it into the desired format.

For example: for parsing and formatting 2018-05-11T21:03:51Z

DateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:MM:SS'z'", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("yyyy dd mm - HH:mm:ss");
Date date = originalFormat.parse("2018-05-11T21:03:51Z");
String formattedDate = targetFormat.format(date);  // 2018 05 11 - 21:03:51
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • Hi Paresh, ok so for this question I hardcoded the strings. In reality I am grabbing these two dates from different services and they both output the date differently (my hard coded strings display how they format). I will look at your code but hope that makes more sense? – BruceyBandit Sep 15 '19 at 09:25
0

Here:

SimpleDateFormat localDateFormat = new SimpleDateFormat("yyyy dd mm - HH:mm:ss");

That format says: 4 year digits SPACE 2 day digits SPACE 2 month digits DASH and so on.

Thing is: neither your string dates:

    "11 May 2018 21:03:51 GMT"
    "2018-05-11T21:03:51Z"

Look like that. The first one is rather "dd M yyy ..." (doesnt start with year), and the second one uses "-" not " " as separator for the initial date.

Answer: you have to use a pattern that really matches the expected date strings, see here for the specs. And note for example that you will need to use M to match "May", the lowercase m is about digits, not words!

And note: the second example is an ISO date, and the DateTimeFormatter already has pre-defined formatters for those! (so be careful about re-inventing the wheel)

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Well I hard coded the strings for the question. In reality I actually pull these dates. So my thinking is how do I assert against these dates if both different date formats? That's why my thinking was convert them to one date format and then assert – BruceyBandit Sep 15 '19 at 09:26
0

java.time

    DateTimeFormatter firstFormatteer
            = DateTimeFormatter.ofPattern("d MMM uuuu H:mm:ss z", Locale.ENGLISH);

    String firstDateString = "11 May 2018 21:03:51 GMT";
    String secondDateString = "2018-05-11T21:03:51Z";

    Instant firstInstant = firstFormatteer.parse(firstDateString, Instant::from);
    Instant seoncdInstant = Instant.parse(secondDateString);

    System.out.println("The strings are parsed into " + firstInstant + " and " + seoncdInstant);

Output is:

The strings are parsed into 2018-05-11T21:03:51Z and 2018-05-11T21:03:51Z

Your strings from two services are in two different formats, and the best you can do is to handle them in two different ways. For the first, define a formatter that matches the format. The second is in ISO 8601 format. Instant parses this format without any explicit formatter, so here we don’t need to define one.

To compare do for example:

    if (firstInstant.isBefore(seoncdInstant)) {
        System.out.println("The first date and time comes first");
    } else if (firstInstant.equals(seoncdInstant)) {
        System.out.println("The date and time is the same");
    }

The date and time is the same

The Instant class is the modern replacement for the Date class, it represents a moment in time.

The Date class was poorly designed and SimpleDateFormat notoriously troublesome, fortunately they are both long outdated. I recommend you avoid them and use java.time, the modern Java date and time API, instead.

Link: Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161