Java simpledateformat.format给出了一年中最后一周的错误日期

时间:2016-12-14 01:29:52

标签: java date formatting

这实际上是 Y返回2012的重复,而y在SimpleDateFormat中返回2011 我会在下面列出的那个上使用ding复制。我读到了答案,我仍然认为那里有一个错误。我系统中一周的第一天是星期日,所以来自java.doc的描述这应该是2017年的第53周而不是2018年的第一周,如果我要在1月1日到6日之间收到错误应该已经回归2017年。

不是日历的副本返回错误日期,日历工作正常。请参阅下面的其他信息我试图简化我的代码以便在EOD显示,因为我希望能够走出门外

使用SimpleDateFormat.Format将日历日期转换为12/31/2017(2017年, 11 ,31,0,0)(星期日的第一天,一年的最后一周) )SimpleDateFormat返回12/31 / 2018 。我用java 6,7,8尝试了这个并得到了相同的结果,我在两个不同版本的eclipse上尝试过它我在另一台开发人员计算机上尝试了所有这些相同的结果。这是从我的实际junit代码中提取的。

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.

    } 

}

测试结果

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]

当我期待日期时,我看到问题在这种模式中重复

- 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.  

这与一年中最后一个不完整的一周有关,但我无法解决这个问题。

1 个答案:

答案 0 :(得分:0)

月份为0,即1月份为12月份为11

c.set(2017, 11, 31, 0, 0);