0

I am trying to convert a string "8/2/2018 04:25 AM" into a date object but it seems to be converting it wrong making it output a completely wrong date and time when I do a date.toString(). Note - I do not care about the date.toString() format, I just need it to be the same date and time

Here is my code,

String timestampString = dateText.getText().toString() + " " + timeText.getText().toString();
try {
     Date timestamp2 = new SimpleDateFormat("dd/MM/yyyy hh:mm a").parse(timestampString);
     Log.d("TIME", timestamp2.toString());

} catch (ParseException e) {Log.d("TAG", e)}

Here is the output:

D/TIME: Wed Feb 07 23:25:00 EST 2018

if anyone could lead me in the right direction, it is greatly appreciated.

SQLUser
  • 97
  • 11
  • 1
    Timezone issue. Set SimpleDateFormat or the Date or both to the correct timezone to fix. – Sheepy Apr 23 '18 at 08:05
  • How do I go about doing that ? – SQLUser Apr 23 '18 at 08:05
  • 1
    Search before posting. This has been addressed many many many times already. And never use `Date` or `SimpleDateFormat` classes. Use only *java.time* classes instead. – Basil Bourque Apr 23 '18 at 08:09
  • Are you sure you are looking at the right log entry? I am wondering whether you copied the log entry and added it here without changing it? Because colon is present at the end and d/ is present at the beginning. – notionquest Apr 23 '18 at 08:09
  • @notionquest It looks like the right date-time, only 5 hours off, which would typically be a time zone issue. – Ole V.V. Apr 23 '18 at 08:11
  • As an aside and assuming you’re programming for Android, 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. – Ole V.V. Apr 23 '18 at 08:11
  • I cannot reproduce on my Java 9. When I set my time zone to America/New_York, I get `D/TIME: Thu Feb 08 04:25:00 EST 2018`. It would appear that your formatter uses UTC, but I would have expected both it and `Date.toString()` to use the same time zone, namely the time zone setting of your JVM. – Ole V.V. Apr 23 '18 at 08:20
  • 1
    Possible duplicate of [simpledateformat changing timezone](https://stackoverflow.com/questions/45112775/simpledateformat-changing-timezone) – Sheepy Apr 23 '18 at 08:35

1 Answers1

1
    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("d/M/uuuu");
    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);
    LocalDate date = LocalDate.parse(dateText.getText(), dateFormatter);
    LocalTime time = LocalTime.parse(timeText.getText(), timeFormatter);
    LocalDateTime dateTime = date.atTime(time);
    Log.d("TIME", dateTime.toString());

Prints:

D/TIME: 2018-02-08T04:25

I am using and recommending java.time, the modern Java date and time API.The Date class that you were using is long outdated, SimpleDateFormat is too and also notoriously troublesome. Avoid those if you can. java.time is so much nicer to work with. And offers the LocalXx classes that don’t have time zones, which guarantees to guard you against time zone issues. That said, you may want to convert your date-time into a ZonedDateTime in a time zone of your choice to make it an unambiguous point in time.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

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