The time used in the Cloudstack project is UTC time, specifically what is UTC time we can Baidu, but we need time is normal time, so in Guotai Junan development test Cloud, synchronous resource management in the synchronization of virtual machine management, the need for virtual machine time format conversion. Tool class, the key is the time format problem, the time format is 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;
}
Test class:
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));
}
}
Another method is as follows:
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 ();
}
}
Reprint Please specify: http://www.xujin.org
Cloudstack UTC time to local time in the cloud host