Java 爪哇一年中最糟糕的一周

Java 爪哇一年中最糟糕的一周,java,gregorian-calendar,Java,Gregorian Calendar,我对一年中正确的一周有问题。我有一个JTextField,在这里我以DD.MM.YYYY格式输入日期 private void buttonAddCulturalActionActionPerformed(java.awt.event.ActionEvent evt) { String date = textfieldDateOfEvent.getText(); if (!isCorre

我对一年中正确的一周有问题。我有一个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");
    }
}
在这里,我在构造器中设置了文化行为:

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);
}
当我输入日期2013年10月10日时,它将返回第45周,但40是正确的。我来自捷克共和国

谢谢你的建议

这就是问题所在:

GregorianCalendar enteredDate = new GregorianCalendar(year, month, day);
这将被称为2013年10月10日,也就是11月,而不是10月

从:

月-用于在日历中设置月日历字段的值。月份值以0为基础。e、 g.一月份为0

另外两条建议:

如果必须只使用Java库,请使用SimpleDataFormat执行解析,而不是自己执行 如果可能,请改用,这是一个更合理的API tl;博士 41

定义“周” 一周可以用多种方式定义

由于你没有定义你的一周,我将使用一个标准

避免遗留类 Jon Skeet的问题和问题都使用了过时的日期时间类。现代方法使用java.time类

完整地分析输入字符串。在实际工作中,如果输入不是预期的格式,则从解析尝试捕获异常

String input = "10.10.2013" ; 
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd.MM.uuuu" , Locale.US ) ;
LocalDate ld = LocalDate.parse( input , f ) ;
等场 使用该类提取基于年份的周数

int week = ld.get( IsoFields.WEEK_OF_WEEK_BASED_YEAR ) ;
41

额外的三十年一周

也可以考虑将库添加到项目中。它提供了一个方便的类来表示ISO8601周

YearWeek yw = YearWeek.from( ld ) ;
yw.toString:2013-W41


谢谢你的回复。我再一次发现了完全不同类型的错误。我尝试使用SimpleDataFormat。非常感谢!我还有一个关于SimpleDateFormat的问题。我现在使用这个方法来控制正确的日期格式:例如,当我以错误的格式dd.MM.yy 10.10.20输入日期时,程序错误。为什么?@avalagne:你说的错误是什么意思?如果你的意思是它抛出ParseException,那么如果你给它不可解析的数据,这就是它要做的。您的代码吞没了ParseException这一事实意味着您正在丢失该信息。不要那样做。不要在没有日志记录的情况下吞咽这样的异常。错误是StringIndexOutOfBoundsException。那么我应该如何处理输入的日期呢?我需要的是,当用户输入错误的日期时,程序不会掉下来,并通过另一次尝试输入调用它。这可以通过使用exception或not来实现?我将date的子字符串保存为year,预计为4个字符,但当用户只输入两个字符时,程序会出现StringIndexOutOfBoundsException错误。我不知道还能怎么治疗。
YearWeek yw = YearWeek.from( ld ) ;