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

90
Visualizações
SimpleDateFormat is not returning the expected value in unit test

I have a logic to extract the exact time from the given date (e.g: 12:00 PM).

At the end of the logic, I'm using the SimpleDateFormat to parse the date. When I run the code on the Android devices everything is working fine. Also when I run the unit tests on my MacBook, windows os, and Bitbucket pipeline all the tests are passed. But, when I run the unit tests on the MacBook M1-pro 2021 with the same Android studio and codebase one of the tests is failed.

This is the part of the code to format the date:

val output = SimpleDateFormat("hh:mm a")
return output.format(date)

The expected result is something like 12:00 PM which is totally fine in those scenarios I mentioned.

this is the UI on my Android device:

But on the MacBook M1-pro 2021 test fails because the formatter returns the 12:00 p.m.:

expected: 12:00 PM
but was : 12:00 p.m.

This is the result of the unit test on MacBook M1-pro 2021

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I'm assuming you're implicitly relying on the JVM's current default locale, and that locale differs between your various test environments.

Or you are using different versions of Java or Android that have different definitions of the local rules.

And, another problem: You're using terrible datetime classes that were supplanted years ago by the modern java.time classes defined in JSR 310.

The java.time classes are included with Java 8+ and with Android 26+. For older versions of Android, use the latest tools to provide most of the functionality of java.time via "API desugaring".

Convert your legacy java.util.Date class to its java.time.Instant replacement by calling new conversion methods added to the old classes.

 Instant instant = myJavaUtilDate.toInstant() ;

Set to your desired time zone.

 ZoneId z = ZoneId.of( "America/Edmonton" ) ; ZonedDateTime zdt = instant.atZone( z ) ;

Extract the portion of time, the focus of your Question.

 LocalTime lt = zdt.toLocalTime() ;

Automatically locates the exit. Specify the Locale explicitly. The Locale determines the human language and cultural norms that will apply to the locale.

 Locale locale = Locale.CANADA ; DateTimeFormatter f = DateTimeFormatter.ofLocalizedTime( FormatStyle.SHORT ).withLocale( locale ) ; String output = lt.format( lt ) ;

Note that Java 9 changed its default behavior to use the much richer set of locale definitions in the Common Locale Data Repository (CLDR) provided by the Unicode Consortium. This change may cause some differences, such as the AM/PM format. Also, CLDR data continues to evolve, so later versions may change some rules.

Android may have also changed the rules between versions, but I don't know the history there.

I suggest you don't worry about such minor differences in localized formatting. But if you insist, you will have to:

  • Control the Locale your code depends on, either implicitly or explicitly.
  • Check the source and version of the locale definitions/rules used by that Locale .
over 4 years ago · Santiago Trujillo Relatório

0

try this

val currentTime = SimpleDateFormat("hh:mm a", Locale.getDefault()).format(Date())
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