`
bluenemo
  • 浏览: 176106 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Calendar中的DAY_OF_WEEK

阅读更多

java.util.Date类中的很多方法已经过时

 

原作者

http://blog.sina.com.cn/s/blog_45c06e600100pm77.html

 Java中对日期的处理需要用到Calendar类,其中有几个方法在使用时需要新手注意。

1. 在获取月份时,Calendar.MONTH + 1 的原因
Java中的月份遵循了罗马历中的规则:当时一年中的月份数量是不固定的,第一个月是JANUARY。而Java中Calendar.MONTH返回的数值其实是当前月距离第一个月有多少个月份的数值,JANUARY在Java中返回“0”,所以我们需要+1。

2. 在获取星期几 Calendar.DAY_OF_WEEK – 1 的原因
Java中Calendar.DAY_OF_WEEK其实表示:一周中的第几天,所以他会受到 第一天是星期几 的影响。
有些地区以星期日作为一周的第一天,而有些地区以星期一作为一周的第一天,这2种情况是需要区分的。
看下表的返回值

星期日为一周的第一天 SUN MON TUE WED THU FRI SAT
DAY_OF_WEEK返回值 1 2 3 4 5 6 7
星期一为一周的第一天 MON TUE WED THU FRI SAT SUN
DAY_OF_WEEK返回值 1 2 3 4 5 6 7

所以Calendar.DAY_OF_WEEK需要根据本地化设置的不同而确定是否需要 “-1”
Java中设置不同地区的输出可以使用 Locale.setDefault(Locale.地区名) 来实现。

3. 获取日期时 Calendar.DAY_OF_MONTH 不需要特殊的操作,他直接返回一个月中的第几天

分享到:
评论

相关推荐

    java 获取时间 本周本年本月第一天最后一天

    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday System.out.println(c.getTime()); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday //本周第一天,以星期日开始 。。。...

    java时间处理工具类--CalendarUtil(java源码)

    int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 now.set(Calendar.DATE, first_day_of_week); return now.getTime(); } /** * 获得所在星期的最后一天 */ public static ...

    SYS_CALENDAR_DATE.sql

    `WEEK_OF_YEAR` int(10) NOT NULL COMMENT '获得指定日期是所在年份的第几周', `WEEK_OF_MONTH` int(10) NOT NULL COMMENT '获得指定日期是所在月份的第几周', `DAY_OF_MONTH` int(10) NOT NULL COMMENT '获得...

    周日历选择插件jcalendar_week

    $(obj).addClass("calendar_day_act").siblings().removeClass("calendar_day_act"); } }); 点击上方显示当前年份和周的DOM部分可选择并跳转到指定年份和周。 插件提供的方法: //获取周第一天方法weekfirstdate...

    java时间处理工具类--DateUtils

    cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); cal.add(Calendar.DAY_OF_MONTH, 6); return cal.getTime(); } /** * 获取指定时间范围的第一天 * * @param dateRangeType * 时间范围类型 * ...

    Day-of-year-and-week.c.zip_This Is the Day

    a program to prompt for the Grogorian calendar day, (i) the total number of days January 1st, year 1 to this day, (ii) the total number of days January 1st of the year to the day, and (iii) the day of...

    calendar.java

    通过Calendar对象的get(Calendar.DAY_OF_WEEK)方法可以获得Calendar对象为星期几(星期天为1,星期六为7)。 注意: Calendar中的月份从0开始(1月为0,12月为11),日期从1开始(1日为1,28日为28); 12生肖的...

    Determining Day of Week

    In the Gregorian calendar, which is widely used now, the year AD 1 is directly preceded by the year 1 BC; a year 0 does not exist in this system. In contrast, astronomical reckoning indeed uses a year...

    java时间格式大全(算法源码)

    java时间格式大全(算法源码) java,date,时间,时间格式,算法源码 * * @return */ public static String getTimeShort() { SimpleDateFormat formatter = new ... if (cal1.get(Calendar.WEEK_OF_YEAR) == ...

    如何打印2019年每个月的第一个星期天的日期

    但Calendar类Calendar.WEEK_OF_MONTH属性,即这一周在一个月中属于第几周这一属性设计的很不合我们的习惯,他是以周六作为一周的结束,比如19年的6月1日,恰好在周六,那么第一周就只有这一天,而作为周日的2号就在第...

    jar编写的日程管理系统

    int i=calendar.get(Calendar.DAY_OF_WEEK)-1; if(i==0) i=7; return i; } static boolean in_term(Date date){ int[] term=WeekInfStore.get_term_date(); Calendar c1=Calendar.getInstance()...

    JSP万年历 JSP万年历

    int firstIndex = thisMonth.get(Calendar.DAY_OF_WEEK) - 1; int maxIndex = thisMonth.getActualMaximum(Calendar.DAY_OF_MONTH); for (int i = 0; i ; i++) { days[firstIndex + i] = String.valueOf(i + 1);...

    时间控件,个人喜欢用的js时间控件

    // table of short day names var ar = new Array(); for (var i = 8; i > 0;) { ar[--i] = Calendar._DN[i].substr(0, 3); } Calendar._DN3 = ar; // table of short month names ar = new Array(); for ...

    swift实现的Week & Day 日历控件.zip

    swift实现的Week & Day 日历控件.zip,Calendar Week & Day View in iOS Swift

    Java电子日历设计.doc

    Java程序设计 课程设计报告 设计题目:电子日历设计 班级: 学号: 姓名: 1. 需求分析 设计并实现一个电子日历,当用户在下拉列表中选择年份... int week=date.get(Calendar.DAY_OF_WEEK)-1; int day=0; if(month==1

    Android代码-Android-Week-View

    Android Week View is an android library to display calendars (week view or day view) within the app. It supports custom styling. Features Week view calendar Day view calendar Custom styling ...

    界面标准的JAVA日历记事本

    在得到日期的同时判断DAY OF WEEK星期几,在窗体上显示 5为各个按钮及标签添加监听 通过ActionPerform MousePressed实现其动作 6 保存日志 显示日志及删除日志,总过创建哈希表来进行文件的读写

    D3-Calendar-Heat-Map

    所需的数据格式-[{“ DATEID”:“ 20210401”,“ DB_DATE”:“ 2021-04-01”,“ DAY”:“ 1”,“ DAY_NAME_SHORT”:“星期四”,“ WEEKDAYNO”:“ 4”, “ WEEKNO”:“ 13”,“ WEEK_OF_MONTH”:“ 0...

    dhtmlxScheduler_v30_120111.zip

    The events can be displayed in different views (Day, Week, Month, Year, Agenda, etc.) Advanced drag-and-drop functionality allows users to change date and time of an event by easily moving or ...

    DS1306 Serial Alarm Real Time Clock (RTC)

    day of the week 1 Hz and 32.768 kHz clock outputs Serial interface supports Motorola Serial Peripheral Interface (SPI) serial data ports or standard 3-wire interface Burst Mode for reading/...

Global site tag (gtag.js) - Google Analytics