java中把中标准时间换成yyyy-mm-dd hh:mm:ss的Date
java中把中标准时间换成yyyy-mm-dd hh:mm:ss的Date
如:Wed Jan 04 06:05:00 CST 2017 换成2017-02-04 06:05:00
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.format(new Date());
DateFormate类和其子类SimpleDateFormate
String string = "2015-02-10 22:00:00";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1;
try {
d1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(string);
System.out.println("DateTime d1>>>>>>: " + d1);
String d2 = format.format(d1);
System.out.println("DateTime d2>>>>>>: " + d2);
Date d3;
d3 = format.parse(d2);
System.out.println("DateTime d3>>>>>>: " + format.format(d3));
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
我之前做过的至少有三种能实现这样的效果
1、Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料
t.setToNow(); // 取得系统时间。
int year = t.year;
int month = t.month;
int date = t.monthDay;
int hour = t.hour; // 0-23
2、DateFormat df = new SimpleDateFormat("HH:mm:ss");
df.format(new Date());
然后获取到了相应的数据后打印出来就行了呗
似乎打印自定义的格式必须要把Date转换为字符串,它才存在格式问题,如果是Date类型,就一定是Wed Jan 04 06:05:00 CST 2017这种格式,所以你的这个效果是达不到的,想要有格式,就必须要用SimpleDateFormat或是DateFormat 转换成字符串,要使用date又可以把它转换为date。
Date date=new Date();
System.out.println(date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
try {
String strDate = sdf.format(date);
System.out.println(strDate);
Date newDate = sdf.parse(strDate);
System.out.println(newDate);
} catch (Exception e) {
}
Sun Feb 05 14:26:15 CST 2017
2017-02-05 14:15:26
Sun Feb 05 14:26:15 CST 2017
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String time = format.format(date);
System.out.println("当前时间为:"+time);
http://blog.csdn.net/itzhangdaopin/article/details/53688808
可以参考这里的资料