JAVA的规则-开发篇(2)
(6) 国际化开发建议:不要对日期对象使用'date.tostring ()'
不要使用'date.tostring ()'方法,日期格式对于地区和语言不同的国家来说是不一样的,务必不要使用。
错误示例:'dateformat'类提供了一个预定义的格式类型来指定本地的格式。
public void printtoday () {
date today = new date ();
string todaystr = today.tostring ();
system.out.println (todaystr);
}
正确示例:
public void printtoday () {
locale currentlocale = locale.getdefault ();
dateformat dateformatter = dateformat.getdateinstance (
dateformat.default, currentlocale);
date today = new date ();
string todaystr = dateformatter.format (today);
system.out.println (todaystr);
}
参考:http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html
http://java.sun.com/docs/books/tutorial/i18n/format/dateformat.html
(7) 国际化开发建议:不要对数字变量使用'tostring ()'方法
在全球化的开发中,不要对数字变量使用'tostring ()'方法,对于java.lang.number的任何子类都适用。包括:bigdecimal, biginteger, byte, double, float, integer, long, and short.对于这样的情况,java里也与定义了"numberformat"方法来格式化。
错误示例:
public class nts {
public void method (double amount) {
string amountstr = amount.tostring ();
system.out.println (amountstr);
}
}
正确示例:
public class ntsfixed {
public void method (double amount) {
locale currentlocale = locale.getdefault ();
numberformat numberformatter =
numberformat.getnumberinstance (currentlocale);
string amountstr = numberformatter.format (amount); //
system.out.println (amountstr + ' ' + currentlocale.tostring ());
}
}
参考:http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html
http://java.sun.com/docs/books/tutorial/i18n/format/numberformat.html
(8) 国际化开发建议:不要使用'string.equals ()'方法
建议不要使用'string.equals ()'方法,因为在统一字符比较标准中不一定按照相关的顺序来比较。'collator'提供的预定义整理规则来排序string,collator类调用'getinstance ()'方法,一般来说,可以为默认的本地创建一个collator。例如:collator mycollator = collator.getinstance ();创建collator的时候你也可以指定一个特殊的locale。例如:collator myfrenchcollator = collator.getinstance (locale.french);然后就可以调用'collator.compare ()'来执行一个本地的字符比较mycollator.compare (s1,s2);从这里可以了解更多的有关collator类的信息:http://java.sun.com/docs/books/tutorial/i18n/text/collationintro.html
错误示例:
public class se {
public boolean compstr (string s1, string s2) {
boolean b = (s1.equals (s2));
return b;
}
}
正确示例:
public class sefixed {
public boolean compstr (string s1, string s2) {
collator mycollator = collator.getinstance ();
boolean b = (mycollator.compare(s1,s2) == 0);
return b;
}
}
参考:http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html
http://java.sun.com/docs/books/tutorial/i18n/text/locale.html
(9) 国际化开发建议:不要使用'stringtokenizer()'方法
错误示例:stringtokenizer st = new stringtokenizer(str);
可以从这里得到更多的信息:‘
参考:http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html
(10) 国际化开发建议:不要使用'time.tostring ()'方法
因为时间的格式各个国家也不一样。如果你使用日期格式类,你的应用就能够在世界上各个地方正确的显示时间和日期了。首先,用'gettimeinstance ()'方法创建一个formatter。然后,调用'format ()'方法。
错误示例:
public class tts {
public void printtime (time t1) {
string timestr = t1.tostring ();
system.out.println (timestr);
}
}
正确示例:
import java.sql.time;
import java.text.dateformat;
import java.util.locale;
(文章来源 www.iocblog.net)
public class ttsfixed {
public void printtime (time t1) {
dateformat timeformatter = dateformat.gettimeinstance(
dateformat.default, locale.getdefault ());
string timestr = timeformatter.format(t1);
system.out.println (timestr);
文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。