Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

229
Vistas
Android Java : Difference between current time and specific time in future is calculated wrong

I need the difference between current time and specific time in future so that i can pass countdown timer as a parameter. I have function for this operation but it calculates wrong. Here is my function that calculates difference

   public Date RetriveLeftTime(Date incomingTime) {

    DateFormat milisFormat = new SimpleDateFormat("HH:mm:ss");
    Date moment = Calendar.getInstance().getTime();
    milisFormat.format(moment);
    Date configuredTime = ConfigureTime(incomingTime);
    milisFormat.format(configuredTime);
    long leftTime =configuredTime.getTime()-moment.getTime();

    Date result = new Date(leftTime);
    result = ConfigureTime(result);

    Log.d("Result",result.toString());

    return result;

}

The ConfigureTime() function for configuring date details

public Date ConfigureTime(Date date){

    Calendar today = Calendar.getInstance();
    int date = today.get(Calendar.DATE);
    int year = today.get(Calendar.YEAR);
    int day = today.get(Calendar.DAY_OF_WEEK);
    int month = today.get(Calendar.MONTH);
    int zone = today.get(Calendar.ZONE_OFFSET);

    Calendar time = Calendar.getInstance();
    time.setTime(date);

    time.set(Calendar.YEAR, year);
    time.set(Calendar.MONTH, month);
    time.set(Calendar.DAY_OF_WEEK, day);
    time.set(Calendar.DATE, date);
    time.set(Calendar.ZONE_OFFSET,zone);

    Log.d("ConfigureTime()",time.getTime().toString());
    Log.d("Current Time",today.getTime().toString());

    return time.getTime();
}

I have looked similar post about this issue.I checked and configured all my time specification like timezone,date,year but still get wrong result.I don't get why it is happening.Maybe my code is doing it wrong way i don't know. What do i do wrong? Most people recommend to use joda time but i took it little too far to switch joda time.Is there a solution before switching to joda time?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

tl;dr

Duration
.between(
    Instant.now() ,
    myJavaUtilDate.toInstant()
)
.toMillis()

java.time

You are using terrible classes that were years ago supplanted by the modern java.time classes defined in JSR 310. Never use Calendar, Date, SimpleDateFormat.

Instant

If you were handed a java.util.Date object, immediately convert to a Instant object. Use new conversion methods added to the old classes.

Instant then = myJavaUtilDate.toInstant() ;

An Instant represents a moment as seen “in UTC”, with an offset of zero hours-minutes-seconds.

Capture the current moment as seen in UTC.

Instant now = Instant.now() ;

Calculate time to elapse.

Duration d = Duration.between( now , then ) ;

Verify that your target moment is indeed in the future.

if( d.isNegative() ) { … deal with faulty input … }

Generally best to pass around a Duration object rather than a mere integer counting milliseconds or nanoseconds. But if need be, you can extract a count from the Duration object.

long milliseconds = d.toMillis() ;

Notice that by working in UTC, we need not deal with any time zone issues.

Android

If using Android 26+, this functionality is built-in. For earlier Android, the latest tooling brings most of this functionality via “API de-sugaring”.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda