Skip to content

[OffsetDateTime.format] Different result between robolectric and emulator #7910

Closed

Description

@likai79511

Description

When i execute OffsetDateTime.format, different results will occur between Robolectric and Device/Emulator.

Steps to Reproduce

@Test
fun test() {
    Locale.setDefault(Locale("en", "AU"))

    val time = OffsetDateTime.parse("2023-01-01T10:00:00Z")
    val formatter = DateTimeFormatter.ofPattern("d MMM yyyy")

    println("FINGERPRINT: ${Build.FINGERPRINT}\nresult:${time.format(formatter)}")
}
Robolectric device/emulator
image image

Robolectric : 1 Jan. 2023 (with a ".")
Device/Emulator: 1 Jan 2023

Robolectric & Android Version

org.robolectric:robolectric:4.9.2
junit:junit:4.13.2

Link to a public git repo demonstrating the problem:

Activity

  1. hoisie commented on Jan 5, 2023

    @hoisie
    Contributor

    Unfortunately OffsetDateTime is a Java core class, so any differences are due to the implementation differences between the JDK and Libcore (Android's implementation of the Java core classes).

    This is the Android version:
    https://cs.android.com/android/platform/superproject/+/master:libcore/ojluni/src/main/java/java/time/OffsetDateTime.java;l=122?q=OffsetDateTime&sq=

  2. okamayana-tinyspeck commented on May 11, 2023

    @okamayana-tinyspeck

    Unfortunately OffsetDateTime is a Java core class, so any differences are due to the implementation differences between the JDK and Libcore (Android's implementation of the Java core classes).

    This is the Android version: https://cs.android.com/android/platform/superproject/+/master:libcore/ojluni/src/main/java/java/time/OffsetDateTime.java;l=122?q=OffsetDateTime&sq=

    Is this also the case for DateTimeFormatter? Also, are there any workarounds?

  3. MGaetan89 commented on Jul 8, 2024

    @MGaetan89
    Member

    It's the same for DateTimeFormatter, yes.
    We have documented it here, for the timezone difference: https://robolectric.org/best-practices/#android-libcore-and-openjdk-have-different-implementations

    For your issue regarding month name, we can add a dedicated section, if there is a known alternative/workaround.

  4. MGaetan89 commented on Jul 17, 2024

    @MGaetan89
    Member

    We've added a section on Robolectric's website for this case: https://robolectric.org/best-practices/#month-names
    As @hoisie pointed out, these classes are part of the Java API, and Robolectric cannot alter their behavior.
    You can run your tests on an Android device to get the correct result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions