SCJP解析系列之十一

 

  作者:刘颖

试题:
Given the exhibit:
d is valid ,non-null Date object
df is valid ,non-null DateFormat object set to the current local
    What outputs the current:local's country name and the appropriate version of d's date?
A. Locale loc = Locale.getLocal();
   System.out.println(loc.getDisplayCountry());
B. Locale loc = Locale.getDefault();
   System.out.println(loc.getDisplayCountry()+""+ df.format(d));
C. Locale loc = Locale.getLocal();
   System.out.println(loc.getDisplayCountry()+""+df.setDateFormat(d));
D. Locale loc = Locale.getDefault();
   System.out.println(loc.getDisplayCountry()+""+df.setDateFormat(d));

    这个程序让我们完成的功能就是得到当前国家的名字,然后显示根据当地习惯显示的日期. 我使用Locale类中提供的getDefault()方法获得此 Java 虚拟机实例的当前默认语言环境值。使用它的getDisplayCountry() 返回适合向用户显示的语言环境国家/地区名。DateFormat对象df的format()方法,将一个 Date 格式化为日期/时间字符串。这样就可以达到我们的要求了. 所以,答案为B.

    关于时间格式化问题,请参见帖子SCJP考试试题解析四.