Java Date to String convertDateToServerTimeZoneDate(DateFormat formatter, String timeZoneName, long milliSecond)

Here you can find the source of convertDateToServerTimeZoneDate(DateFormat formatter, String timeZoneName, long milliSecond)

Description

This will convert server date/time to specified Time zone date/time and return the date in specified format.

License

Open Source License

Parameter

Parameter Description
formatter a parameter
timeZoneName a parameter
milliSecond a parameter

Declaration

public static String convertDateToServerTimeZoneDate(DateFormat formatter, String timeZoneName,
        long milliSecond) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    /**//from   w ww. j  ava2s  .  c  o  m
     * This will convert server date/time to specified Time zone date/time and return the date in specified format.
     * 
     * @param formatter
     * @param timeZoneName
     * @param milliSecond
     */
    public static String convertDateToServerTimeZoneDate(DateFormat formatter, String timeZoneName,
            long milliSecond) {
        Long loginTimeZoneDate = milliSecond;
        if (timeZoneName != null) {
            loginTimeZoneDate = convertDateToServerTimeZoneDateInMilliSec(timeZoneName, milliSecond);
        }
        return formatter.format(new Date(loginTimeZoneDate));
    }

    /**
     * This will convert server date/time to specified Time zone date/time and return the date in milliSecond.
     * 
     * @param timeZoneName
     * @param milliSecond
     * @return
     */
    public static long convertDateToServerTimeZoneDateInMilliSec(String timeZoneName, long milliSecond) {
        if (timeZoneName != null) {
            TimeZone loginTimeZone = TimeZone.getTimeZone(timeZoneName);
            Calendar calender = Calendar.getInstance(loginTimeZone);
            if (milliSecond != 0) {
                calender.setTimeInMillis(milliSecond);
            }
            calender.add(Calendar.MILLISECOND, -(calender.getTimeZone().getRawOffset()));
            calender.add(Calendar.MILLISECOND, -(calender.getTimeZone().getDSTSavings()));
            calender.add(Calendar.MILLISECOND, TimeZone.getDefault().getRawOffset());
            return calender.getTimeInMillis();
        }
        return milliSecond;
    }
}

Related

  1. convertDate2Str(java.util.Date date, String format, String defaultValue)
  2. convertDate2String(Date date)
  3. convertDate2String8(Date time)
  4. convertDateTimeToString(Date date)
  5. convertDateTo(Date date, String patternToUse)
  6. convertDateToStr(Date d, String format)
  7. convertDateToStr(Date date, String format)
  8. convertDateToString(final Date datum, final boolean withDay)
  9. convertDateToString(java.util.Date dt, String pattern)