-1

How to get specific date of the month in android programmatically? Example, i want to get 23 april 2020 as a string or int and put it inside "if" statement.

Example

if("23 april 2020"){
// some code
} else if ("24 april 2020"){
// some code
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
alizulfuqar
  • 149
  • 1
  • 12

3 Answers3

2

You should convert your string date into Date format, and then compare them.

Here is an example

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd",Locale.getDefault());
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();

Date date1 = dateFormat.parse("2013-01-01");
Date date2 = dateFormat.parse("2013-01-02");

calendar1.setTime(date1);
calendar2.setTime(date2);

System.out.println("Compare Result : " + calendar2.compareTo(calendar1));
System.out.println("Compare Result : " + calendar1.compareTo(calendar2));
Hamid Reza
  • 624
  • 7
  • 23
  • Sorry, this is not what i intent. i want just get specific date and put it inside "if-statement" and show inside if statement what will occur at that specific date. But thanks for answer – alizulfuqar Feb 08 '20 at 08:09
  • 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. Feb 08 '20 at 10:12
1

java.time and ThreeTenABP

    LocalDate apr23 = LocalDate.of(2020, Month.APRIL, 23);
    LocalDate apr24 = LocalDate.of(2020, Month.APRIL, 24);

    LocalDate today = LocalDate.now(ZoneId.of("Australia/Tasmania"));
    if (today.equals(apr23)) {
        System.out.println("It’s April 23");
    } else if (today.equals(apr24)) {
        System.out.println("It’s April 24");
    } else {
        System.out.println("It’s none of those dates");
    }

When I ran this snippet today (February 8 in Tasmania), the output was:

It’s none of those dates

Please insert your desired time zone where I put Australia/Tasmania. To use the time zone of the device use ZoneId.systemDefault(), only beware that the default may be changed by some other part of your program or any other program running in the same JVM, in which case you won’t get the device setting.

An object of the LocalDate class from java.time represents a date. So it’s neither a string nor an int, but it’s what you need. java.time is the modern Java date and time API, it’s warmly recommended. In addition to equals() a LocalDate also has methods isAfter and isBefore. If it happens that you user doesn’t launch your app exactly on APril 23, you may use one of these for determining that the date has been passed (once the user does run the app).

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both 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) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern 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. So:

    import org.threeten.bp.LocalDate;
    import org.threeten.bp.Month;
    import org.threeten.bp.ZoneId;
    

    Since you are using Gradle, this line in your build.gradle should do it (taken from Maven Repository):

       compile group: 'com.jakewharton.threetenabp', name: 'threetenabp', version: '1.2.2'
    

    If I remember correctly, this syntax works too (save any typos):

       compile 'com.jakewharton.threetenabp:threetenabp:1.2.2'
    

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    I use Android Studio 2.3.3, i added java 8 compatibility to my gradle, but unfortunately it needs Api 26 minimum target, my minimum is 19, anyway thanks for answer, if my minimum was 26 it would work – alizulfuqar Feb 08 '20 at 11:18
  • Thanks for using your time. i did as you wrote, i added dependency in my gradle, but i dont know what im doing not right? i screenshoted all those https://ibb.co/Kxq6Dy2 https://ibb.co/0qqwFpV https://ibb.co/VQMCStv – alizulfuqar Feb 08 '20 at 11:59
  • I believe it’s in what I wrote: *make sure you import the date and time classes from `org.threeten.bp` with subpackages.* I have edited and spelled it out a bit more. Hope you get it to work. – Ole V.V. Feb 08 '20 at 12:04
  • 1
    Yes, i needed to import dates manually (it was me not understood you firstly, now understood), very very thanks for great tutorial! You are the man! – alizulfuqar Feb 08 '20 at 12:12
0

You can get current date by calling System.currentTimeMillis() and use SimpleDateFormat to parse it to String and do your comparison.

Here's an example:

final SimpleDateFormat sdf  = new SimpleDateFormat("dd MMMM yyyy", Locale.getDefault());
final String now = sdf.format(System.currentTimeMillis());

You can also parse date as String as mentioned by @Hamid Reza. Rest depends upon your use case.

Just make sure to select proper matching pattern for parsing date, in your case dd MMMM yyyy

Harsh Jatinder
  • 833
  • 9
  • 15