Question

I have problem with correct week of year. I have JTextField, where I enter date in format 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. I'm from Czech Republic.

Thank you for advices!

Was it helpful?

Solution

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.

From the GregorianCalendar constructor docs:

month - the value used to set the MONTH calendar field in the calendar. Month value is 0-based. e.g., 0 for January.

Two other bits of advice:

  • If you have to use just the Java libraries, use SimpleDateFormat to perform the parsing, rather than doing it yourself
  • If you possibly can, use Joda Time instead, which is a much more sensible API

OTHER TIPS

tl;dr

LocalDate.parse( "10.10.2013" , DateTimeFormatter.ofPattern( "dd.MM.uuuu" ) )
         .get( IsoFields.WEEK_OF_WEEK_BASED_YEAR )

41

Define 'week'

A week can be defined in various ways.

As you do not define your week, I will go with a standard ISO 8601 week.

Avoid legacy classes

Both the Question and the correct Answer by Jon Skeet use old outdated date-time classes. The modern approach uses the java.time classes instead.

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.

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

41

ThreeTen-Extra YearWeek

Also, consider adding the ThreeTen-Extra library to your project. It offers a handy YearWeek class to represent ISO 8601 weeks.

YearWeek yw = YearWeek.from( ld ) ;

yw.toString(): 2013-W41

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top