-7

I need your help to show remaining minutes instead of hours

15 minutes instead of 15:30

Sample: time left to start reservation: 15 minutes

 private Notification getNotification(Date countdownEnds) {
    DateFormat timeFormat = countdownTimeFormatFactory.getTimeFormat();
    String countdownEndsString = timeFormat.format(countdownEnds);
    String title = resources.getString(R.string.countdown_notification_title);
    String text = resources.getString(R.string.countdown_notification_text, countdownEndsString);

    PendingIntent tapIntent =
            PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentTitle(title)
            .setContentText(text)
            .setTicker(title)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(tapIntent)
            .setOngoing(true)
            .setAutoCancel(false);

    return builder.build();
}




   public DateFormat getTimeFormat() {
        return android.text.format.DateFormat.getTimeFormat(context);
    }

kod:code

Kunu
  • 5,078
  • 6
  • 33
  • 61
  • 1
    _15 minutes instead of 15:30_. That doesn't make sense as far as I understand from your question 15 is the hour, not the minute – Kunu May 02 '18 at 11:21
  • 1
    Is it so that 15:30 is the time when the countdown ends (or 3:30 PM) and when the time is 15:15, so there are 15 minutes remaining, you want to display “15 minutes”? You can get a lot of inspiration from [Java Calculate time until event from current time](https://stackoverflow.com/questions/36042954/java-calculate-time-until-event-from-current-time). Please search for more interesting and helpful questions and answers. – Ole V.V. May 02 '18 at 12:45

1 Answers1

0

Barebones solution:

    long remainingMillis = countdownEnds.getTime() - System.currentTimeMillis();
    long remainingMinutes = TimeUnit.MILLISECONDS.toMinutes(remainingMillis);
    String countdownEndsString = String.format("%d minutes", remainingMinutes);

For a nicer solution use java.time, the modern Java date and time API, for the calculation of the minutes:

    long remainingMinutes = ChronoUnit.MINUTES.between(
            Instant.now(), DateTimeUtils.toInstant(countdownEnds));

In this case also see if you can get rid of the use of Date completely since that class is long outdated, and all the functionality and more is in java.time. In the last snippet I am using the ThreeTen Backport (see explanation and links below) and its DateTimeUtils class. For anyone reading along and using Java 8 or later and still not having got rid of the Date class, the conversion is built into that class, so it is slightly simpler yet:

    long remainingMinutes 
            = ChronoUnit.MINUTES.between(Instant.now(), countdownEnds.toInstant());

You may also want to look into the Duration class of java.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