关于SimpleDateFormat

wenwuchige 2012-11-05 10:24:43
代码如下:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class ShowDate {

public static void main(String[] args) throws ParseException
{
SimpleDateFormat form = new SimpleDateFormat("YYYY-MM-dd-HH-mm");

String start = "2012-05-09-04-08";
String end = "2012-05-10-04-08";

System.out.println(" 你给的时间段为:");

Date date1 = form.parse(start);
Date date2 = form.parse(end);

System.out.println(start);
System.out.println(end);

System.out.println(date1);
System.out.println(date1);

System.out.println(form.format(date1));
System.out.println(form.format(date2));

}
}
new SimpleDateFormat("YYYY-MM-dd-HH-mm")时y写成了Y
结果输出如下:

你给的时间段为:
2012-05-09-04-08
2012-05-10-04-08
Sun Jan 01 04:08:00 CST 2012
Sun Jan 01 04:08:00 CST 2012
2012-01-01-04-08
2012-01-01-04-08
然后将Y改为y结果就正确了。。。
你给的时间段为:
2012-05-09-04-08
2012-05-10-04-08
Wed May 09 04:08:00 CST 2012
Wed May 09 04:08:00 CST 2012
2012-05-09-04-08
2012-05-10-04-08

问题:造成这种差别的原因?
...全文
361 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
IT0918liu 2012-11-07
  • 打赏
  • 举报
回复
没事纠结这种问题干嘛
wenwuchige 2012-11-07
  • 打赏
  • 举报
回复
引用 9 楼 ticmy 的回复:
A week year is in sync with a WEEK_OF_YEAR cycle. All weeks between the first and last weeks (inclusive) have the same week year value. Therefore, the first and last days of a week year m……
嗯,我去下个1.7的帮助文档看看吧。虽然没懂,但知道问题所在了,准备考试就不纠结它了
wenwuchige 2012-11-07
  • 打赏
  • 举报
回复
引用 7 楼 IT0918liu 的回复:
没事纠结这种问题干嘛
就是一次作业比较日期时老是和预期的不一致,后来一测试发现对字符串的转换不对,想知道为啥不对
龙四 2012-11-07
  • 打赏
  • 举报
回复
引用 5 楼 wenwuchige 的回复:
引用 4 楼 diypyh 的回复:LZ好神啊,不知道你的是什么环境?我的是JDK1.6+MyEclipse9.0: Java code12345public static void main(String[] args) { String str = new SimpleDateFormat("YYYY-MM-dd").format(new Date()); ……
如果能用Y,那么jdk 1.7.0_05的API中一定会有说明含义的
龙四 2012-11-07
  • 打赏
  • 举报
回复
A week year is in sync with a WEEK_OF_YEAR cycle. All weeks between the first and last weeks (inclusive) have the same week year value. Therefore, the first and last days of a week year may have different calendar year values. http://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#week_year
龙四 2012-11-07
  • 打赏
  • 举报
回复
引用 6 楼 ticmy 的回复:
引用 5 楼 wenwuchige 的回复:引用 4 楼 diypyh 的回复:LZ好神啊,不知道你的是什么环境?我的是JDK1.6+MyEclipse9.0:
Java code12345public static void main(String[] args) { String str = new SimpleDateFormat("YYYY-MM-dd")……
wenwuchige 2012-11-06
  • 打赏
  • 举报
回复
引用 4 楼 diypyh 的回复:
LZ好神啊,不知道你的是什么环境?我的是JDK1.6+MyEclipse9.0: Java code12345public static void main(String[] args) { String str = new SimpleDateFormat("YYYY-MM-dd").format(new Date()); System.out.print……
我的jdk版本是:version "1.7.0_05" Eclipse 也是最新的
diypyh 2012-11-06
  • 打赏
  • 举报
回复
LZ好神啊,不知道你的是什么环境?我的是JDK1.6+MyEclipse9.0:

	public static void main(String[] args) {
		String str = new SimpleDateFormat("YYYY-MM-dd").format(new Date());
		System.out.println(str);
	}
当Y大写时报以下错误:

Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 'Y'
	at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:769)
	at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:576)
	at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:501)
	at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:476)
	at com.pyh.demo3.Demo5.main(Demo5.java:8)
龙四 2012-11-06
  • 打赏
  • 举报
回复
引用 2 楼 wenwuchige 的回复:
擦...这个文档我也有的...问题是为啥大写的Y就不对了呢?当Y为大写时,程序是如何parse的?
我表示用Y运行会报错

Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 'Y'
	at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:696)
	at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:515)
	at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:464)
	at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:445)
wenwuchige 2012-11-05
  • 打赏
  • 举报
回复
擦...这个文档我也有的...问题是为啥大写的Y就不对了呢?当Y为大写时,程序是如何parse的?
龙四 2012-11-05
  • 打赏
  • 举报
回复

50,594

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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