Java TimeZone Format string2Timezone(String srcFormater, String srcDateTime, String dstFormater, String dstTimeZoneId)

Here you can find the source of string2Timezone(String srcFormater, String srcDateTime, String dstFormater, String dstTimeZoneId)

Description

string Timezone

License

Apache License

Declaration

public static String string2Timezone(String srcFormater, String srcDateTime, String dstFormater,
        String dstTimeZoneId) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {

    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 {/*from w  w  w .  j  a v a  2  s . c  o  m*/
            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 int getDiffTimeZoneRawOffset(String timeZoneId) {
        return TimeZone.getDefault().getRawOffset() - TimeZone.getTimeZone(timeZoneId).getRawOffset();
    }

    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());
    }
}

Related

  1. getTimezoneFormattedString(Date date, String timezone)
  2. getUserToServerDateTimeString(TimeZone timeZone, int dateFormat, int timeFormat, String date)
  3. getZfgcTimeZoneDateFormat(String timezone)
  4. resolveTimeZone(SimpleDateFormat sdf, String timezone)
  5. String2Date(String date, String formatPattern, Locale locale, TimeZone timeZone)
  6. toAPITimeString(Date date, String format, String timeZone)
  7. toTimeFormat(String timeFormat, TimeZone tz, Locale locale)