集册 Java实例教程 使用预定义的日期格式

使用预定义的日期格式

欢马劈雪     最近更新时间:2020-01-02 10:19:05

468
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用预定义的日期格式

import java.text.DateFormat;

import java.util.Date;

import java.util.Locale;
/**
 from
* 时代Java公众号 
**/


public class Main {

  public static void main(String[] args) {

    // Get the current date

    Date today = new Date();


    // Print date in the default locale format

    Locale defaultLocale = Locale.getDefault();

    printLocaleDetails(defaultLocale);

    printDate(defaultLocale, today);


    // Print date in French format

    printLocaleDetails(Locale.FRANCE);

    printDate(Locale.FRANCE, today);/** 来 自 N o w J a v a . c o m**/


    // Print date in German format. We could also use Locale.GERMANY

    // instead of new Locale ("de", "DE").

    Locale germanLocale = new Locale("de", "DE");

    printLocaleDetails(germanLocale);

    printDate(germanLocale, today);

  }


  public static void printLocaleDetails(Locale locale) {

    String languageCode = locale.getLanguage();

    String languageName = locale.getDisplayLanguage();

    String countryCode = locale.getCountry();

    String countryName = locale.getDisplayCountry();

    // Print the locale info

    System.out.println("Language: " + languageName + "(" + languageCode + "); "

        + "Country: " + countryName + "(" + countryCode + ")");

  }


  public static void printDate(Locale locale, Date date) {

    DateFormat formatter;

    String formattedDate;


    // Format and print the date in SHORT style

    formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);

    formattedDate = formatter.format(date);

    System.out.println(
展开阅读全文