将GregorianCalendar与SimpleDateFormat一起使用

Rej*_*rts 28 java gregorian-calendar simpledateformat illegalargumentexception

因此,我一直在思考这个(应该是)简单的练习,让程序将日期字符串转换为GregorianCalendar对象,格式化它,并在完成时将其作为字符串再次返回.

这是一个程序的最后一点,从文件中获取文本,将其分解为单个记录,然后将记录分成单独的数据并将它们分配给一个人对象.

我已经在多个地方检查了代码,代码确实完成了它应该做的事情,直到我调用格式函数,这会抛出IllegalArgumentException.GergorianCalendar对象被赋予它应该被分配的值(虽然打印它是另一个完整的故事,如下所示......),但格式不接受格式化的对象.

不幸的是,教练不太确定如何使用GregorianCalendar和SimpleDateFormat(但指定我们使用它们)并且说"Just Google it"......我试过了,我发现没有任何帮助.

我到目前为止的代码是:

public class DateUtil {

    public static GregorianCalendar convertFromDMY(String dd_mm_yy) throws ParseException{

        // this actually works, got rid of the original code idea
        String[] splitDate = dd_mm_yy.split("-");
        int days = Integer.parseInt(splitDate[0]);
        int month = Integer.parseInt(splitDate[1]);
        int year = Integer.parseInt(splitDate[2]);

        // Dates are going in right, checked in print statement,
        // but the object is not getting formatted…
        GregorianCalendar dateConverted = new GregorianCalendar(year, month, days);
        format(dateConverted);
        return dateConverted;
    }

    public static String format(GregorianCalendar date){

        SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy");
        String dateFormatted = fmt.format(date);
        return dateFormatted;
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object >as a Date

    at java.text.DateFormat.format(DateFormat.java:281)
    at java.text.Format.format(Format.java:140)
    at lab2.DateUtil.format(DateUtil.java:26) 
    at lab2.DateUtil.convertFromDMY(DateUtil.java:19)
    at lab2.Lab2.createStudent(Lab2.java:75)
    at lab2.Lab2.main(Lab2.java:34)

还有一件事,我甚至使用GregorianCalendar吗?当我打印出该对象的值(应该是一个正确的日期?)我得到这个:

java.util.GregorianCalendar中的[时间= ?, areFieldsSet =假,areAllFieldsSet =假,宽松= TRUE,区= sun.util.calendar.ZoneInfo [ID = "美洲/温哥华",偏移量= -28800000,dstSavings = 3600000,useDaylight =真,过渡= 189,lastRule = java.util.SimpleTimeZone中[ID =美国/温哥华,偏移= -28800000,dstSavings = 3600000,useDaylight =真,startYear = 0,STARTMODE = 3,startMonth = 2,朝九特派= 8, startDayOfWeek = 1,开始时间= 7200000,startTimeMode = 0,endMode = 3,endMonth = 10,endday指定= 1,一个endDayOfWeek = 1,结束时间= 7200000,endTimeMode = 0]],Firstdayofweek可= 1,minimalDaysInFirstWeek = 1,ERA = ?, YEAR = 1985,MONTH = 4,WEEK_OF_YEAR = ?, WEEK_OF_MONTH = ?, DAY_OF_MONTH = 22,DAY_OF_YEAR = ?, DAY_OF_WEEK = ?, DAY_OF_WEEK_IN_MONTH = ?, AM_PM = 0,HOUR = 0,HOUR_OF_DAY = 0,MINUTE = 0,SECOND = 0,微差= ?, ZONE_OFFSET = ?, DST_OFFSET =?]

year,month和day_of_month值都是正确的,因为它们是我传递给它的数字.

思考,建议,我甚至关闭?

编辑

原始问题被清除(谢谢assylias!)但我仍然无法正确打印,因为这两个函数没有链接,并且要求是从person对象打印出GregorianCalendar日期值(因为birthdate是GregorianCalendar).

更新的代码:

public class DateUtil {

    static SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy");

    public static GregorianCalendar convertFromDMY(String dd_mm_yy) throws ParseException{

        // this actually works, got rid of the original code idea
        String[] splitDate = dd_mm_yy.split("-");
        int days = Integer.parseInt(splitDate[0]);
        int month = (Integer.parseInt(splitDate[1]) - 1);
        int year = Integer.parseInt(splitDate[2]);

        // dates go in properly
        GregorianCalendar dateConverted = new GregorianCalendar(year, month, days);
        String finalDate = format(dateConverted);
        return ;
    }

    public static String format(GregorianCalendar date) throws ParseException{

       fmt.setCalendar(date);
        String dateFormatted = fmt.format(date.getTime());
        System.out.println(dateFormatted);
        return dateFormatted;
    }

}
Run Code Online (Sandbox Code Playgroud)

最后编辑

好吧,好像我是一个白痴,并且不需要将两个DateUtil函数链接在一起,但是串联使用它们.首先,将birthdate转换为GregorianCalendar并将其存储在person对象中.然后在print语句中只需告诉程序在打印日期时格式化该日期.问题解决了.现在所有的工作都按照规格进行了,我觉得这很愚蠢,因为在最后一天左右,我在DateUtil课程中像鱼一样甩水,试图让它们同时工作.

感谢所有关于正确使用日期的帮助!

ass*_*ias 49

SimpleDateFormat.format()method以a Date为参数.你可以得到一个DateCalendar通过调用其getTime()方法:

public static String format(GregorianCalendar calendar) {
    SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy");
    fmt.setCalendar(calendar);
    String dateFormatted = fmt.format(calendar.getTime());

    return dateFormatted;
}
Run Code Online (Sandbox Code Playgroud)

另请注意,月份从0开始,所以您可能意味着:

int month = Integer.parseInt(splitDate[1]) - 1;
Run Code Online (Sandbox Code Playgroud)

  • 您可能还想在*SimpleDateFormat`中设置*日历,以便它知道使用它而不是使用不同的日历系统. (3认同)

m0s*_*it0 5

为什么会有这样的并发症?

public static GregorianCalendar convertFromDMY(String dd_mm_yy) throws ParseException 
{
    SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy");
    Date date = fmt.parse(dd_mm_yy);
    GregorianCalendar cal = GregorianCalendar.getInstance();
    cal.setTime(date);
    return cal;
}
Run Code Online (Sandbox Code Playgroud)