Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

226
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda