Java Calendar使用Calendar.DAY_OF_WEEK获取特定日期的第一个和最后一个日期 [英] Java Calendar using Calendar.DAY_OF_WEEK to get the first and the last dates for a particular date

查看:697
本文介绍了Java Calendar使用Calendar.DAY_OF_WEEK获取特定日期的第一个和最后一个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中有一个代码工作突然有时,它关于使用java日历对象通过Calendar.DAY_OF_WEEK获得一个星期的时间间隔。
代码检查星期一是星期开始,星期日是星期结束,如:
fromCal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
toCal.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
toCal设置为最后一个星期日,而不是星期日。
除了这种硬编码之外,还有其他替代方法。

In my application there lies a code which works abruptly sometimes, its about getting a week interval using the java calendar object through Calendar.DAY_OF_WEEK. The code checked for monday as start of week and sunday as end of week like: fromCal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); toCal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); the toCal is set as the last sunday instead of coming sunday. Is there any alternate way to do this other then this kind of hard coding.

提前帮助。

感谢,

Vaibhav

推荐答案

问题是在区域设置。在英语(美国),星期日是一周的第一天。
检查此代码:

The issue is in locale. In English(US), Sunday is the first day of the week. Check this code:

Calendar cal = Calendar.getInstance(Locale.ENGLISH);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    System.out.println("FirstDayOfWeek="+cal.getFirstDayOfWeek());
    System.out.println(cal.getTime().toString());
    cal = Calendar.getInstance(Locale.FRANCE);
    System.out.println("FirstDayOfWeek="+cal.getFirstDayOfWeek());
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    System.out.println(cal.getTime().toString());

这篇关于Java Calendar使用Calendar.DAY_OF_WEEK获取特定日期的第一个和最后一个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆