-1

I would like to set two different inputs from date and timestamp into a date in UTC at the end. I am not sure if using replaceAll function would work.

Inputs:

  • string date = "06/04/2019" which the format is "MM/dd/yyyy"
  • string timestamp = "15/03/2018 15:46:59.000386 PM -05:00" which the format is "dd/MM/yyyy hh:mm:ss.SSSSSS a XXX"

Output: "06/04/2019 15:46:59.000386 PM -05:00" then transform it into UTC...

user2537246
  • 143
  • 2
  • 10
  • 1
    You are right, `replaceAll` would not work. Instead, parse the string as a date and do whichever manipulations are needed with the date object. – Henry Mar 01 '18 at 05:47
  • So in the timestamp 15 is the month number?!? – Ole V.V. Mar 01 '18 at 06:09
  • 1
    Search for [`LocalDate`](https://docs.oracle.com/javase/9/docs/api/java/time/LocalDate.html), [`OffsetDateTime`](https://docs.oracle.com/javase/9/docs/api/java/time/OffsetDateTime.html) and [`DateTimeFormatter`](https://docs.oracle.com/javase/9/docs/api/java/time/format/DateTimeFormatter.html). There are many examples to find. – Ole V.V. Mar 01 '18 at 06:11
  • 2
    And the time in the timestamp is 15 PM?!? Also, what is the expected result of converting your date to UTC?? – Ole V.V. Mar 01 '18 at 07:04
  • Possible duplicate of [How to convert any Date time to UTC using ZonedDateTime or Java 8](https://stackoverflow.com/questions/34115801/how-to-convert-any-date-time-to-utc-using-zoneddatetime-or-java-8). The question has been asked and answered more times, please search. – Ole V.V. Mar 01 '18 at 08:44
  • @Ole V.V my question was just if using replaceAll was the right thing to do for manipulating those two inputs before parsing them and once that is set, then parse it and convert "06/04/2019 15:46:59.000386 PM -05:00" to UTC. 15 is the dd from that timestamp but I care more of the actual date is in that string date. – user2537246 Mar 01 '18 at 10:39

2 Answers2

1

Just to complement the other answer above, you can also parse the LocalDate first, and then join it with OffsetTime to create the OffsetDateTime:

OffsetDateTime timestampOnDate = LocalDate
    // parse date (dd/MM/uuuu)
    .parse(dateString, dateFormatter)
    // join with time and offset (ignores 15/03/2018) 
    .atTime(OffsetTime.parse(timestampString, timestampFormatter));

OffsetTime.parse will create an object that has the time and offset part (ignoring the date you don't want, in this case, "15/03/2018"), and joining it with a LocalDate produces the desired OffsetDateTime.

Not sure which code is better, though. I guess both are equivalent.


replaceAll might work, but only if the dateString is a valid date - which is checked when parsing. If the date string contains an invalid date, such as 35/99/0000, or a valid date in another format, or even some nonsense text, replaceAll won't complain and the result will be an incorrect string.

But parsing those invalid values as a date will throw an exception, so it's better to handle your data as the types they really represent.

fads3
  • 11
  • 1
  • Elegant! java.time is full of possibilities. I think i’d still want to parse the timestamp into `OffsetDateTime` not to throw information away form the outset. But then you may use its `toOffsetTime` method and proceed as in your code. It might feel more natural that way? – Ole V.V. Mar 02 '18 at 06:13
  • Yeah I played with both of your feedback and it worked perfect! and at the end the timestamp input I got from a third party, they modified it so I can receive a normal timestamp as MM/dd/yyyy ..... Thank you all! – user2537246 Mar 02 '18 at 21:34
0

Executive answer: No, String.replaceAll will not work for converting your datetime nor your date to UTC.

Java (and Java 8 and later in particular) has very good support for parsing, handling, converting and formatting dates and times. For example:

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu");
    DateTimeFormatter timestampFormatter = DateTimeFormatter
            .ofPattern("dd/MM/yyyy hh:mm:ss.SSSSSS a XXX", Locale.ENGLISH);

    String dateString = "06/04/2019";
    String timestampString = "15/03/2018 03:46:59.000386 PM -05:00";
    OffsetDateTime timestampOnDate = OffsetDateTime
            .parse(timestampString, timestampFormatter)
            .with(LocalDate.parse(dateString, dateFormatter));
    System.out.println("Output:        " + timestampOnDate.format(timestampFormatter));
    System.out.println("Output in UTC: "
            + timestampOnDate.withOffsetSameInstant(ZoneOffset.UTC)
                    .format(timestampFormatter));

This prints:

Output:        06/04/2019 03:46:59.000386 PM -05:00
Output in UTC: 06/04/2019 08:46:59.000386 PM Z

Your strings and formats didn’t quite make sense, so I have changed them a bit: MM/dd to dd/MM, 15 PM to 03 PM and therefore also HH (for hour of day) to hh (for hour within AM or PM). If these changes were not the correct ones, just make the ones you want instead.

Edit: Of course, if you insist, you may do

    timestampString = timestampString.replaceFirst("^\\d{2}/\\d{2}/\\d{4}", dateString);

— to obtain 06/04/2019 03:46:59.000386 PM -05:00 and then parse as before. I consider this code less readable, and it also won’t give you the validation of the date that was in the timestamp string at first. I recommend using the date-time classes for manipulating your date and timestamp as I do in the code example.

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