Java tutorial
//package com.java2s; /* * Copyright (C) 2015 iChano incorporation's Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; public class Main { public static String string2TimezoneUTF(String srcDateTime) { return string2Timezone("yyyy-MM-dd HH:mm:ss", srcDateTime, "yyyy-MM-dd'T'HH:mm:ss", "UTC"); } public static String string2Timezone(String srcFormater, String srcDateTime, String dstFormater, String dstTimeZoneId) { if (srcFormater == null || "".equals(srcFormater)) return null; if (srcDateTime == null || "".equals(srcDateTime)) return null; if (dstFormater == null || "".equals(dstFormater)) return null; if (dstTimeZoneId == null || "".equals(dstTimeZoneId)) return null; SimpleDateFormat sdf = new SimpleDateFormat(srcFormater); try { int diffTime = getDiffTimeZoneRawOffset(dstTimeZoneId); Date d = sdf.parse(srcDateTime); long nowTime = d.getTime(); long newNowTime = nowTime - diffTime; d = new Date(newNowTime); return date2String(dstFormater, d); } catch (ParseException e) { return null; } finally { sdf = null; } } public static String string2Timezone(String srcFormater, String srcDateTime, String dstFormater, String srcTimeZoneId, String dstTimeZoneId) { if (srcFormater == null || "".equals(srcFormater)) return null; if (srcDateTime == null || "".equals(srcDateTime)) return null; if (dstFormater == null || "".equals(dstFormater)) return null; if (dstTimeZoneId == null || "".equals(dstTimeZoneId)) return null; SimpleDateFormat sdf = new SimpleDateFormat(srcFormater); try { int diffTime = getDiffTimeZoneRawOffset(srcTimeZoneId, dstTimeZoneId); Date d = sdf.parse(srcDateTime); long nowTime = d.getTime(); long newNowTime = nowTime - diffTime; d = new Date(newNowTime); return date2String(dstFormater, d); } catch (ParseException e) { return null; } finally { sdf = null; } } private static int getDiffTimeZoneRawOffset(String timeZoneId) { return TimeZone.getDefault().getRawOffset() - TimeZone.getTimeZone(timeZoneId).getRawOffset(); } private static int getDiffTimeZoneRawOffset(String srcZoneId, String toZoneId) { return TimeZone.getTimeZone(srcZoneId).getRawOffset() - TimeZone.getTimeZone(toZoneId).getRawOffset(); } public static String getTime() { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String str = ""; try { str = sdf.format(System.currentTimeMillis()); } catch (Exception e) { e.printStackTrace(); } return str; } public static String date2String(String formater, Date aDate) { if (formater == null || "".equals(formater)) return null; if (aDate == null) return null; return (new SimpleDateFormat(formater)).format(aDate); } public static String date2String(String formater) { return date2String(formater, new Date()); } }