How do I get default date and ti…

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

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

The DateFormat class allows you to format dates and times with predefined styles in a locale-sensitive manner. Formatting dates or times with the DateFormat class is a two-step process.

First, you create a formatter with the getDateInstance() method for formatting date or getTimeInstance()method for formatting time or getDateTimeInstance() when you want formatting both date and time.

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

package dateformat;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
 
public class TestDateFormat {
    public static void main(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 a DateFormat instance for the specified locale
            // and format a date object by calling the format
            // method.
            //
            DateFormat df = DateFormat.getDateInstance(
                    DateFormat.DEFAULT, locale);
            String date = df.format(today);
            sb.append("\n Default date format: ").append(date);
 
            //
            // Gets a DateFormat instance for the specified locale
            // and format a time information by calling the format
            // method.
            //
            DateFormat tf = DateFormat.getTimeInstance(
                    DateFormat.DEFAULT, locale);
            String time = tf.format(today.getTime());
            sb.append("\n Default time format: ").append(time)
                    .append("\n");
             
            DateFormat tf2 = DateFormat.getTimeInstance(
            DateFormat.SHORT, locale);
            String time2 = 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 Italy locale using
        // To display a date and time in the same String, create the
        // formatter with the getDateTimeInstance method.
        // The first parameter is the date style, and the second is
        // the time style. The third parameter is the Locale
        //
        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 produces output:

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值