广州北大青鸟计算机职业培训学校
互联网技术培训、软件技术培训、大数据培训、云计算培训、数据分析培训信息网
当前位置:网站首页 > 软件教程 > Java技术 > 正文

惠州Java日期格式样式是什么?_北大青鸟IT学校

作者:邓华发布时间:2021-04-19分类:Java技术浏览:1032


导读:Java日期格式样式是什么?这是大家在学习Java的过程都会遇到的问题,那么下面我们一起来看看惠州北大青鸟学术部老师是怎么讲解这道题的。

Java日期格式样式是什么?这是大家在学习Java的过程都会遇到的问题,那么下面我们一起来看看惠州北大青鸟学术部老师是怎么讲解这道题的。

Date Format 类将五个样式定义为常量:

DateFormat.DEFAULT

DateFormat.SHORT

DateFormat.MEDIUM

DateFormat.LONG

DateFormat.FULL

DEFAULT 格式与 MEDIUM 相同。getInstance()使用 SHORT 。

下表显示了对于美国区域设置以不同样式格式化的相同日期。

样式            格式化日期

DEFAULT    Mar 27, 2014    

SHORT        3/27/14    

MEDIUM    Mar 26, 2014    

LONG         March 26, 2014    

FULL           Sunday, November 2, 2014    

例子

以下代码显示如何以简体中文格式显示语言环境的默认日期,法国和德国。

import java.text.DateFormat;

 import java.util.Date;

 import java.util.Locale;


 public class Main {

   public static void main(String[] args) {

     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);


     // 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 = DateFormat.getDateInstance(DateFormat.SHORT, locale);

     String formattedDate = formatter.format(date);

     System.out.println("SHORT: " + formattedDate);


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

     formattedDate = formatter.format(date);

     System.out.println("MEDIUM: " + formattedDate+"\n");


   }

 } 

上面的代码生成以下结果。

Java格式样式例子.png

java.util.Locale 类包含常见语言环境的常量。

我们可以使用 Locale.getDefault()方法获取系统的默认区域设置。

更多Java资讯关注网站动态,或者来惠州北大青鸟新方舟校区了解一下。

Java16.png

Java

标签:惠州计算机JAVA软件开发惠州计算机Java软件开发惠州计算机JAVA培训惠州计算机JAVA软件开发学校惠州计算机Java软件开发培训JAVAJava软件开发北大青鸟IT计算机学校北大青鸟IT软件学校北大青鸟IT学校


Java技术排行
标签列表
网站分类
文章归档
最近发表