小编Rej*_*rts的帖子

将GregorianCalendar与SimpleDateFormat一起使用

因此,我一直在思考这个(应该是)简单的练习,让程序将日期字符串转换为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 …
Run Code Online (Sandbox Code Playgroud)

java gregorian-calendar simpledateformat illegalargumentexception

28
推荐指数
2
解决办法
9万
查看次数