androidkotlintime

Strange timestamp using calendar


I am trying to get the current timestamp of the android device

Below is the code

val calendar = Calendar.getInstance()
val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val currentTimeStamp = simpleDateFormat.format(calendar.time).toString()

In some of the devices I am getting timestamp as \u0662\u0660\u0662\u0664-\u0660\u0668-\u0660\u0665 \u0660\u0668:\u0662\u0661:\u0661\u0669

What kind of format is this?


Solution

  • The issue you're encountering is likely related to the locale settings of the device. The Unicode characters you see (\u0662, \u0660, etc.) are Arabic numerals, which indicate that the device's locale settings might be using an Arabic script for numbers.

    To ensure that the timestamp is formatted with standard Arabic numerals (0-9) do :

    import java.text.SimpleDateFormat
    import java.util.*
    
    val calendar = Calendar.getInstance()
    val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", 
    Locale.US)
    val currentTimeStamp = simpleDateFormat.format(calendar.time).toString()
    

    By specifying Locale.US, you ensure that the date and time format uses the standard numerals and formats regardless of the device's locale.