How do I get default date and ti…

来源:互联网 发布:nginx lua waf 编辑:程序博客网 时间:2024/05/21 06:21

How do I get default date and time format for a definedcountry?

Category: java.text examples,viewed: 2K time(s).
【http://www.kodejava.org/examples/678.html】

The DateFormat classallows you to format dates and times with predefined styles in alocale-sensitive manner. Formatting dates or times withthe DateFormat classis a two-step process.

First, you create a formatter withthe getDateInstance() methodfor formatting date or getTimeInstance()methodfor formatting time or getDateTimeInstance() whenyou want formatting both date and time.

Second, you invoke the format method, which returns a Stringcontaining the formatted date. The following example formatstoday's date and time by calling those two methods.

?
packagedateformat;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
 
public class TestDateFormat {
    public static voidmain(String[] args) {
       Locale[] locales = {
              Locale.CANADA,
              Locale.FRANCE,
              Locale.GERMANY,
              Locale.US,
              Locale.JAPAN,
              Locale.ITALY
       };
       Date today = new Date();
 
       for (Locale locale : locales) {
          StringBuilder sb = new StringBuilder();
          sb.append(locale.getDisplayCountry());
          sb.append("\n--------------------------------");
 
           //
           // Gets aDateFormat instance for the specified locale
           // andformat a date object by calling the format
           //method.
           //
           DateFormatdf = DateFormat.getDateInstance(
                 DateFormat.DEFAULT, locale);
           Stringdate = df.format(today);
          sb.append("\n Default date format: ").append(date);
 
           //
           // Gets aDateFormat instance for the specified locale
           // andformat a time information by calling the format
           //method.
           //
           DateFormattf = DateFormat.getTimeInstance(
                 DateFormat.DEFAULT, locale);
           Stringtime = tf.format(today.getTime());
          sb.append("\n Default time format: ").append(time)
                 .append("\n");
          
           DateFormattf2 = DateFormat.getTimeInstance(
          DateFormat.SHORT, locale);
           Stringtime2 = tf2.format(today.getTime());
          sb.append("\n Short time format: ").append(time2)
          .append("\n");
 
          System.out.println(sb.toString());
       }
 
       //
       // Gets date and time formatted value for Italylocale using
       // To display a date and time in the sameString, create the
       // formatter with the getDateTimeInstancemethod.
       // The first parameter is the date style, andthe second is
       // the time style. The third parameter is theLocale
       //
       DateFormat dtf =DateFormat.getDateTimeInstance(
              DateFormat.DEFAULT,DateFormat.DEFAULT,
              Locale.ITALY);
       String datetime = dtf.format(today);
 
       System.out.println("date time format in "+
             Locale.ITALY.getDisplayCountry() + ": " + datetime);
    }
}

Here are the producesoutput:

Canada

--------------------------------

 Default date format: 5-Sep-2012

 Default time format: 2:56:14 PM


 Short time format: 2:56 PM


France

--------------------------------

 Default date format: 5 sept. 2012

 Default time format: 14:56:14


 Short time format: 14:56


Germany

--------------------------------

 Default date format: 05.09.2012

 Default time format: 14:56:14


 Short time format: 14:56


United States

--------------------------------

 Default date format: Sep 5, 2012

 Default time format: 2:56:14 PM


 Short time format: 2:56 PM


Japan

--------------------------------

 Default date format: 2012/09/05

 Default time format: 14:56:14


 Short time format: 14:56


Italy

--------------------------------

 Default date format: 5-set-2012

 Default time format: 14.56.14


 Short time format: 14.56


date time format in Italy: 5-set-2012 14.56.14


0 0
原创粉丝点击