java--获取当前时间(Calendar类)

用Calendar类来获取当前的时间

代码:

import java.util.Calendar;
public class CalendarDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Calendar c=Calendar.getInstance();
        int year=c.get(Calendar.YEAR);
        int month=c.get(Calendar.MONTH);
        int day=c.get(Calendar.DAY_OF_MONTH);
        int week=c.get(Calendar.DAY_OF_WEEK);
        int hour=c.get(Calendar.HOUR_OF_DAY);
        int min=c.get(Calendar.MINUTE);
        int second=c.get(Calendar.SECOND);
        System.out.println(year+"年"+(month+1)+"月"+day+"日"+",周"+changeWeek(week)+","+hour+":"+min+":"+second);
	}
    private static String changeWeek(int week){
    	String str="";
    	switch(week)
    	{
    	case 1:
    		str="日";
    		break;
    	case 2:
    		str="一";
    		break;
    	case 3:
    		str="二";
    		break;
    	case 4:
    		str="三";
    		break;
    	case 5:
    		str="四";
    		break;
    	case 6:
    		str="五";
    		break;
    	case 7:
    		str="六";
    		break;
    	}
    	return str;
    }
}

运行结果:


你可能感兴趣的:(编程练习)