Java 國際化 - 基於區域設定的日期格式化



Locale 可以用於在 SimpleDateFormat 類中基於模式建立特定於區域設定的格式。請參閱以下使用特定於區域設定的 SimpleDateFormat 類的示例。

示例

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class I18NTester {
   public static void main(String[] args) throws ParseException {

      Locale locale = new Locale("da", "DK");
      String pattern = "EEEEE MMMMM yyyy";

      SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

      Date date = new Date();

      System.out.println(date);
      System.out.println(simpleDateFormat.format(date));

      simpleDateFormat = new SimpleDateFormat(pattern,locale);

      System.out.println(simpleDateFormat.format(date));
   }
}

輸出

它將列印以下結果。

Fri Jun 07 15:02:27 IST 2024
Friday June 2024
fredag juni 2024
廣告

© . All rights reserved.