Cloudstack UTC time to local time in the cloud host

Source: Internet
Author: User
Tags cloudstack

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.