前段时间,在做项目的时候遇到个date转换成二〇〇九这种中文格式的,想偷懒一下在网上找了好久都没有找到,最后自己写了个,也供大家参考下,有好的建议都可以提出来。
public String DateToCN(Date date) {
if (null == date || "".equals(date)) {
return null;
}
String[] CN = { "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
StringBuffer cn = new StringBuffer();
String year = String.valueOf(calendar.get(Calendar.YEAR));
for (int i = 0; i < year.length(); i++) {
cn.append(CN[year.charAt(i) - 48]);
}
cn.append("年");
int mon = calendar.get(Calendar.MONTH) + 1;
if (mon < 10) {
cn.append(CN[mon]);
} else if (mon < 20) {
if (mon == 10) {
cn.append("十");
} else {
cn.append("十").append(CN[mon % 10]);
}
}
cn.append("月");
int day = calendar.get(Calendar.DAY_OF_MONTH);
if (day < 10) {
cn.append(CN[day]);
} else if (day < 20) {
if (day == 10) {
cn.append("十");
} else {
cn.append("十").append(CN[day % 10]);
}
} else if (day < 30) {
if (day == 20) {
cn.append("二十");
} else {
cn.append("二十").append(CN[day % 10]);
}
} else {
cn.append("三十").append(CN[day % 10]);
}
cn.append("日");
return cn.toString();
}