java⽇期中⽂_JAVA的时间类型转换为中⽂⼤写⽅法
今天做项⽬,需要数字类型的时间转换为中⽂类型的时间,这是⼀个java数字时间类型转中⽂类型的⽅法,适⽤于⽂档使⽤等等。和对时间有特殊要求的项⽬。通俗易懂很容易看懂。不多说了直接上代码
⾸先⽤时间函数获取想要的格式的时间SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Demo {
private static String getChineseDate(String riqi) throws Exception {
String[] newsdate = new String[8];
for (int i = 0; i
int k = Integer.String(riqi.charAt(i)));
switch (k) {
case 0:
newsdate[i] = "〇";
break;
case 1:
newsdate[i] = "⼀";
break;
case 2:
newsdate[i] = "⼆";
break;
case 3:
newsdate[i] = "三";
break;
case 4:
newsdate[i] = "四";
break;
case 5:
newsdate[i] = "五";
break;
case 6:
newsdate[i] = "六";
break;
case 7:
newsdate[i] = "七"; break;
case 8:
newsdate[i] = "⼋"; break;
case 9:
newsdate[i] = "九"; break;
}
}
// 加⼊年⽉⽇
List s1 = new ArrayList(); for (int i = 0; i
if (i
s1.add(newsdate[i]);
} else if (i == 4) {
s1.add("年");
s1.add(newsdate[i]);
} else if (i == 5) {
s1.add(newsdate[i]);
} else if (i == 6) {
s1.add("⽉");
s1.add(newsdate[i]);
} else if (i == 7) {
s1.add(newsdate[i]);
s1.add("⽇");
}
}
String newstr = "";
for (String s : s1) { newstr += s;
}
/*
* 截取⽉份、⽇期
*/
int i = newstr.indexOf("年");
int j = newstr.indexOf("⽉");
String month = newstr.substring(i + 1, j);
String day = newstr.substring(j + 1, newstr.length() - 1); /*
* 处理⽉份
*/
String str1 = month.substring(0, 1);
String str2 = month.substring(1);
String newmonth = "";
if ("〇".equals(str1)) {
newmonth = str2;
} else if ("⼀".equals(str1) && "〇".equals(str2)) { newmonth = "⼗";
} else if ("⼀".equals(str1) && !"〇".equals(str2)) { newmonth = "⼗" + str2;
}
/*
* 处理⽇期
*/
String st1 = day.substring(0, 1);
String st2 = day.substring(1);
String newday = "";
if ("〇".equals(st1)) {
newday = st2;
} else if ("⼀".equals(st1) && "〇".equals(st2)) {
newday = "⼗";
} else if ("⼀".equals(st1) && !"〇".equals(st2)) {
newday = "⼗" + st2;
} else if ("⼆".equals(st1) && "〇".equals(st2)) {
newday = st1 + "⼗";
} else if ("⼆".equals(st1) && !"〇".equals(st2)) {
newday = st1 + "⼗" + st2;
} else if ("三".equals(st1) && "〇".equals(st2)) {
newday = st1 + "⼗";
} else if ("三".equals(st1) && !"〇".equals(st2)) {
newday = st1 + "⼗" + st2;
}
java时间日期格式转换
String newstring = newstr.substring(0, i) + "年" + newmonth + "⽉" + newday + "⽇"; return newstring;
}
public static void main(String[] args) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String riqi = sdf.format(new Date());
System.out.println(getChineseDate(riqi));
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出⽰例:⼆〇⼀六年九⽉⼆⼗六⽇

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。