DateConvertor
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateConvertor {
public static void main(String[] args)throws Exception {
Long day = 1000L * 60 * 60 * 24;
Long month = day * 30;
Long year = day * 365;
Date date = new Date();
// for (int i = 0; i < 10000; i++) {
// long time = date.getTime();
// time = time + day * i;
System.out.println(formatStr(new Date()));
// }
}
/**
* @param date 时间
* @return 汉字年月日
*/
public static String formatStr(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
String format = sdf.format(date);
String[] split = format.split("-");
//2020-7-18 ==> 二零二零年七月十八日
String year = split[0];
String month = split[1];
String day = split[2];
String strYear = getStrYear(year);
String strMonth = getStrMonth(month);
String strDay = getStrDay(day);
String formatStr = strYear + "年" + strMonth + "月" + strDay + "号";
return formatStr;
}
/**
* @param day 数字日 13
* @return 汉字日 13==>十三
*/
public static String getStrDay(String day) {
int dayLength = day.length();
String strDay = "";
if (dayLength < 2) {
strDay = numToStr(day);
return strDay;
} else {
if (day.startsWith("1")) {
if (day.equals("10")) {
return "十";
} else {
String substring = day.substring(1);
strDay = "十" + numToStr(substring);
return strDay;
}
} else if (day.startsWith("2")) {
if (day.equals("20")) {
return "二十";
} else {
String substring = day.substring(1);
strDay = "二十" + numToStr(substring);
return strDay;
}
} else if (day.startsWith("3")) {
if (day.equals("30")) {
return "三十";
} else {
String substring = day.substring(1);
strDay = "三十" + numToStr(substring);
return strDay;
}
}
}
return "";
}
/**
* @param month 数字月 7
* @return 中文月 7==>七
*/
public static String getStrMonth(String month) {
String strMonth = numToStr(month);
return strMonth;
}
/**
* @param year 数字年 2020
* @return 汉字年 二零二零
*/
public static String getStrYear(String year) {
int length = year.length();
String strYear = "";
for (int i = 0; i < length; i++) {
String substring = year.substring(i, i + 1);
strYear = strYear + numToStr(substring);
}
return strYear;
}
/**
* @param nun 0~9 ==>零~九
* @return
*/
public static String numToStr(String nun) {
if (nun.equals("0")) {
return "零";
} else if (nun.equals("1")) {
return "一";
} else if (nun.equals("2")) {
return "二";
} else if (nun.equals("3")) {
return "三";
} else if (nun.equals("4")) {
return "四";
} else if (nun.equals("5")) {
return "五";
} else if (nun.equals("6")) {
return "六";
} else if (nun.equals("7")) {
return "七";
} else if (nun.equals("8")) {
return "八";
} else if (nun.equals("9")) {
return "九";
} else if (nun.equals("10")) {
return "十";
} else if (nun.equals("11")) {
return "十一";
} else if (nun.equals("12")) {
return "十二";
}
return "";
}
}
执行main方法得到结果如下 (可以更改为将时分秒转换为汉字) :