0

I'm trying to show the lokalize date to Chinese and Vietnamese.But somehow AM/PM is not translating to Vietnamese.

Locale locale = new Locale("vi","VN");
String p1 = "MMM dd, yyy 'at' h:mm a";
SimpleDateFormat dateFormat = new SimpleDateFormat(p1, locale);
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Manu Ram V
  • 369
  • 1
  • 3
  • 23
  • 1
    As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Nov 12 '19 at 07:18
  • 1
    I cannot reproduce. On my desktop Java 9 I get `thg 11 12, 2019 at 8:25 SA`. I cannot tell whether SA is Vietnamese, but it doesn’t appear to be English. Which result are you getting on Android? And on which Android version or API level? – Ole V.V. Nov 12 '19 at 07:26

3 Answers3

1

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Solution using java.time, the modern Date-Time API:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM dd, uuuu 'at' h:mm a", new Locale("vi", "VN"));
        System.out.println(LocalDateTime.of(2021, 8, 4, 10, 20).format(dtf));
        System.out.println(LocalDateTime.of(2021, 8, 4, 20, 30).format(dtf));
    }
}

Output from a sample run:

thg 8 04, 2021 at 10:20 SA
thg 8 04, 2021 at 8:30 CH

ONLINE DEMO

Learn more about the modern Date-Time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

If you want to show AM/PM then you will try with Locale.ENGLISH:

Locale locale = Locale.ENGLISH;
String p1 = "MMM dd, yyy 'hiat' h:mm a";
SimpleDateFormat dateFormat = new SimpleDateFormat(p1, locale);
John Le
  • 1,116
  • 9
  • 12
0

For anyone who still searching for this topic, I have written it manually. I upload my file here for you in case you don't want to write it.

Usage:

val spf = SimpleDateFormat("EEEE, dd/MM/yy hh:mm a", Locale.US)
val dateStr = spf.formatToVN(date)
Dzung Vu
  • 56
  • 1
  • 6