[英]Bad week of year in Java
I have problem with correct week of year. 我对一年中的正确一周有疑问。 I have JTextField, where I enter date in format DD.MM.YYYY.
我有JTextField,以DD.MM.YYYY格式输入日期。
private void buttonAddCulturalActionActionPerformed(java.awt.event.ActionEvent evt) {
String date = textfieldDateOfEvent.getText();
if (!isCorrectFormatDate(date)) {
textareaExtract.append("Wrong date format!\n");
return;
}
int day = Integer.parseInt(date.substring(0, 2));
int month = Integer.parseInt(date.substring(3, 5));
int year = Integer.parseInt(date.substring(6, 10));
GregorianCalendar enteredDate = new GregorianCalendar(year, month, day);
String actionName = textfieldActionName.getText();
String placeActionName = textfieldPlaceActionName.getText();
int startHourAction = Integer.parseInt(textfieldStartHourAction.getText());
if (isAllEntered(actionName, enteredDate, placeActionName, startHourAction)) {
CulturalAction newAction = new CulturalAction(actionName, enteredDate,
placeActionName, startHourAction);
// culturalActions is priority queue
culturalActions.add(newAction);
extractAction(newAction);
} else {
textareaExtract.append("You need entered all parameters!\n");
}
}
Here I set the cultural action (in constructor): 在这里,我设置了文化行为(在构造函数中):
public CulturalAction(String nameAction, GregorianCalendar dateAction, String placeAction, int startHourAction) {
this.nameAction = nameAction;
this.placeAction = placeAction;
this.startHourAction = startHourAction;
// I have private attribute numberOfWeek in class CulturalAction
cisloTydne = dateAction.get(GregorianCalendar.WEEK_OF_YEAR);
}
When I entered date 10.10.2013 and it would return the week number 45, but 40 is properly. 当我输入日期10.10.2013时,它将返回第45周,但40是正确的。 I'm from Czech Republic.
我来自捷克共和国。
Thank you for advices! 谢谢您的指教!
This is the problem: 这就是问题:
GregorianCalendar enteredDate = new GregorianCalendar(year, month, day);
That's going to be calling new GregorianCalendar(2013, 10, 10)
- which means November, not October. 这将被称为
new GregorianCalendar(2013, 10, 10)
-这意味着11月,而不是10月。
From the GregorianCalendar
constructor docs : 从
GregorianCalendar
构造函数docs :
month
- the value used to set the MONTH calendar field in the calendar.month
-用于设置日历中的MONTH日历字段的值。 Month value is 0-based.月值从0开始。 eg, 0 for January.
例如,一月为0。
Two other bits of advice: 其他两点建议:
LocalDate.parse( "10.10.2013" , DateTimeFormatter.ofPattern( "dd.MM.uuuu" ) )
.get( IsoFields.WEEK_OF_WEEK_BASED_YEAR )
41
41
A week can be defined in various ways. 可以通过多种方式定义一周。
As you do not define your week, I will go with a standard ISO 8601 week . 由于您没有定义星期,因此我将采用标准的ISO 8601星期 。
Both the Question and the correct Answer by Jon Skeet use old outdated date-time classes. 乔恩·斯凯特(Jon Skeet)的问题和正确答案都使用了过时的旧日期时间类。 The modern approach uses the java.time classes instead.
现代方法改为使用java.time类。
Parse the input string in its entirety. 完整解析输入字符串。 In real work, catch the exception from the parse attempt in case the input is not in expected format.
在实际工作中,如果输入的格式不是预期的格式,请从解析尝试中捕获异常。
String input = "10.10.2013" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd.MM.uuuu" , Locale.US ) ;
LocalDate ld = LocalDate.parse( input , f ) ;
IsoFields
Use the IsoFields
class to extract the week of week-based-year number. 使用
IsoFields
类提取基于星期的年份数字。
int week = ld.get( IsoFields.WEEK_OF_WEEK_BASED_YEAR ) ;
41
41
YearWeek
YearWeek
Also, consider adding the ThreeTen-Extra library to your project. 另外,请考虑将ThreeTen-Extra库添加到您的项目中。 It offers a handy
YearWeek
class to represent ISO 8601 weeks. 它提供了一个方便的
YearWeek
类来代表ISO 8601周。
YearWeek yw = YearWeek.from( ld ) ;
yw.toString(): 2013-W41
yw.toString():2013-W41
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.