错误的“一年中的星期”在安卓中
从日期返回的“一年中的周”数是错误的。
这是我的代码:
Calendar c = Calendar.getInstance();
c.setTime(my_date);
int num_week = c.get(Calendar.WEEK_OF_YEAR);
如果 my_date (日期类型)是 01/01/2011,我认为“一年中的一周”是 1。但它返回 52。
我尝试用这些方法进行测试,但没有得到任何结果:
c.setFirstDayOfWeek(6);
c.setMinimalDaysInFirstWeek(1)
如果有趣的是,我来自西班牙,我们的一周从星期一开始。
我需要做些什么才能获得正确的结果吗?
谢谢!
The number of "week of year" returned from a Date is wrong.
This is my code:
Calendar c = Calendar.getInstance();
c.setTime(my_date);
int num_week = c.get(Calendar.WEEK_OF_YEAR);
If my_date (type Date) is 01/01/2011, I supposed that "week of year" is 1. But it returned 52.
I try to test with these methods but I don't obtain anything:
c.setFirstDayOfWeek(6);
c.setMinimalDaysInFirstWeek(1)
If It's interesting, I'm from Spain, and our week begin on Monday.
Have I to do anything for obtain right results?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能是 Android/Harmony 特定的。例如,这对我使用桌面 Java 有效:
您能确认完全相同的代码(模数日志选项)在 Android 上记录 52 两次吗?
This may be Android/Harmony-specific. For example, this works for me with desktop Java:
Can you confirm that the exact same code (modulo logging options) logs 52 twice on Android?
这里可以查看oracle的参考
https:// docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html
我已经放置了一个快速解决方案来查找当天的周数。您可以按照自己的方式进行更改和优化。也可以根据您方便的 GMT 值进行设置
最后只需调用方法 getWeeksOfMonth();
Here you can view the reference by oracle
https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html
And I have placed a quick solution to find the week count of current day. You can alter and optimize as your way. Also set according to your convenient GMT value
At the end just call the method getWeeksOfMonth();