0

How can I change the time format to Bangla from English without changing the app language?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

6 Answers6

4

Convert Bangla digit to basic Latin, and it should work...

public static String convertEnToBn(String data) {
        return data.replaceAll("0", "\u09E6")
                .replaceAll("1", "\u09E7")
                .replaceAll("2", "\u09E8")
                .replaceAll("3", "\u09E9")
                .replaceAll("4", "\u09EA")
                .replaceAll("5", "\u09EB")
                .replaceAll("6", "\u09EC")
                .replaceAll("7", "\u09ED")
                .replaceAll("8", "\u09EE")
                .replaceAll("9", "\u09EF");
    }
A.A Noman
  • 5,244
  • 9
  • 24
  • 46
Shawn Muktadir
  • 140
  • 2
  • 7
2

Theoretically this should work:

    Locale bangla = Locale.forLanguageTag("bn-BD");
    DateTimeFormatter timeFormatter = DateTimeFormatter
            .ofLocalizedTime(FormatStyle.FULL)
            .withLocale(bangla);
    String formattedTime
            = ZonedDateTime.now(ZoneId.of("Asia/Dhaka")).format(timeFormatter);
    System.out.println(formattedTime);

However the output on my desktop Java 7 is:

2:29:21 PM BDT

That is, I see no difference from English.

On Java 9 I do get a difference:

10:32:00 অপরাহ্ণ বাংলাদেশ মানক সময়

What you get on Android I dare not tell, but you can try.

Instead of FormatStyle.FULL you may specify .SHORT, MEDIUM or LONG depending on how long of a format you want.

I am using ThreeTen Backport, the backport of java.time, the modern Java date and time API, to Java 6 and 7. For older Android (under API level 26) use the Android adaption of the same, ThreeTenABP. See the links at the bottom.

I use language tag bn-BD for Bangla (Bengali) as spoken in Bangladesh. You may also try bn alone if you don’t want to specify a country, or bn-IN for Bangla as spoken in India. It may or may not make a difference.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1

This is a Kotlin extension function. Using this you can convert any English digit to Bangla.

fun String.englishNumberChangeToBanglaNumber(): String {
    val hash = HashMap<Int, String>()
    hash[0] = "০"
    hash[1] = "১"
    hash[2] = "২"
    hash[3] = "৩"
    hash[4] = "৪"
    hash[5] = "৫"
    hash[6] = "৬"
    hash[7] = "৭"
    hash[8] = "৮"
    hash[9] = "৯"
    var banglaDate = ""
    this.forEach {
        try {
            val digit = it.toString().toInt()
            banglaDate += hash[digit]
        } catch (e: Exception) {
            banglaDate += it
        }
    }
    return banglaDate

}

Now use this way in Kotlin,

"10/12/2020".fun String.englishNumberChangeToBanglaNumber()

output: ১০/১২/২০২০

Make sure your string is in English.

Shaon
  • 2,496
  • 26
  • 27
0

Specify the Locale using the bn-IN language tag when obtaining the date formatter.

ULocale bangla = ULocale.forLanguageTag("bn-IN");
SimpleDateFormat timeFormat = SimpleDateFormat.getTimeInstance(DateFormat.LONG, bangla);
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Would it work with `bn` (Bangla/Bengali without country) or `bn-BD` (Bangla/Bangladesh)? And you had seen this question coming: is there a solution without the notoriously troublesome and long outdated `SimpleDateFormat` class? – Ole V.V. Jul 10 '19 at 08:39
  • 1
    @OleV.V. Yes, all 3 of `bn`, `bn-BD`, and `bn-IN` would work. To use better Date/Time API, use the [JSR-310 Android Backport](https://github.com/JakeWharton/ThreeTenABP). – Andreas Jul 10 '19 at 16:08
0

How about this one:

public static String convertBnToEn(String data) {
    return data.replaceAll("০", "0")
            .replaceAll("১", "1")
            .replaceAll("২", "2")
            .replaceAll("৩", "3")
            .replaceAll("৪", "4")
            .replaceAll("৫", "5")
            .replaceAll("৬", "6")
            .replaceAll("৭", "7")
            .replaceAll("৮", "8")
            .replaceAll("৯", "9");
}
Tushar Monirul
  • 4,944
  • 9
  • 39
  • 49
0

Using Kotlin extension function

fun String.enDigitToBn():String{
    val bnDigits = listOf('০','১','২','৩','৪','৫','৬','৭','৮','৯')
    return this.map { bnDigits[it.toString().toInt()] }.joinToString("")
}