从Date类型转为中文字符串
Posted Zhon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从Date类型转为中文字符串相关的知识,希望对你有一定的参考价值。
//主方法 public static String DateToCh(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH) + 1; int day = cal.get(Calendar.DAY_OF_MONTH); return getYear(year) + getTenString(month) + "月" + getTenString(day) + "日"; } public static String getYear(int year) { String result = ""; for (int i = 0; i < 4; i++) { Integer s = Integer.parseInt((year + "").substring(i, i + 1)); switch (s) { case 0: result += "〇"; break; case 1: result += "一"; break; case 2: result += "二"; break; case 3: result += "三"; break; case 4: result += "四"; break; case 5: result += "五"; break; case 6: result += "六"; break; case 7: result += "七"; break; case 8: result += "八"; break; case 9: result += "九"; break; default: break; } } return result; } //获取月 日 public static String getTenString(int ten) { String ALL_CN_NUMBER = "一二三四五六七八九"; if (ten==0){ return ""; }else if(ten < 10) { return String.valueOf(ALL_CN_NUMBER.charAt(ten - 1)); } else if (ten == 10) { return "十"; }else { String x = getTenString(Integer.parseInt((ten + "").substring(0, 1))); String y = getTenString(Integer.parseInt((ten + "").substring(1, 2))); return x + "十" + y; } }
以上是关于从Date类型转为中文字符串的主要内容,如果未能解决你的问题,请参考以下文章