// 大家有没有更高效的方法呢?来讨论下,别贴千行代码…
public class TestClass { public static void main(String[] args) {
System.out.print(testYear(1987) + "年" + testMonth(3) + "月"
+ testDay(22) + "日");
} /**
* 年转换成中文
*
* @param year
* 年
*
* @return 中文年
*/
public static String testYear(int year) {
char[] c = Integer.toString(year).toCharArray();
StringBuffer sb = new StringBuffer();
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>();
String[] capital = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
int i = 0;
for (String str : capital) {
hashtable.put(i++, str);
}
for (char charYear : c) {
sb.append(hashtable.get(Integer.parseInt(Character
.toString(charYear))));
}
return sb.toString();
} /**
* 月份转换成中文
*
* @param month
* 1 ~ 12 之间
* @return 中文月份
*/
public static String testMonth(int month) { Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>();
String[] capital = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十",
"十一", "十二" };
int i = 0;
for (String str : capital) {
hashtable.put(++i, str);
}
String str = hashtable.get(month);
return str;
} /**
* 日期转换成中文
*
* @param day
* 1 ~ 31 之间
* @return 中文日期
*/
public static String testDay(int day) {
StringBuffer sb = new StringBuffer();
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>();
String[] capital = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
int i = 0;
for (String str : capital) {
hashtable.put(++i, str);
}
char[] c = Integer.toString(day).toCharArray();
if (2 == c.length) {
if (c[0] == '1') {
sb.append(capital[9]);
if (c[1] != '0') {
sb.append(hashtable.get(Integer.parseInt(Character
.toString(c[1]))));
}
}
if (c[0] == '2') {
sb.append(capital[1] + capital[9]);
if (c[1] != '0') {
sb.append(hashtable.get(Integer.parseInt(Character
.toString(c[1]))));
}
}
if (c[0] == '3') {
sb.append(capital[2] + capital[9]);
if (c[1] != '0') {
sb.append(hashtable.get(Integer.parseInt(Character
.toString(c[1]))));
} } } else {
sb
.append(hashtable.get(Integer.parseInt(Character
.toString(c[0]))));
}
return sb.toString();
}
}
public class TestClass { public static void main(String[] args) {
System.out.print(testYear(1987) + "年" + testMonth(3) + "月"
+ testDay(22) + "日");
} /**
* 年转换成中文
*
* @param year
* 年
*
* @return 中文年
*/
public static String testYear(int year) {
char[] c = Integer.toString(year).toCharArray();
StringBuffer sb = new StringBuffer();
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>();
String[] capital = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
int i = 0;
for (String str : capital) {
hashtable.put(i++, str);
}
for (char charYear : c) {
sb.append(hashtable.get(Integer.parseInt(Character
.toString(charYear))));
}
return sb.toString();
} /**
* 月份转换成中文
*
* @param month
* 1 ~ 12 之间
* @return 中文月份
*/
public static String testMonth(int month) { Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>();
String[] capital = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十",
"十一", "十二" };
int i = 0;
for (String str : capital) {
hashtable.put(++i, str);
}
String str = hashtable.get(month);
return str;
} /**
* 日期转换成中文
*
* @param day
* 1 ~ 31 之间
* @return 中文日期
*/
public static String testDay(int day) {
StringBuffer sb = new StringBuffer();
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>();
String[] capital = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
int i = 0;
for (String str : capital) {
hashtable.put(++i, str);
}
char[] c = Integer.toString(day).toCharArray();
if (2 == c.length) {
if (c[0] == '1') {
sb.append(capital[9]);
if (c[1] != '0') {
sb.append(hashtable.get(Integer.parseInt(Character
.toString(c[1]))));
}
}
if (c[0] == '2') {
sb.append(capital[1] + capital[9]);
if (c[1] != '0') {
sb.append(hashtable.get(Integer.parseInt(Character
.toString(c[1]))));
}
}
if (c[0] == '3') {
sb.append(capital[2] + capital[9]);
if (c[1] != '0') {
sb.append(hashtable.get(Integer.parseInt(Character
.toString(c[1]))));
} } } else {
sb
.append(hashtable.get(Integer.parseInt(Character
.toString(c[0]))));
}
return sb.toString();
}
}
public static String []capital = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
public static void main(String[] args) {
System.out.print(testYear(1987) + "年" + testMonth(3) + "月"
+ testMonth(22) + "日");
}
/**
* 年转换成中文
*
* @param year
* 年
*
* @return 中文年
*/
public static String testYear(int year) {
String strYear = year+"";
return capital[strYear.charAt(0)-'0']+capital[strYear.charAt(1)-'0']+capital[strYear.charAt(2)-'0']+capital[strYear.charAt(3)-'0'];
}
public static String testMonth(int num) {
if(num>10)
{
return capital[num/10]+"十"+capital[num%10];
}else
{
return capital[num];
}
}
}
charAt(0)返回索引0处的char值,如题意charAt(0)处的字符为‘1’,则其ASCII码为49,而字符‘0’的ASCII码为48,所以charAt(0)-'0'的ASCII码值为1,则求得capital[1]=‘一’即charAt(0) - '0'是通过ASCII码来求得数组capital的下标值...
charAt(0)返回索引0处的char值,如题意charAt(0)处的字符为‘1’,则其ASCII码为49,而字符‘0’的ASCII码为48,所以charAt(0)-'0'的ASCII码值为1,则求得capital[1]=‘一’即charAt(0) - '0'是通过ASCII码来求得数组capital的下标值...以此类推!