当前位置:编程学习 > JAVA >>

关于SimpleDateFormat

代码如下:
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

问题:造成这种差别的原因? --------------------编程问答-------------------- --------------------编程问答-------------------- 擦...这个文档我也有的...问题是为啥大写的Y就不对了呢?当Y为大写时,程序是如何parse的? --------------------编程问答--------------------
引用 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)
--------------------编程问答-------------------- 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)
--------------------编程问答--------------------
引用 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 也是最新的 --------------------编程问答--------------------
引用 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中一定会有说明含义的 --------------------编程问答-------------------- 没事纠结这种问题干嘛 --------------------编程问答--------------------
引用 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")……
--------------------编程问答-------------------- 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 --------------------编程问答--------------------
引用 7 楼 IT0918liu 的回复:
没事纠结这种问题干嘛

就是一次作业比较日期时老是和预期的不一致,后来一测试发现对字符串的转换不对,想知道为啥不对 --------------------编程问答--------------------
引用 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的帮助文档看看吧。虽然没懂,但知道问题所在了,准备考试就不纠结它了
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,