[Java] Java 时区转换 →→→→→进入此内容的聊天室

来自 , 2019-02-16, 写在 Java, 查看 142 次.
URL http://www.code666.cn/view/f0682320
  1. /*
  2.    * Converts time from sourceTZ TimeZone to destTZ TimeZone.
  3.    *
  4.    * @return converted time, or the original time, in case the datetime could not be parsed
  5.    *
  6.    */
  7.   private String convTimeZone(String time, String sourceTZ, String destTZ)
  8.   {
  9.     final String DATE_TIME_FORMAT = "yyyyMMdd-HH:mm:ss";
  10.  
  11.     SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
  12.  
  13.     Date specifiedTime;
  14.     try {
  15.       if (sourceTZ != null)
  16.         sdf.setTimeZone(TimeZone.getTimeZone(sourceTZ));
  17.       else
  18.         sdf.setTimeZone(TimeZone.getDefault()); // default to server's timezone
  19.       specifiedTime = sdf.parse(time);
  20.     }
  21.     catch (Exception e1) {
  22.       try {
  23.         specifiedTime = new Time(time).getAsDate();
  24.       } catch (Exception e2) {
  25.         //
  26.         return time;
  27.       }
  28.     }
  29.  
  30.     // switch timezone
  31.     if (destTZ != null)
  32.       sdf.setTimeZone(TimeZone.getTimeZone(destTZ));
  33.     else
  34.       sdf.setTimeZone(TimeZone.getDefault()); // default to server's timezone
  35.  
  36.     return sdf.format(specifiedTime);
  37.   }
  38. //java/2351

回复 "Java 时区转换"

这儿你可以回复上面这条便签

captcha