简体   繁体   English

Calendar.WEEK_OF_YEAR返回错误的值?

[英]Calendar.WEEK_OF_YEAR is returning a wrong value?

I tried getting week of the year through below code but it always returns "3", I even set the timezone to GMT but still returning the same value( "3" ). 我尝试通过下面的代码获取一年中的星期,但它始终返回“ 3”,甚至将时区设置为GMT,但仍返回相同的值( "3" )。 Please help me out over here 请帮帮我

DateFormat formatter = new SimpleDateFormat("dd-mm-yyyy");
Date date = formatter.parse("14-10-2014")
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR));

You're setting the minutes not the month . 您设置的是分钟而不是月份 It sounds like it's parsing this as 2014-01-14T00:10:00, and that January 14th 2014 is in the third week. 听起来好像是将其解析为2014-01-14T00:10:00,而2014年1月14日是第三周。 You want MM for months, not mm . 您需要MM几个月而不是mm

(I'd also encourage you to use yyyy-MM-dd if you get a chance to change the format entirely - that's ISO-8601 compliant...) (如果您有机会完全更改格式,则我也鼓励您使用yyyy-MM-dd -dd-这符合ISO-8601标准...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM