Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Sunday, February 3, 2008

java locale的设置

今天碰到一个问题,指定了一个SimpleDateFormat的格式为yyyy-MMM-dd,然后传一个字符串"2008-Feb-03"让他解析,结果在家里的机器上没有问题,但在公司的机器上说不认识"Feb",我倒~~!

比较了2个java环境,家里的locale是US,公司的是CN,所以公司机器要使用"2月"这个形式而不是"Feb"。所以只能在公司机器的java程序里设置locale。

设置代码:
Locale locale=new Locale("en","US");
Locale.setDefault(locale);
SimpleDateFormat format=new SimpleDateFormat("yyyy-MMM-dd",locale);
Date date=format.parse("2008-Feb-03");
System.out.println(format.format(date));

Tuesday, January 29, 2008

java读取utf-8编码文件

发觉直接读中文会出现乱码,所以要这样:
BufferedReader br=new BufferedReader(new InputStreamReader (new FileInputStream(new File("utf-8.txt")),"UTF-8"));

Tuesday, March 20, 2007

get the last day in a month in java

from http://forum.java.sun.com/thread.jspa?threadID=700253&messageID=4063642
it says:

// create calendar instance :: now
Calendar calendar = Calendar.getInstance();
int lastDateofThisMonth = calendar.getActualMaximum(Calendar.DATE);



Just check this one and understand how to expand

// create calendar instance :: now
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,2000);
// month 0 is january
calendar.set(Calendar.MONTH,1);
int lastDateofMonth = calendar.getActualMaximum(Calendar.DATE);
System.out.println(lastDateofMonth);