今天在学习java是学习到一种查表法进行数据形式的转换,感觉蛮有用的,分享一下
首先看看程序代码吧!
import java.util.Calendar;
public class CalendarDemo {
public static void main(String[] args) {
CalendarDemo calendarDemo = new CalendarDemo();
calendarDemo.showDate();
}
public void showDate(){
Calendar calendar = Calendar.getInstance();
String months[] = {"一月", "二月", "三月", "四月",
"五月", "六月", "七月", "八月",
"九月", "十月", "十一月", "十二月", };
String weeks[] = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
int mIndex = calendar.get(calendar.MONTH);
int wIndex = calendar.get(calendar.DAY_OF_WEEK);
System.out.println(calendar.get(calendar.YEAR) + "年" +
(calendar.get(calendar.MONTH)+1) + "月" +
calendar.get(calendar.DAY_OF_MONTH)+ "日"
+ "星期" + (calendar.get(calendar.DAY_OF_WEEK)-1));
System.out.println(calendar.get(calendar.YEAR) + "年" +
months[mIndex] +
calendar.get(calendar.DAY_OF_MONTH)+ "日"
+ weeks[wIndex]);
}
}
输出结果:
2014年6月27日星期5
2014年六月27日星期五
利用查表法有效结果日期输出月份和星期时需要进行加1和减一问题