标签:
CloudStack项目中使用的时间是UTC时间,具体什么是UTC时间大家可以百度,但是我们需要的时间是正常的时间,所以在国泰君安开发测试云中,同步资源管理中虚拟机的同步管理,需要对虚拟机的时间格式化进行转换。工具类如下,关键是时间格式的问题,时间格式为yyyy-MM-dd‘T‘HH:mm:ss+SSSS。
public static Date utc2LocalDate(String utcTime) {
String utcTimePatten = "yyyy-MM-dd‘T‘HH:mm:ss+SSSS";
SimpleDateFormat sdf = new SimpleDateFormat(utcTimePatten);
Date dt = null;
try {
dt = sdf.parse(utcTime);
}
catch (ParseException e) {
e.printStackTrace();
}
return dt;
}
测试类:
public class TestUtcTime {
/**
*
* @param args
* @return void
*/
public static void main(String[] args) {
String ts = "2015-04-22T15:58:54+0800";
Date date = new Date();
System.out.println(date);
System.out.println(DateUtil.utc2LocalDate(ts));
}
}
另外的方法如下:
public class TestUtc {
public static void main(String args[]) {
try {
String ts = "2015-04-22T15:58:54+0800";
System.out.println("ts = " + ts);
ts = ts.replace("Z", " UTC");
System.out.println("ts = " + ts);
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd‘T‘HH:mm:ss+SSSS");
Date dt = sdf.parse(ts);
TimeZone tz = sdf.getTimeZone();
Calendar c = sdf.getCalendar();
System.out.println("Display name: " + tz.getDisplayName());
System.out.println(getString(c));
}
catch (ParseException pe) {
System.out.println("Error offset: " + pe.getErrorOffset());
pe.printStackTrace();
}
}
private static String getString(Calendar c) {
StringBuffer result = new StringBuffer();
result.append(c.get(Calendar.YEAR));
result.append("-");
result.append((c.get(Calendar.MONTH) + 1));
result.append("-");
result.append(c.get(Calendar.DAY_OF_MONTH));
result.append(" ");
result.append(c.get(Calendar.HOUR_OF_DAY));
result.append(":");
result.append(c.get(Calendar.MINUTE));
result.append(":");
result.append(c.get(Calendar.SECOND));
return result.toString();
}
}
转载请注明:http://www.xujin.org
标签:
原文地址:http://www.cnblogs.com/ACMer/p/4468106.html