user10065900
user10065900

Reputation:

How to calculate remaining hours to setted time

I want to create an Alarm like app in Android(Java), I want to caluclate how to many hours and minutes left. When I set my alarm to 1:00 am. My current time is 20:00 pm, it will show me "Alarm will sound after 5 hours", but it shows me after 19 hours, how to solve it.

Toast.makeText(this, "Hours: " + (calendar.getTime().getHours() - timePicker.getCurrentHour()) + "Minutes: " + (calendar.getTime().getMinutes() - timePicker.getCurrentMinute()) , Toast.LENGTH_SHORT).show();

Upvotes: 1

Views: 1352

Answers (3)

Ankur
Ankur

Reputation: 922

try something like this:

    Calendar cal = Calendar.getInstance();

    int hrs = cal.get(Calendar.HOUR_OF_DAY);

    Calendar alCal = Calendar.getInstance();
    alCal.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());

    alCal.set(Calendar.MINUTE, timePicker.getCurrentMinute());

    if(timePicker.getCurrentHour() < hrs){
        alCal.add(Calendar.DAY_OF_MONTH, 1);
    }

    long diff = alCal.getTimeInMillis() - cal.getTimeInMillis();

    long leftHrs = (diff / (60 * 60 * 1000));
    long leftMin = (diff / (60 * 1000)) - (leftHrs * 60);

    // Output
    Toast.makeText(this,"Hours: "+leftHrs+"Minutes: "+leftMin,Toast.LENGTH_SHORT).show();

Upvotes: 2

Basil Bourque
Basil Bourque

Reputation: 338896

java.time

Modern solution uses the java.time classes.

Get current moment on your default time zone. If important, confirm with the user.

ZoneId z = ZoneId.systemDefault() ; 
ZonedDateTime zdt = ZonedDateTime.now( z ) ;

Extract the time-of-day.

LocalTime lt = zdt.toLocalTime() ;

If after the alarm time, add a day.

LocalTime alarm = LocalTime.of( 1 , 0 ) ;

LocalDate ld = zdt.toLocalDate() ;
if( ! alarm.isBefore( lt ) ) {
    ld = ld.plusDays( 1 ) ;
}

ZonedDateTime fire = ZonedDateTime.of( ld , alarm , z ) 

If your other code needs UTC, extract Instant

Instant instant = fire.toInstant() ;

Calculate elapsed time.

Duration d = Duration.between( zdt , fire ) ; 
int hours = d.toHoursPart() ;
int minutes = d.toMinutesPart() ;

You should add a bit more code, a fudge factor, in case the current moment is extremely close to the alarm time, close enough that it might roll over the alarm time as this code executes.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Upvotes: 0

the_dani
the_dani

Reputation: 2524

You should use Java Date.

Calendar cal = Calendar.getInstance().set(...); //set the date
Date alarmDate = cal.time;

long duration = alarmDate - System.currentTimeMillis();
int hours = duration / (1000 * 60 * 60);

Upvotes: 2

Related Questions