-3

Here is my situation. The method TodayFirst I want to get everyday AM 9:00 and compare with Android System time. But I run this method it will get the System time.

public String TodayFirst()
{
    String time ="";
    SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();
    System.out.println(df.format(date));
    Calendar calendar = Calendar.getInstance();
    calendar.set(calendar.HOUR_OF_DAY,9);
    time = df.format(calendar.getTime());
}
Kliff
  • 19
  • 5
  • Its really unclear what exactly you trying to do . But to get Exact 9AM time add `calendar.set(calendar.MINUTE,0); calendar.set(calendar.SECOND,0); calendar.set(calendar.MILLISECOND,0);`. – ADM Aug 28 '18 at 04:03
  • Check this example [link](https://www.stacktips.com/tutorials/android/repeat-alarm-example-in-android) – PriyankVadariya Aug 28 '18 at 04:17
  • 1
    and what is the problem? What are you missing, or what is wrong with the code? – Vladyslav Matviienko Aug 28 '18 at 05:12
  • 1
    As an aside 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. Aug 28 '18 at 07:13
  • Search Stack Overflow before posting. You can assume any basic date-time Question has already been asked and answered. – Basil Bourque Aug 28 '18 at 07:22

3 Answers3

1

java.time

    LocalTime now = LocalTime.now(ZoneId.systemDefault());
    if (now.isAfter(LocalTime.of(9, 0))) {
        System.out.println("It’s past 9");
    }

Notice how much clearer it reads than the code in your question.

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).

What went wrong in your code?

You need a return statement in your method. Assuming you wanted to return time, the method produces a string like 2018/08/28 09:21:03. The hours have been set to 9, but the minutes and seconds are unchanged from the current time. Also you are producing a similar string for current system time, but not comparing the two.

Links

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

For that you should use Alarm Manager. from here you get how to use Alarm Manage.

Alarm manager work on system clock

This alarm manager will send one broadcast at every 9:00 Am. using that Broadcast you can do for other stuff.

Sameer Donga
  • 988
  • 9
  • 24
0
 You could do as belows:
    public String TodayFirst()
    {
        String time;
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        System.out.println(df.format(date));
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.HOUR_OF_DAY,9);
        calendar.set(calendar.MINUTE,0);
        calendar.set(calendar.SECOND,0);

        time = df.format(calendar.getTime());
        return time;  //time will be 2018/08/28 09:00:00 if you call the function today
    }
navylover
  • 12,383
  • 5
  • 28
  • 41
  • These terrible date-time classes were supplanted by the *java.time* classes years ago. See the modern approach taken in the [Answer by Ole V.V.](https://stackoverflow.com/a/52052116/642706) – Basil Bourque Aug 28 '18 at 07:19