Buggy code:
} else { String lmWeek = ContextAnalyzer . getLastMentionedX ( linearDates , i , "week" , language ) ; if ( lmWeek . equals ( "" ) ) { valueNew = valueNew . replace ( checkUndef , "XXXX-WXX" ) ; } else { valueNew = valueNew . replace ( checkUndef , DateCalculator . getXNextWeek ( lmWeek , 1 , language ) ) ; } } } else if ( ambigString . startsWith ( "UNDEF-last-quarter" ) ) { String checkUndef = "UNDEF-last-quarter" ; if ( ( ( documentTypeNews || documentTypeColloquial ) || documentTypeScientific ) && dctAvailable ) { if ( dctQuarter . equals ( "Q1" ) ) { valueNew = valueNew . replace ( checkUndef , ( ( dctYear - 1 ) + "-Q4" ) ) ; } else { int newQuarter = ( Integer . parseInt ( dctQuarter . substring ( 1 , 2 ) ) ) - 1 ; valueNew = valueNew . replace ( checkUndef , ( ( dctYear + "-Q" ) + newQuarter ) ) ; } } else { String lmQuarter = ContextAnalyzer . getLastMentionedX ( linearDates , i , "quarter" , language ) ; if ( lmQuarter . equals ( "" ) ) { valueNew = valueNew . replace ( checkUndef , "XXXX-QX" ) ; } else { int lmQuarterOnly = Integer . parseInt ( lmQuarter . substring ( 6 , 7 ) ) ; int lmYearOnly = Integer . parseInt ( lmQuarter . substring ( 0 , 4 ) ) ; if ( lmQuarterOnly == 1 ) { valueNew = valueNew . replace ( checkUndef , ( ( lmYearOnly - 1 ) + "-Q4" ) ) ; } else { int newQuarter = lmQuarterOnly - 1 ; valueNew = valueNew . replace ( checkUndef , ( ( lmYearOnly + "-Q" ) + newQuarter ) ) ; } } } } else if ( ambigString . startsWith ( "UNDEF-this-quarter" ) ) { String checkUndef = "UNDEF-this-quarter" ; if ( ( ( documentTypeNews || documentTypeColloquial ) || documentTypeScientific ) && dctAvailable ) { valueNew = valueNew . replace ( checkUndef , ( ( dctYear + "-" ) + dctQuarter ) ) ; } else { String lmQuarter = ContextAnalyzer . getLastMentionedX ( linearDates , i , "quarter" , language ) ; if ( lmQuarter . equals ( "" ) ) { valueNew = valueNew . replace ( checkUndef , "XXXX-QX" ) ; } else { valueNew = valueNew . replace ( checkUndef , lmQuarter ) ; } } } else if ( ambigString . startsWith ( "UNDEF-next-quarter" ) ) { String checkUndef = "UNDEF-next-quarter" ; if ( ( ( documentTypeNews || documentTypeColloquial ) || documentTypeScientific ) && dctAvailable ) { if ( dctQuarter . equals ( "Q4" ) ) { valueNew = valueNew . replace ( checkUndef , ( ( dctYear + 1 ) + "-Q1" ) ) ; } else { int newQuarter = ( Integer . parseInt ( dctQuarter . substring ( 1 , 2 ) ) ) + 1 ; valueNew = valueNew . replace ( checkUndef , ( ( dctYear + "-Q" ) + newQuarter ) ) ; } } else { String lmQuarter = ContextAnalyzer . getLastMentionedX ( linearDates , i , "quarter" , language ) ; if ( lmQuarter . equals ( "" ) ) { valueNew = valueNew . replace ( checkUndef , "XXXX-QX" ) ; } else { int lmQuarterOnly = Integer . parseInt ( lmQuarter . substring ( 6 , 7 ) ) ; int lmYearOnly = Integer . parseInt ( lmQuarter . substring ( 0 , 4 ) ) ; if ( lmQuarterOnly == 4 ) { valueNew = valueNew . replace ( checkUndef , ( ( lmYearOnly + 1 ) + "-Q1" ) ) ; } else { int newQuarter = lmQuarterOnly + 1 ; <START_BUG> valueNew = valueNew . replace ( checkUndef , ( ( dctYear + "-Q" ) + newQuarter ) ) ; <END_BUG> } } } } else if ( ambigString . matches ( "UNDEF-(last|this|next)-(january|february|march|april|may|june|july|august|september|october|november|december).*" ) ) { for ( MatchResult mr : Toolbox . findMatches ( Pattern . compile ( "(UNDEF-(last|this|next)-(january|february|march|april|may|june|july|august|september|october|november|december))(.*)" ) , ambigString ) ) { String rest = mr . group ( 4 ) ; int day = 0 ; for ( MatchResult mr_rest : Toolbox . findMatches ( Pattern . compile ( "-([0-9][0-9])" ) , rest ) ) { day = Integer . parseInt ( mr_rest . group ( 1 ) ) ; } String checkUndef = mr . group ( 1 ) ; String ltn = mr . group ( 2 ) ; String newMonth = norm . getFromNormMonthName ( mr . group ( 3 ) ) ; int newMonthInt = Integer . parseInt ( newMonth ) ; if ( ltn . equals ( "last" ) ) { if ( ( ( documentTypeNews || documentTypeColloquial ) || documentTypeScientific ) && dctAvailable ) { if ( ( dctMonth == newMonthInt ) && ( ! ( day == 0 ) ) ) { if ( dctDay > day ) { valueNew = valueNew . replace ( checkUndef , ( ( dctYear + "-" ) + newMonth ) ) ; } else { valueNew = valueNew . replace ( checkUndef , ( ( ( dctYear - 1 ) + "-" ) + newMonth ) ) ; } } else if ( dctMonth <= newMonthInt ) { valueNew = valueNew . replace ( checkUndef , ( ( ( dctYear - 1 ) + "-" ) + newMonth ) ) ; } else { valueNew = valueNew . replace ( checkUndef , ( ( dctYear + "-" ) + newMonth ) ) ; } } else { String lmMonth = ContextAnalyzer . getLastMentionedX ( linearDates , i , "month-with-details" , language ) ; if ( lmMonth . equals ( "" ) ) { valueNew = valueNew . replace ( checkUndef , "XXXX-XX" ) ; } else { int lmMonthInt = Integer . parseInt ( lmMonth . substring ( 5 , 7 ) ) ; int lmDayInt = 0 ;

Fix:
valueNew = valueNew . replace ( checkUndef , ( ( lmYearOnly + "-Q" ) + newQuarter ) ) ;