Java DATE类型日期转换为指定格式


使用SimpleDateFormat类,此类在java.text包下:

import java.text.SimpleDateFormat;或者直接使用java.text.SimpleDateFormat定义变量

转换步骤:

1、原格式

String str = "2012-09-19 23:00:09";

2、新格式

SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

或者 java.text.SimpleDateFormat sd = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

3、转换

Date d = sd.parse(str);

String new = sd.format(d); //Date类型转换为其他格式String类型


"yyyy-MM-dd HH:mm:ss" "yyyy年MM月dd日 HH时mm分ss秒"


取出时间与系统时间差8小时

通过Java Date取出的时间与系统时间对不上

解决方法:设置时区

TimeZone tz = TimeZone.getTimeZone("ETC/GMT-8");

TimeZone.setDefault(tz);