gpt4 book ai didi

java - 将 GregorianCalendar 与 SimpleDateFormat 一起使用

转载 作者:IT老高 更新时间:2023-10-28 21:11:34 28 4
gpt4 key购买 nike

所以,我一直在为这个(应该)简单的练习绞尽脑汁,让程序将日期字符串转换为 GregorianCalendar 对象,对其进行格式化,然后再次将其作为完成后的字符串。

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

我在多个地方检查了代码,并且代码完全按照它应该做的那样做,直到我调用格式函数,它抛出了一个 IllegalArgumentExceptionGergorianCalendar 对象被分配了它应该被分配的值(尽管打印它又是一个完全不同的故事,如下所示......),但格式不会接受该对象进行格式化。

不幸的是,讲师不太清楚如何使用 GregorianCalendarSimpleDateFormat(但已指定我们使用它们)并说:“只需谷歌一下......”我试过了,但我发现没有任何帮助。

我目前的代码是:

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;
}
}

我得到的错误是:

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)

And another thing, am I even using the GregorianCalendar right?? When I print out that object's value (should be getting a date, right?) I get this:

java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=false,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/Vancouver",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=189,lastRule=java.util.SimpleTimeZone[id=America/Vancouver,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=?,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,MILLISECOND=?,ZONE_OFFSET=?,DST_OFFSET=?]

The year, month and day_of_month values are all correct as they are the numbers I passed into the creation of it.

Thoughts, suggestions, am I even close?

Edit

Original issues cleared up (thank you assylias!), but I still can't print properly because the two functions aren't linked and the requirements are to have a GregorianCalendar date value printed out from the person object (as birthdate is a GregorianCalendar).

Updated code:

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;
}

}

上次编辑

好的,看来我是个白痴,不需要将两个 DateUtil 函数链接在一起,而是串联使用它们。首先,将生日转换为 GregorianCalendar 并将其存储在 person 对象中。然后,在打印语句中,只需告诉程序在打印日期时格式化该日期。问题解决了。现在一切都按照规范工作,我觉得自己更愚蠢,因为我在使用 DateUtil 类的最后一天左右的时间里像一条出水的鱼一样挥舞着,试图让它们同时工作时间。

感谢大家在正确输入日期方面的帮助!

最佳答案

SimpleDateFormat.format() 方法将 Date 作为参数。您可以通过调用 getTime() 方法从 Calendar 获取 Date:

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

return dateFormatted;
}

另请注意,月份从 0 开始,因此您的意思可能是:

int month = Integer.parseInt(splitDate[1]) - 1;

关于java - 将 GregorianCalendar 与 SimpleDateFormat 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10829942/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com