<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>
很多朋友都想知道java怎麼獲取當前星期?下面就一起來了解一下吧~
java可以採用下面兩種方式取得當前星期:
a、使用Calendar類
//根據日期取得星期幾public static String getWeek(Date date){String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};Calendar cal = Calendar.getInstance();cal.setTime(date);int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;if(week_index<0){week_index = 0;} return weeks[week_index];}
b、使用SimpleDateFormat類
//根據日期取得星期幾public static String getWeek(Date date){ SimpleDateFormat sdf = new SimpleDateFormat("EEEE");String week = sdf.format(date);return week;}
注:格式化字符串存在區分大小寫
對於創建SimpleDateFormat傳入的參數:EEEE代表星期,如“星期四”;MMMM代表中文月份,如“十一月”;MM代表月份,如“11”;
yyyy代表年份,如“2010”;dd代表天,如“25”
以上就是小編今天的分享,希望能夠幫到大家。