- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
所以,我一直在为这个(应该)简单的练习绞尽脑汁,让程序将日期字符串转换为 GregorianCalendar
对象,对其进行格式化,然后再次将其作为完成后的字符串。
这是程序的最后一点,它从文件中获取一小段文本,将其分解为单独的记录,然后将记录分解为单独的数据片段并将它们分配给人员对象。
我在多个地方检查了代码,并且代码完全按照它应该做的那样做,直到我调用格式函数,它抛出了一个 IllegalArgumentException
。 GergorianCalendar
对象被分配了它应该被分配的值(尽管打印它又是一个完全不同的故事,如下所示......),但格式不会接受该对象进行格式化。
不幸的是,讲师不太清楚如何使用 GregorianCalendar
和 SimpleDateFormat
(但已指定我们使用它们)并说:“只需谷歌一下......”我试过了,但我发现没有任何帮助。
我目前的代码是:
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/
我正在使用 CMIS 和 Java,我从文档加载所有属性并将它们写入 xml 文件。 我使用 Object.toString() 方法在 xml 文件中创建文本节点。因此我使用 .toString()
对于以下代码片段 GregorianCalendar a = new GregorianCalendar(2009, 11, 10); System.out.println(a.getTime());
我在一个文本文件中有以下数据,稍后我将扫描该文件。 17-Mar-2006 1100 1280 16-Jan-2002 1120 1230 15-Jun-2003 1140 1900 我正在将此数据构
这个问题已经有答案了: "EEE MMM dd HH:mm:ss ZZZ yyyy" date format to java.sql.Date (2 个回答) 已关闭 4 年前。 我是 Java 新手
我在使用 GregorianCalendar 时遇到问题,因此请您帮我解决一下。首先我会给你我的代码: private String changeClock(String day, String cl
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎偏离主题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或 include a mini
这个问题已经有答案了: why do I get wrong month when parsing with java calendar (3 个回答) 已关闭5 年前。 我有下面的代码,我试图在其中
好的。 calendar.get(Calendar.MONTH); 返回当前月份的索引。例如,如果现在执行这行代码,它将返回 August 的 7。 但是 calendar.get(Calendar.
当我像这样使用 GregorianCalendar 实例化新日期时: GregorianCalendar myCal = new GregorianCalendar(29,5,2011); 然后,最后
java 6.0 假设今天是:2018 年 8 月 8 日 此处方法返回 2 个日期的月差。 public static Integer getDiffMonths(Date dateFrom
我在 Java 中使用 GregorianCalendar 类时遇到问题。为什么 get 方法会更改我的 GregorianCalendar 类型的变量? 我从事这项工作: System.out.pr
我尝试将字符串转换为 GregorianCalendar,然后检查它。下面,我发布我的代码。我真的很困惑为什么字段看起来不对。非常感谢您的帮助。 public class test{ priv
java.util.GregorianCalendar 是轻量级对象吗?换句话说,在 Web 应用程序设置中使用每个请求调用构造函数只会带来微不足道的性能损失,还是缓存对象的副本以供应用程序共享使用是
我的程序中有这段代码: if (expire.after(start) && expire.before(end)) //do somethig 但是这段代码不包含绑定(bind)日期。我的意
这个问题已经有答案了: How to sanity check a date in Java (25 个回答) 已关闭 7 年前。 GregorianCalendar构造函数有签名: Gregoria
我想计算一年中特定周数的星期一。我是这样做的: final GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY); ca
出现此错误,JSP 页面变为空白(白色): Jan 9, 2013 7:30:39 PM org.apache.catalina.loader.WebappClassLoader clearThrea
我正在尝试设置 HOUR_OF_DAY 字段并更改 GregorianCalendar 日期对象的时区。 GregorianCalendar date = new GregorianCalendar(
虽然我阅读了很多关于日历和公历的内容,但仍然会出现一些复杂情况。哪个更好或者您可以推荐使用什么?我无法弄清楚其中的区别。 GregorianCalendar gc = (GregorianCalend
TimeZone utcTimezone_ = TimeZone.getTimeZone("UTC"); TimeZone _utc = TimeZone.getTimeZone("Zulu"); D
我是一名优秀的程序员,十分优秀!