0

This is actually a duplicate of Y returns 2012 while y returns 2011 in SimpleDateFormat I'll take the ding for duplicate on this one not the one listed below. I read the answer on that and I still think there is an error there. The first day of the week in my system is Sunday so by the description from the java.doc This should have been the 53rd week of 2017 not the 1st week of 2018, if I was going to get an error January 1st through the 6th should have returned 2017.

This is not a duplicate of Calendar return incorrect date, the calendar is working fine. see additional information below I was trying to simplify my code for display purposes at EOD as I was hopeing to get out of the door

When using SimpleDateFormat.Format to convert a Calendar date of 12/31/2017 (2017,11,31,0,0) (Sunday first day of the week, last week of the year) SimpleDateFormat returns 12/31/2018. I have tried this with java 6,7,8 and get the same results, I tried it on two different versions of eclipse I tried it on another developers computer all this the same result. This is pulled from my actual junit code.

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class SimpleDateTest2 {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Calendar t_calendar =   Calendar.getInstance();
    Calendar m_calc_calendar =  Calendar.getInstance();
    int rc=0;
    int testDays=20;
    Date rDate;
    SimpleDateFormat ssaDateFormat= new SimpleDateFormat("YYMMdd");  
    SimpleDateFormat logDateFormat= new SimpleDateFormat("E MM/dd/YYYY");
    ArrayList<Date> testDates = new ArrayList<Date>();
    t_calendar.set(2017,11,20,0,0);  // start date
    //t_calendar.set(2018,11,20,0,0);  // start date
    t_calendar.setLenient(false);
    // builds 2600 dates for test run reason for Date (24 bytes) vs Calendar (423 bytes)
    for(int days=0; days<testDays;days++){
        t_calendar.add(Calendar.DATE, 1);
        testDates.add(t_calendar.getTime());
    }

    for(Date testDate: testDates){
     String test = testDate.toString();
        System.out.println("toString: " + test);
        t_calendar.setTimeInMillis(testDate.getTime());  
        test = ssaDateFormat.format(t_calendar.getTime());
        System.out.println(" Cal.year " + t_calendar.get(Calendar.YEAR) +   " Cal.month " + t_calendar.get(Calendar.MONTH) +   " Cal.day " + t_calendar.get(Calendar.DATE) +   " Cal.week " + t_calendar.get(Calendar.WEEK_OF_YEAR) );
        System.out.println("ssa conversion: " + test); 
        test = logDateFormat.format(t_calendar.getTime());
        System.out.println("log conversion: " + test);
        System.out.println(t_calendar.toString());
        System.out.println("");
        // call to method under test
        //rDate=rsl.calculateSSATransDate(ssaDateFormat.format(testDate),new BigDecimal(1.0));
        // result validation.

    } 

}

Test Results

toString: Sat Dec 29 00:00:53 PST 2018 <-- input date
Cal.year 2018 Cal.month 11 Cal.day 29 Cal.week 52
ssa conversion: 181229
log conversion: Sat 12/29/2018 <-- result is good
java.util.GregorianCalendar[time=1546070453716,areFieldsSet=true,areAllFieldsSet=true,lenient=false,zone=sun.util.calendar.ZoneInfo[id="America/Los_Angeles",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=11,WEEK_OF_YEAR=52,WEEK_OF_MONTH=5,DAY_OF_MONTH=29,DAY_OF_YEAR=363,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=5,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=53,MILLISECOND=716,ZONE_OFFSET=-28800000,DST_OFFSET=0]

toString: Sun Dec 30 00:00:53 PST 2018 <-- Input date
Cal.year 2018 Cal.month 11 Cal.day 30 Cal.week 1 
ssa conversion: 191230 <-- Result is bad
log conversion: Sun 12/30/2019
java.util.GregorianCalendar[time=1546156853716,areFieldsSet=true,areAllFieldsSet=true,lenient=false,zone=sun.util.calendar.ZoneInfo[id="America/Los_Angeles",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=11,WEEK_OF_YEAR=1,WEEK_OF_MONTH=6,DAY_OF_MONTH=30,DAY_OF_YEAR=364,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=5,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=53,MILLISECOND=716,ZONE_OFFSET=-28800000,DST_OFFSET=0]

toString: Mon Dec 31 00:00:53 PST 2018  <-- Input date
Cal.year 2018 Cal.month 11 Cal.day 31 Cal.week 1
ssa conversion: 191231 
log conversion: Mon 12/31/2019 <-- Result is bad
java.util.GregorianCalendar[time=1546243253716,areFieldsSet=true,areAllFieldsSet=true,lenient=false,zone=sun.util.calendar.ZoneInfo[id="America/Los_Angeles",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=11,WEEK_OF_YEAR=1,WEEK_OF_MONTH=6,DAY_OF_MONTH=31,DAY_OF_YEAR=365,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=5,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=53,MILLISECOND=716,ZONE_OFFSET=-28800000,DST_OFFSET=0]

toString: Tue Jan 01 00:00:53 PST 2019 <-- input date
Cal.year 2019 Cal.month 0 Cal.day 1 Cal.week 1
ssa conversion: 190101
log conversion: Tue 01/01/2019 <-- Result is good
    java.util.GregorianCalendar[time=1546329653716,areFieldsSet=true,areAllFieldsSet=true,lenient=false,zone=sun.util.calendar.ZoneInfo[id="America/Los_Angeles",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2019,MONTH=0,WEEK_OF_YEAR=1,WEEK_OF_MONTH=1,DAY_OF_MONTH=1,DAY_OF_YEAR=1,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=53,MILLISECOND=716,ZONE_OFFSET=-28800000,DST_OFFSET=0]

As I look forward to the dates I see the issue repeat in this pattern

- in 2018 12/30 (Sun) and 12/31(Mon) in 2018,
- in 2019 I have issues with 12/29(Sun), 12/30(Mon) and 12/31 (Tues)
- in 2020 12/27(Sun), 12/28(Mon), 12/29(Tues), 12/30 (Wed) 12/31(Thurs)  
- in 2021 12/26(Sun) - 12/31(Fri) 
- 2022 is correct  
- 2023 restarts the loop.  

It has to do with the last incomplete week of the year but I haven't been able to solve the issue.

kilidarria
  • 21
  • 1
  • 2
  • Scary please read the question this is not a duplicate of calendar returning incorrect date. c.set(2017, 11, 31, 0, 0); isn't relevant to the question the error happens on c.set(2017, **12**, 31, 0, 0); – kilidarria Dec 14 '16 at 17:42
  • Internally is was 2017,11,31, the error was in my sample code – kilidarria Dec 14 '16 at 22:54
  • @ScaryWombat Wrong duplicate. The real problem here is formatting with code uppercase `YYYY`/`YY` rather than lowercase `yyyy`/`yy`. Correct duplicate: [Java Date year calculation is off by year for two days](http://stackoverflow.com/q/14755742/642706). – Basil Bourque Dec 15 '16 at 01:07
  • Hi @BasilBourque this duplicate was correct at time of writing ;-) In version 1 the OP has the correct mask of `yyyy` - see also the comments below my answer – Scary Wombat Dec 15 '16 at 01:12
  • @ScaryWombat Aha. I see that now. Is there a way to close or mark a Question that has been edited to the extent that its meaning is altered? (I'm curious.) And I am now closing this for its new duplicate. – Basil Bourque Dec 15 '16 at 01:16
  • @kilidarria Lesson to learn here is that virtually all basic date-time questions and answers have already been posted to Stack Overflow. Search thoroughly before posting. – Basil Bourque Dec 15 '16 at 01:19
  • BasilBourque Alhtough I closed this question originally, It looks like this question was re-opened @ρяσѕρєя-k after the OP edited – Scary Wombat Dec 15 '16 at 01:22

1 Answers1

1

Months are 0 based i.e. January is 0 December is 11

try

c.set(2017, 11, 31, 0, 0);
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64