2

I'm trying to compare current date with an input date, received in this format "EEE MMM dd HH:mm:ss zzz yyyy"

This piece of code works when my input string is Wed Apr 01 09:00:00 GMT 2020 but doesn't work if its Wed Apr 01 09:00:00 BST 2020 .

        DateTime currentTime =DateTime.now().withZone(DateTimeZone.forID("Europe/London"));
        DateTimeFormatter fmt = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss zzz yyyy");
        DateTime inputDateTime = fmt.parseDateTime("Wed Apr 01 09:00:00 BST 2020");


        if (inputDateTime.isBefore(currentTime))
            Log.d(TAG, "if ");
        else
            Log.d(TAG, "else ");

Any idea on what am I doing wrong? Also, feel free to suggest if there's a better way to do it (can't use new Java date and time library, since we support android API 19+ )

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • Joda-Time has a set of preferred time zones, which default is small (some 10 zones including GMT), but you can set it to what you want. Set it to something that includes the time zone you want to parse, and you should be fine. – Ole V.V. Apr 10 '20 at 07:34
  • @OleV.V. thought of asking you this question, is there a place to find the corresponding iso timezone code, for example (BST-->Europe/Isle_of_Man, EST-->America/New_York). Thanks – Ryuzaki L Apr 10 '20 at 13:26
  • 1
    @Deadpool Two ways I know of: (1) Guess from https://www.timeanddate.com/time/zones/ (2) Iterate over all available time zones in Java, format one summer date and one winter date with time zone abbreviation and see which ones match. – Ole V.V. Apr 10 '20 at 15:51
  • If this is for new code, I recommend the answer by Andreas: Prefer java.time, the modern Java date and time API, over Joda-Time. If you’re already using Joda-Time and you don’t want to migrate to java.time just yet, I’m immodest enough to think that [my answer to the linked original question](https://stackoverflow.com/a/57976007/5772882) is better than the other answers below. – Ole V.V. Apr 11 '20 at 11:43

3 Answers3

2

If you use ThreeTen Android Backport instead of Joda-Time, and you should, then it parses fine:

import org.threeten.bp.ZonedDateTime;
import org.threeten.bp.format.DateTimeFormatter;
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy");
ZonedDateTime inputDateTime = ZonedDateTime.parse("Wed Apr 01 09:00:00 BST 2020", fmt);
System.out.println(inputDateTime);

Output

2020-04-01T09:00+01:00[Europe/Isle_of_Man]
Andreas
  • 154,647
  • 11
  • 152
  • 247
1

Docs has mentioned that Time zone names cannot be parsed in joda DateTimeFormat because time zone abbreviations are ambiguous and the parser can't know which time zone it is exactly

Zone names: Time zone names ('z') cannot be parsed.

For example

  • BST can be British Summer Time or Bangladesh Standard Time or Bougainville Standard Time

  • PST can be Pacific Standard Time or Pakistan Standard Time

  • CST could be Central Standard Time (USA), China Standard Time, or Cuba Standard Time
  • EST could be Eastern Standard Time (USA), or Eastern Standard Time (Australia).

And the best way to is to replace BST with appropriate iso timezone code here (for example Europe/Isle_of_Man), and then simple use "EEE MMM dd HH:mm:ss ZZZ yyyy" DateTimeFormat

DateTimeFormatter fmt = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss ZZZ yyyy");
DateTime inputDateTime = fmt.parseDateTime("Wed Apr 01 09:00:00 Europe/Isle_of_Man 2020");

System.out.println(inputDateTime);  //2020-04-01T09:00:00.000+01:00
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
0

I'm not sure if this meets your requirements for the Andriod API 19+, but here is something for the date formatting with SimpleDateFormatter

    String pattern = "EEEEE MMMMM yyyy HH:mm:ss";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
    String date = simpleDateFormat.format(new Date());
    System.out.println(date);

Hope this solves your problem!

TheXDShrimp
  • 116
  • 13
  • not really, unfortunately. I need to use the formatted to compare with current date. Converting to String defeats the purpose. – Rachel Green Apr 10 '20 at 03:21
  • Thanks for wanting to contribute. 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`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Apr 11 '20 at 11:45