1

I have 2 times SimpleDateFormat :

  1. 2020-11-23T21:17:03.039023Z
  2. 2020-11-23T21:17:03Z

How to check if the time use "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" or "yyyy-MM-dd'T'HH:mm:ss'Z'" ?

example :

if ("2020-11-23T21:17:03.039023Z".contentEquals("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")) {
    Log.e("TAG", "true");
} else {
    Log.e("TAG", "false");
}
  • 1
    As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [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. Nov 23 '20 at 16:16
  • 2
    Why do you want that? Both conform to [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601). If you please explain the need to distinguish between them, we can possibly help you better. – Ole V.V. Nov 23 '20 at 16:19
  • Do the following questions (perhaps in combination) answer yours? (1) [Illegal pattern character 'T' when parsing a date string to java.util.Date](https://stackoverflow.com/questions/2597083/illegal-pattern-character-t-when-parsing-a-date-string-to-java-util-date) (2) [ISO 8601 String to Date/Time object in Android](https://stackoverflow.com/questions/3941357/iso-8601-string-to-date-time-object-in-android) … – Ole V.V. Nov 23 '20 at 16:25
  • (3) [Java LocalDateTime.parse with millisecond precision but optional microsecond precision](https://stackoverflow.com/questions/54682028/java-localdatetime-parse-with-millisecond-precision-but-optional-microsecond-pre) (4) [how do you account for two possible date formats one with milliseconds and one without when parsing date using simpledateformat? \[duplicate\]](https://stackoverflow.com/questions/63857192/how-do-you-account-for-two-possible-date-formats-one-with-milliseconds-and-one-w) – Ole V.V. Nov 23 '20 at 16:27
  • Thank you so much for explaining to me @OleV.V. – Muhammad Rafi Bahrur Rizki Nov 23 '20 at 16:28
  • 1
    [`Instant.parse( "2020-11-23T21:17:03.039023Z" )`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/Instant.html#parse(java.lang.CharSequence)) handles either case. Never use the legacy date-time classes such as `SimpleDateFormat`, `Date`, and `Calendar`. Use only the *java.time* classes. – Basil Bourque Nov 23 '20 at 18:35

2 Answers2

2

You probably want String#matches here with an appropriate regex pattern:

String dt = "2020-11-23T21:17:03.039023Z";
String regex = "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{6}Z";
if (dt.matches(regex)) {
Log.e("TAG", "true");
}
else {
    Log.e("TAG", "false");
}

But note that the above does not actually do any validation on the input, but rather just detects the general pattern, and distinguishes from the version with no millisecond/microsecond precision.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

You can parse the date-time string to Instant and check if the fraction of second is greater than 0.

import java.time.DateTimeException;
import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(isTagEnabled("2020-11-23T21:17:03.039023Z"));
        System.out.println(isTagEnabled("2020-11-23T21:17:03Z"));
    }

    static boolean isTagEnabled(String strDateTime) {
        boolean enbaled = false;
        try {
            if (Instant.parse(strDateTime).getNano() > 0) {
                enbaled = true;
            }
        } catch (DateTimeException e) {
            e.printStackTrace();
        }
        return enbaled;
    }
}

Output:

true
false

Based on this, you can write your code as

if (isTagEnabled("2020-11-23T21:17:03.039023Z")) {
    Log.e("TAG", "true");
} else {
    Log.e("TAG", "false");
}

Note that the date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone. I suggest you should stop using them completely and switch to the modern date-time API.

Learn more about the modern date-time API at Trail: Date Time. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Elegant solution with good use of java.time. It doesn’t tell us whether the string was for example `2020-11-23T21:17:03.000000Z`, said without knowing whether this was ever a requirement. – Ole V.V. Nov 24 '20 at 15:40