SimpleDateFormat
是一个以国别敏感的方式格式化和分析数据的具体类。 它容许格式化 (date -> text)、语法分析 (text -> date)和标准化。java
SimpleDateFormat
容许觉得日期-时间格式化选择任何用户指定的方式启动。 可是,但愿用 DateFormat
中的getTimeInstance
、 getDateInstance
或 getDateTimeInstance
建立一个日期-时间格式化程序。 每一个类方法返回一个以缺省格式化方式初始化的日期/时间格式化程序。 能够根据须要用 applyPattern
方法修改格式化方式。
SimpleDateFormat函数的继承关系:web
java.lang.Object app
| +----java.text.Format 函数
| +----java.text.DateFormat 测试
| +----java.text.SimpleDateFormatspa
下面是个小例子:.net
import java.text.*; import java.util.Date;code
/** SimpleDateFormat函数语法:orm
G 年代标志符 y 年 M 月 d 日 h 时 在上午或下午 (1~12) H 时 在一天中 (0~23) m 分 s 秒 S 毫秒 E 星期 D 一年中的第几天 F 一月中第几个星期几 w 一年中第几个星期 W 一月中第几个星期 a 上午 / 下午 标记符 k 时 在一天中 (1~24) K 时 在上午或下午 (0~11) z 时区 */ blog
public class FormatDateTime {
public static void main(String[] args) {
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm");
SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等价于now.toLocaleString()
SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");
SimpleDateFormat myFmt4=new SimpleDateFormat( "一年中的第 D 天 一年中第w个星期 一月中第W个星期 在一天中k时 z时区");
Date now=new Date();
System.out.println(myFmt.format(now));
System.out.println(myFmt1.format(now));
System.out.println(myFmt2.format(now));
System.out.println(myFmt3.format(now));
System.out.println(myFmt4.format(now));
System.out.println(now.toGMTString());
System.out.println(now.toLocaleString());
System.out.println(now.toString());
}
}
效果: 2004年12月16日 17时24分27秒 04/12/16 17:24 2004-12-16 17:24:27 2004年12月16日 17时24分27秒 星期四 一年中的第 351 天 一年中第51个星期 一月中第3个星期 在一天中17时 CST时区 16 Dec 2004 09:24:27 GMT 2004-12-16 17:24:27 Thu Dec 16 17:24:27 CST 2004
下面是个JavaBean: public class FormatDateTime {
public static String toLongDateString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");
return myFmt.format(dt);
}
public static String toShortDateString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yy年MM月dd日 HH时mm分");
return myFmt.format(dt);
}
public static String toLongTimeString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("HH mm ss SSSS");
return myFmt.format(dt);
} public static String toShortTimeString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yy/MM/dd HH:mm");
return myFmt.format(dt);
}
public static void main(String[] args) {
Date now=new Date();
System.out.println(FormatDateTime.toLongDateString(now));
System.out.println(FormatDateTime.toShortDateString(now));
System.out.println(FormatDateTime.toLongTimeString(now));
System.out.println(FormatDateTime.toShortTimeString(now));
}
}
调用的main 测试结果: 2004年12月16日 17时38分26秒 星期四 04年12月16日 17时38分 17 38 26 0965 04/12/16 17:38
// 12小时制 SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm");
// 24小时制 SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
使用SimpleDateFormat 时,小时若是是小写的“hh”为12小时制,若是是大写的“HH”为24小时制;
c.get(Calendar.HOUR); // 12小时制 c.get(Calendar.HOUR_OF_DAY); // 24小时制
示例:
public class Test {
public static void main(String[] args) throws ParseException {
String time="2011-05-12 12:02";
// 12小时制
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm");
// 24小时制
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date d1 = sdf1.parse(time);
Date d2 = sdf2.parse(time);
System.out.println(d1.toString());
// 从新format倒是正确的
System.out.println(sdf1.format(d1).toString());
System.out.println(d2.toString());
Calendar c=Calendar.getInstance();
c.setTime(new Date());
c.get(Calendar.HOUR);
System.out.println(c.getTime().toString());
c.get(Calendar.HOUR_OF_DAY);
System.out.println(c.getTime().toString());
}
}
参考:http://blog.csdn.net/shibenjie/archive/2009/06/12/4263912.aspx
我碰到的问题终于解决了:每到下午,设定的时间会+12小时,这样设置的报警时间就成了明天的时间了。
问题在这里:c.get(Calendar.HOUR); // 12小时制
c.get(Calendar.HOUR_OF_DAY); // 24小时制