您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

java中获取日期是星期几的两种方法

2024/4/25 21:15:11发布6次查看
java中取得指定日期是星期几可以采用下面两种方式取得日期是星期几:(推荐:java视频教程)
1、使用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];}
2、使用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”
更多java知识请关注java基础教程栏目。
以上就是java中获取日期是星期几的两种方法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录