我需要通过接口把时间查出来,然后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啊?请教了。
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啊?请教了。
解决方案 »
- 单选框和复选框值的回显------
- struts2是实现商品种类的添加
- cookie问题
- 在Eclipse下运行AntBuild提示<terminated>如何解决?
- java 图片处理
- hibernate出现的问题!
- J2SDK1.4.2+Apache Tomcat4.06+Oracle9i中的Tomacat的问题特急,请高手来看看,不胜感激~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 请问《java编程思想》最新是第几版呢?想学java,请帮忙,谢谢
- 请教一个j2ee的三层问题
- *.ftl 模版 生成word 后 打开时总是要恢复?
- 在循环外,点一个按钮,所有的表单都提交
- 抽象类、接口作为方法参数 java 反射 (只有这点分了。。。)
试试
送进去的日期却是,2011-1-1 当然是错的。
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();
}
}
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格式的字符串。
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();
}
}
DateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");yyyyMMdd改为yyyy-MM-dd就行
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat (yyyy-MM-dd);
sdf.format(date);
System.out.println(sdf.format(date););
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"));的时候就会抛错了。