发布网友 发布时间:2022-04-29 21:21
共2个回答
好二三四 时间:2022-06-10 17:14
<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”
以上就是小编今天的分享,希望能够帮到大家。
热心网友 时间:2022-06-10 14:22
1、使用Calendar类