由于java默认GMT+0为标准时区,比中国所在时区整整滞后八个小时,所有要获取与我们所在地一样的时间,应先更改时区的设置。
以下代码包含两种获取时间的方法——Calendar获取时间和Date获取时间;包含两种转换字符串为时间的方法——Timestamp.valueOf(str)的方法和SimepleDateFormat的format.parse(str)方法;还包含了两种时间格式的设置方法——SimpleDateFormat("格式模式")和DateFormat.getDateTimeInstance()方法。
运行结果:
sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
sun.util.calendar.ZoneInfo[id="GMT+08:00",offset=28800000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
Calendar获得的日期:2010-7-20 9:14:8 星期2
2010/05/25 09:25:30
2010-7-20 9:14:08