系統時間、星期、年月日

獲取系統的當前時間

long time = System.currentTimeMillis();

以不同的格式顯示
(1) 2015-11-11 11:08:34
(2) 2015/01/06 06-36-23
……

SimpleDateFormat sdff = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  

Date date = new Date(System.currentTimeMillis());  

String str_date = sdf.format(date);  

通過Calendar類獲取年、月、日、星期(int類型1~7)


Calendar calendar = Calendar.getInstance();  

int year = calendar.get(Calendar.YEAR);  

//從0開始,一月得到的month=0 
int month = calendar.get(Calendar.MONTH);

int day = calendar.get(Calendar.DAY_OF_MONTH);  

int weekk = calendar.get(Calendar.DAY_OF_WEEK);  

獲取當前是星期幾

Date date = new Date(System.currentTimeMillis());  

Calendar calendar = Calendar.getInstance();  

calendar.setTime(date);  

String week = "周";  

if(calendar.get(Calendar.DAY_OF_WEEK) == 1)  
{  
    week +="日";  
}else if(calendar.get(Calendar.DAY_OF_WEEK) == 2)  
{  
    week +="一";  
}else if(calendar.get(Calendar.DAY_OF_WEEK) == 3)  
{  
    week +="二";  
}else if(calendar.get(Calendar.DAY_OF_WEEK) == 4)  
{  
    week +="三";  
}else if(calendar.get(Calendar.DAY_OF_WEEK) == 5)  
{  
    week +="四";  
}else if(calendar.get(Calendar.DAY_OF_WEEK) == 6)  
{  
    week +="五";  
}else if(calendar.get(Calendar.DAY_OF_WEEK) == 7)  
{  
    week +="六";  
}   

//最終獲取的格式是:週六
發佈了33 篇原創文章 · 獲贊 10 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章