Calendar时间很诡异,自己会变,求教

wcl_friend 2011-03-31 10:28:18
我需要通过接口把时间查出来,然后set到Calendar类型的字段里面。取出来的格式是:yyyy-MM-dd的,要转成yyyyMMdd的存在Calendar类型的字段里。

private void testDate() {
DateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
try {
Calendar calendar = Calendar.getInstance();
calendar.setTime(simpleDateFormat.parse("2011-1-1"));
System.out.println(calendar.getTime());
System.out.println(simpleDateFormat.format(calendar.getTime()));
} catch (Exception e) {
e.printStackTrace();
}
}

为什么打印出来的是:
Sat Oct 30 00:00:00 CST 2010
20101030

怎么不是2011-1-1啊?

请教了。
...全文
313 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
wcl_friend 2011-04-06
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 cxj61126 的回复:]

引用 12 楼 wcl_friend 的回复:

其实Calendar这个对象挺难控制的,比如:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
calendar.setTime(format.parse("2011-02-29"));

他会把2011-2-29解析成2011-3-1,需要,dateFormat……
[/Quote]

不能这么说啊,我们不能保证用户为怎么输,但如果他输入,2011-2-29的话,我们给他错误提示,而不是将他转成2011-3-1.
总之,学习了。。。。
wcl_friend 2011-04-06
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wcl_friend 的回复:]

其实Calendar这个对象挺难控制的,比如:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
calendar.setTime(format.parse("2011-02-29"));

他会把2011-2-29解析成2011-3-1,需要,dateFormat.setLenient(false);
这样再执行c……
[/Quote]

嗯,我也是后来才知道的。谢了
cxj61126 2011-04-02
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wcl_friend 的回复:]

其实Calendar这个对象挺难控制的,比如:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
calendar.setTime(format.parse("2011-02-29"));

他会把2011-2-29解析成2011-3-1,需要,dateFormat.setLenient(false);
这样再执行c……
[/Quote]大哥2011-2-29没这日子,自已弄个不合逻辑的时间,2011-2-28是2月最后一天,你写29,当然会给你写成3月1号的
amos1989 2011-04-01
  • 打赏
  • 举报
回复
好吧。既然问题解决了我就接一点分吧。
wcl_friend 2011-04-01
  • 打赏
  • 举报
回复
其实Calendar这个对象挺难控制的,比如:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
calendar.setTime(format.parse("2011-02-29"));

他会把2011-2-29解析成2011-3-1,需要,dateFormat.setLenient(false);
这样再执行calendar.setTime(format.parse("2011-02-29"));的时候就会抛错了。
wcl_friend 2011-04-01
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 flounders 的回复:]

楼主马虎问题,DateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");yyyyMMdd改为yyyy-MM-dd就行了,请给分
[/Quote]

是的,你说的对,我给分
luman2002 2011-03-31
  • 打赏
  • 举报
回复

private void testDate() {
DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Calendar calendar = Calendar.getInstance();
calendar.setTime(simpleDateFormat.parse("2011-1-1"));
System.out.println(calendar.getTime());
simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
System.out.println(simpleDateFormat.format(calendar.getTime()));
} catch (Exception e) {
e.printStackTrace();
}
}

  • 打赏
  • 举报
回复

private static void testDate() {
DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Calendar calendar = Calendar.getInstance();
calendar.setTime(simpleDateFormat.parse("2011-1-1"));
System.out.println(calendar.getTime());
System.out.println(simpleDateFormat.format(calendar.getTime()));
} catch (Exception e) {
e.printStackTrace();
}
}

1st System.out.println(calendar.getTime());
这样调用的时候会调用Calendar的toString方法:

public String toString() {
// NOTE: BuddhistCalendar.toString() interprets the string
// produced by this method so that the Gregorian year number
// is substituted by its B.E. year value. It relies on
// "...,YEAR=<year>,..." or "...,YEAR=?,...".
StringBuilder buffer = new StringBuilder(800);
buffer.append(getClass().getName()).append('[');
appendValue(buffer, "time", isTimeSet, time);
buffer.append(",areFieldsSet=").append(areFieldsSet);
buffer.append(",areAllFieldsSet=").append(areAllFieldsSet);
buffer.append(",lenient=").append(lenient);
buffer.append(",zone=").append(zone);
appendValue(buffer, ",firstDayOfWeek", true, (long) firstDayOfWeek);
appendValue(buffer, ",minimalDaysInFirstWeek", true, (long) minimalDaysInFirstWeek);
for (int i = 0; i < FIELD_COUNT; ++i) {
buffer.append(',');
appendValue(buffer, FIELD_NAME[i], isSet(i), (long) fields[i]);
}
buffer.append(']');
return buffer.toString();
}

该方法返回的区时Timezone格式的字符串。
Coolfatman 2011-03-31
  • 打赏
  • 举报
回复

private void testDate() {
DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Calendar calendar = Calendar.getInstance();
calendar.setTime(simpleDateFormat.parse("2011-1-1"));
System.out.println(calendar.getTime());
System.out.println(simpleDateFormat.format(calendar.getTime(),"yyyyMMdd"));
} catch (Exception e) {
e.printStackTrace();
}
}

Coolfatman 2011-03-31
  • 打赏
  • 举报
回复
你日期格式 设为 yyyyMMdd (DateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");)
送进去的日期却是,2011-1-1 当然是错的。
Intboy 2011-03-31
  • 打赏
  • 举报
回复
应该要将时间进行格式化吧!
zqfddqr 2011-03-31
  • 打赏
  • 举报
回复
calendar.setTime(simpleDateFormat.parse("20110101"));
试试
shenjiao080601 2011-03-31
  • 打赏
  • 举报
回复
格式化个日期真不需要那么难的。。。
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat (yyyy-MM-dd);
sdf.format(date);
System.out.println(sdf.format(date););
ljz_761121 2011-03-31
  • 打赏
  • 举报
回复
格式化问题:
DateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");yyyyMMdd改为yyyy-MM-dd就行
flounders 2011-03-31
  • 打赏
  • 举报
回复
楼主马虎问题,DateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");yyyyMMdd改为yyyy-MM-dd就行了,请给分

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧