Anddoi converts a time to a specified time zone

Source: Internet
Author: User

Import java. text. Format;
Import java. text. ParseException;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import java. util. Properties;
Import java. util. SimpleTimeZone;
Import java. util. TimeZone;

Import javax.swing.text.html. HTMLDocument. Iterator;


Public class Test2 {

Protected static Format format = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");

/**
* TimeZoneOffset indicates the time zone. For example, China generally uses UTC + 8, so timeZoneOffset is 8.
* @ Param timeZoneOffset
* @ Return
*/
Public String getFormatedDateString (int timeZoneOffset ){
If (timeZoneOffset> 13 | timeZoneOffset <-12 ){
TimeZoneOffset = 0;
}
TimeZone timeZone;
String [] ids = TimeZone. getAvailableIDs (timeZoneOffset x 60*60*1000 );
If (ids. length = 0 ){
// If no ids were returned, something is wrong. use default TimeZone
TimeZone = TimeZone. getDefault ();
} Else {
TimeZone = new SimpleTimeZone (timeZoneOffset * 60*60*1000, ids [0]);
}

SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
Sdf. setTimeZone (timeZone );
Return sdf. format (new Date ());
}

Public static String getFormatedDateString (String _ timeZone) throws Exception {
SimpleDateFormat s = new SimpleDateFormat ("yyyy/MM/dd HH: mm ");
Date ddd = s. parse ("2014/6/13 4:00 ");
TimeZone timeZone = null;
If ("". equals (_ timeZone )){
TimeZone = TimeZone. getDefault ();
} Else {
TimeZone = TimeZone. getTimeZone (_ timeZone );
}

SimpleDateFormat sdf = new SimpleDateFormat ("MM minute ss seconds on mm dd, yyyy ");
Sdf. setTimeZone (timeZone );
// TimeZone. setDefault (timeZone );
Return sdf. format (ddd );
}

Public static void setCNTimeZone (){
Final TimeZone zone = TimeZone. getTimeZone ("GMT + 8 ");
TimeZone. setDefault (zone );
}

Public static void main (String args []) throws Exception {
System. out. println (getFormatedDateString (""));
System. out. println (getFormatedDateString ("Asia/Shanghai "));
System. out. println (getFormatedDateString ("Asia/Seoul "));
System. out. println (getFormatedDateString ("Europe/Madrid "));
System. out. println (getFormatedDateString ("GMT + 1:00 "));
System. out. println (TimeZone. getDefault (). getID ());
}


}

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.