0

On Android API 19, setting locale to hy, time is displayed as կեսօրից առաջ instead of AM or ԿԱ. The string I get is way too long to fit in the UI. I am displaying time with

<TextClock/> 

I am setting the 12h format with this code:

Settings.System.putString(getContentResolver(), Settings.System.TIME_12_24, "12");

I tried running the same code in java fiddle: https://www.mycompiler.io/view/HnfUgdPt9zt Here I get AM as expected. If I try different locale, for example "sl" I get the same result in the java fiddle and in the Android project.

My code:

DateFormat timeOnly = new SimpleDateFormat("h:mm aa", new Locale("hy"));

Date fullDate =  new Date();
String callTime = timeOnly.format(fullDate);

I also tried to set AM and PM strings like this:

DateFormatSymbols sym = new DateFormatSymbols(locale);
sym.setAmPmStrings(new String[] {"AM", "PM"});

But I don't know how to apply that format to system time.

  • I was now able to resolve this issue with extending TextClock class with help of this link: https://medium.com/@tapuranjannahak/android-textclock-customization-to-deviate-from-system-time-format-b1ab05db7f3a. So every time I set a 12 hour format, it sets hardcoded value if the language is set to Armenian. It's not an ideal solution because problem like this could happen on other languages as well and you would have to hard code each one. – Matevž Jecl Dec 08 '22 at 14:55
  • Consider not using `DateFormat`, `SimpleDateFormat`, DateFormatSymbols` and `Date`. Those classes are notoriously troublesome and long outdated. Use `LocalTime`, `DateTimeFormatter` and `DateTimeFormatterBuilder`, all from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/index.html). If for early Android, then through [desugaring](https://developer.android.com/studio/write/java8-support). – Ole V.V. Dec 08 '22 at 21:24
  • Using java.time I would first try `DateTimeFormatter f = new DateTimeFormatterBuilder() .appendPattern("h:mm ") .appendText(ChronoField.AMPM_OF_DAY, TextStyle.NARROW) .toFormatter(Locale.forLanguageTag("hy"));`. Example outputs on my desktop Java include `8:04 ա` and `9:06 հ`. I don’t know whether you get the same on Android, and it may vary between devices. If not satisfied, set the texts explicitly using `.appendText(ChronoField.AMPM_OF_DAY, Map.of(0L, "AM", 1L, "PM"))`. Now you are sure to get what you asked for: `8:04 AM` and `9:06 PM`. – Ole V.V. Dec 08 '22 at 21:36
  • Related (though unanswered): [Android DateTimeFormatter - Time Conversion not working on samsung devices](https://stackoverflow.com/questions/74528658/android-datetimeformatter-time-conversion-not-working-on-samsung-devices) – Ole V.V. Dec 08 '22 at 21:45

1 Answers1

0
just format your date string like this

startTime = "2013-02-27 21:06:30";
    StringTokenizer tk = new StringTokenizer(startTime);
    String date = tk.nextToken();  
    String time = tk.nextToken();
    
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
    SimpleDateFormat sdfs = new SimpleDateFormat("hh:mm a");
    Date dt;
    try {    
        dt = sdf.parse(time);
        System.out.println("Time Display: " + sdfs.format(dt)); // <-- I got result here
    } catch (ParseException e) {
        e.printStackTrace();
    }
Dhruv Sakariya
  • 792
  • 4
  • 10
  • 1
    I don't have a problem with setting am/pm time format. I have a problem specifically with Armenian language where the am/pm strings are really long for some reason. And I also got the desired format to a variable, but that's not enough since I have to apply it to TextClock widget, which takes time directly from the Android. If you check the original post's comment I posted a workaround. – Matevž Jecl Dec 08 '22 at 14:57