Given the following ThreeTenBp based DateFormatter:
class DateFormatter private constructor() {
private val dateShortTimeShortFormatter =
org.threeten.bp.format.DateTimeFormatter.ofLocalizedDateTime(
FormatStyle.SHORT, FormatStyle.SHORT)
fun getFormattedDateTimeShort(time: Long): String {
return dateShortTimeShortFormatter.withZone(ZoneId.systemDefault())
.format(Instant.ofEpochMilli(time))
}
/* ... */
}
I am running the following test on Ubuntu 20.04 in the GNOME Terminal shell (LANG=en_US.UTF-8):
class DateFormatterTest {
private val systemTimezone = TimeZone.getDefault()
private val systemLocale = Locale.getDefault()
@Before
fun resetTimeZone() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT+1"))
}
@After
fun resetSystemDefaults() {
Locale.setDefault(systemLocale)
TimeZone.setDefault(systemTimezone)
}
@Test
fun getFormattedDateTimeShort() {
Locale.setDefault(Locale.US)
assertThat(DateFormatter.newInstance().getFormattedDateTimeShort(1548115200000L))
.isEqualTo("1/22/19 1:00 AM")
}
}
It succeeds.
When I run it in Android Studio 4.2 Beta 3 or Android Studio 2020.3.1 Canary 4 it fails with the following error:
org.junit.ComparisonFailure:
Expected :"1/22/19 1:00 AM"
Actual :"1/22/19, 1:00 AM"
Java news as of 16.01.2021
Based Ole V.V.'s comment and answer I figured out that the test behaves different in the shell depending on the Java version. Gradle picks up JAVA_HOME - therefore I need to update the environment variable. Please note that changing the symlink to the java executable via sudo update-alternatives --config java has no effect.
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
-> Test succeeds
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
-> Test fails
export JAVA_HOME=/usr/lib/jvm/java-14-openjdk-amd64
-> Process 'Gradle Test Executor 1' finished with non-zero exit value 134
Java/JRE 9 is not available for this Ubuntu version.
Solution: Java/JDK in Android Studio
In the IDE I can also change the JDK location for the project via File > Project Structure ... > SDK location:
Once I change from the pre-configured JDK 11 (within the installation folder of the IDE) to JDK 8 - then the test succeeds!
