Example usage for java.util TimeZone getOffset

List of usage examples for java.util TimeZone getOffset

Introduction

In this page you can find the example usage for java.util TimeZone getOffset.

Prototype

public int getOffset(long date) 

Source Link

Document

Returns the offset of this time zone from UTC at the specified date.

Usage

From source file:Main.java

public static Date transformTime(Date date, TimeZone oldZone, TimeZone newZone) {
    Date finalDate = null;/*from w  w w. j a  v a 2s  .c  om*/
    if (date != null) {
        int timeOffset = oldZone.getOffset(date.getTime()) - newZone.getOffset(date.getTime());
        finalDate = new Date(date.getTime() - timeOffset);
    }
    return finalDate;
}

From source file:Main.java

public static String getTimeAndDate(long j) {
    try {/*from w  ww.  j  av a 2s. c  om*/
        Calendar instance = Calendar.getInstance();
        TimeZone timeZone = TimeZone.getDefault();
        instance.setTimeInMillis(j);
        instance.add(14, timeZone.getOffset(instance.getTimeInMillis()));
        String format = new SimpleDateFormat(" dd, MMM yyyy").format(instance.getTime());
        Time time = new Time();
        time.set(j);
        return time.format("%H:%M") + format;
    } catch (Exception e) {
        return "";
    }
}

From source file:org.nodatime.tzvalidate.Java7Dump.java

/**
 * Work out the next transition from the given starting point, in the given time zone.
 * This checks each day until END, and assumes there is no more than one transition per day.
 * Once it's found a day containing a transition, it performs a binary search to find the actual
 * time of the transition.//w w w  .j a v  a2  s.  c  o m
 * Unfortunately, java.util.TimeZone doesn't provide a cleaner approach than this :(
 */
private static Long getNextTransition(TimeZone zone, long start, long end) {
    long startOffset = zone.getOffset(start);
    boolean startDaylight = zone.inDaylightTime(new Date(start));

    long now = start + ONE_DAY_MILLIS;
    Date nowDate = new Date();

    // Inclusive upper bound to enable us to find transitions within the last day.
    while (now <= end) {
        nowDate.setTime(now);
        if (zone.getOffset(now) != startOffset || zone.inDaylightTime(nowDate) != startDaylight) {
            // Right, so there's a transition strictly after now - ONE_DAY_MILLIS, and less than or equal to now. Binary search...
            long upperInclusive = now;
            long lowerExclusive = now - ONE_DAY_MILLIS;
            while (upperInclusive > lowerExclusive + 1) {
                // The values will never be large enough for this addition to be a problem.
                long candidate = (upperInclusive + lowerExclusive) / 2;
                nowDate.setTime(candidate);
                if (zone.getOffset(candidate) != startOffset || zone.inDaylightTime(nowDate) != startDaylight) {
                    // Still seeing a difference: look earlier
                    upperInclusive = candidate;
                } else {
                    // Same as at start of day: look later
                    lowerExclusive = candidate;
                }
            }
            // If we turn out to have hit the end point, we're done without a final transition. 
            return upperInclusive == end ? null : upperInclusive;
        }
        now += ONE_DAY_MILLIS;
    }
    return null;
}

From source file:Main.java

/**
 * This method will return the local time midnight for the provided normalized UTC date.
 *
 * @param normalizedUtcDate UTC time at midnight for a given date. This number comes from the
 *                          database//from  www. ja v  a  2s .  c om
 *
 * @return The local date corresponding to the given normalized UTC date
 */
private static long getLocalMidnightFromNormalizedUtcDate(long normalizedUtcDate) {
    /* The timeZone object will provide us the current user's time zone offset */
    TimeZone timeZone = TimeZone.getDefault();
    /*
     * This offset, in milliseconds, when added to a UTC date time, will produce the local
     * time.
     */
    long gmtOffset = timeZone.getOffset(normalizedUtcDate);
    long localMidnightMillis = normalizedUtcDate - gmtOffset;
    return localMidnightMillis;
}

From source file:com.google.samples.apps.iosched.util.TimeUtils.java

/**
 * Returns "Today", "Tomorrow", "Yesterday", or a short date format.
 *//*from  w  w  w  .  j a  v a 2 s  .c  o m*/
public static String formatHumanFriendlyShortDate(final Context context, long timestamp) {
    long localTimestamp, localTime;
    long now = UIUtils.getCurrentTime(context);

    TimeZone tz = SettingsUtils.getDisplayTimeZone(context);
    localTimestamp = timestamp + tz.getOffset(timestamp);
    localTime = now + tz.getOffset(now);

    long dayOrd = localTimestamp / 86400000L;
    long nowOrd = localTime / 86400000L;

    if (dayOrd == nowOrd) {
        return context.getString(R.string.day_title_today);
    } else if (dayOrd == nowOrd - 1) {
        return context.getString(R.string.day_title_yesterday);
    } else if (dayOrd == nowOrd + 1) {
        return context.getString(R.string.day_title_tomorrow);
    } else {
        return formatShortDate(context, new Date(timestamp));
    }
}

From source file:com.mousebird.maply.MaplyStarModel.java

public static double getJulianDateDouble(long millis) {

    TimeZone timeZone = TimeZone.getDefault();

    millis += timeZone.getOffset(millis);
    int integer = (int) (millis / MILLIS_IN_DAY);
    double fraction = (double) (millis % MILLIS_IN_DAY) / MILLIS_IN_DAY;
    integer += getInteger(2440587, 0.5);
    fraction += getFraction(2451910, 0.5);
    return (double) integer + fraction;
}

From source file:com.sirma.itt.emf.time.ISO8601DateFormat.java

/**
 * Format calendar instance into ISO format.
 * /*w w  w. ja  v  a  2 s  .  com*/
 * @param calendar
 *            the calendar instance to format
 * @return the ISO formatted string
 */
public static String format(Calendar calendar) {
    if (calendar == null) {
        return null;
    }

    StringBuilder formatted = new StringBuilder(28);
    padInt(formatted, calendar.get(Calendar.YEAR), 4);
    formatted.append('-');
    padInt(formatted, calendar.get(Calendar.MONTH) + 1, 2);
    formatted.append('-');
    padInt(formatted, calendar.get(Calendar.DAY_OF_MONTH), 2);
    formatted.append('T');
    padInt(formatted, calendar.get(Calendar.HOUR_OF_DAY), 2);
    formatted.append(':');
    padInt(formatted, calendar.get(Calendar.MINUTE), 2);
    formatted.append(':');
    padInt(formatted, calendar.get(Calendar.SECOND), 2);
    formatted.append('.');
    padInt(formatted, calendar.get(Calendar.MILLISECOND), 3);

    TimeZone tz = calendar.getTimeZone();
    int offset = tz.getOffset(calendar.getTimeInMillis());
    formatted.append(getTimeZonePadding(offset));

    return formatted.toString();
}

From source file:com.sirma.itt.emf.time.ISO8601DateFormat.java

/**
 * Format date into ISO format.//from   ww  w.  j a va 2 s  . c  o  m
 * 
 * @param isoDate
 *            the date to format
 * @return the ISO formatted string
 */
public static String format(Date isoDate) {
    if (isoDate == null) {
        return null;
    }
    // Note: always serialise to Gregorian Calendar
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(isoDate);

    StringBuilder formatted = new StringBuilder(28);
    padInt(formatted, calendar.get(Calendar.YEAR), 4);
    formatted.append('-');
    padInt(formatted, calendar.get(Calendar.MONTH) + 1, 2);
    formatted.append('-');
    padInt(formatted, calendar.get(Calendar.DAY_OF_MONTH), 2);
    formatted.append('T');
    padInt(formatted, calendar.get(Calendar.HOUR_OF_DAY), 2);
    formatted.append(':');
    padInt(formatted, calendar.get(Calendar.MINUTE), 2);
    formatted.append(':');
    padInt(formatted, calendar.get(Calendar.SECOND), 2);
    formatted.append('.');
    padInt(formatted, calendar.get(Calendar.MILLISECOND), 3);

    TimeZone tz = calendar.getTimeZone();
    int offset = tz.getOffset(calendar.getTimeInMillis());
    formatted.append(getTimeZonePadding(offset));

    return formatted.toString();
}

From source file:Main.java

/**
 * Formats a date in the xml xs:dateTime format, for mountain time.  This
 * method does not support any other timezone.
 *
 * @param calendar the calendar to format
 *
 * @return the formatted date// w  w w .ja v  a  2 s  .c  o  m
 */
public static String xsDateTimeFormatMountain(final Calendar calendar) {
    final SimpleDateFormat dateFormat;
    final SimpleDateFormat timeFormat;
    final TimeZone currentTimeZone;

    dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    timeFormat = new SimpleDateFormat("HH:mm:ss");
    currentTimeZone = TimeZone.getTimeZone("Canada/Mountain");
    final long offset;
    final long hours;
    final String tzID;
    offset = currentTimeZone.getOffset(calendar.getTimeInMillis());
    hours = -offset / (1000 * 60 * 60);
    tzID = "-0" + hours + ":00"; // assume single digit hour, we always are

    return dateFormat.format(calendar.getTime()) + 'T' + timeFormat.format(calendar.getTime()) + tzID;
}

From source file:Main.java

/**
 * Takes a calendar object representing a date/time, ignores its current time zone (which should be the default time zone)
 * applies that date/time to the sourceTimeZone and returns the relative date/time in the current time zone. 
 * /* w w w .j av a2  s  .  co m*/
 * For example, given an input of 13:00 EST and source time zone PST, it will return 16:00 EST 
 * 13:00 EST = 18:00 GMT = 10:00 PST
 *  
 * @param calendar
 * @param sourceTimeZone
 * @return
 */
public static Calendar convertToTimeZone(Calendar calendar, TimeZone sourceTimeZone) {
    Calendar result = Calendar.getInstance();
    // i.e., 13:00 EST becomes 08:00 GMT
    long originalTimeInUtc = calendar.getTimeInMillis()
            + calendar.getTimeZone().getOffset(calendar.getTimeInMillis());
    // 08:00 GMT becomes 16:00 PST
    long sourceTime = originalTimeInUtc - sourceTimeZone.getOffset(originalTimeInUtc);
    result.setTimeZone(sourceTimeZone);
    result.setTimeInMillis(sourceTime);
    /*
    Log.d(TAG, "Converting "+DEBUG_CALENDAR_OUTPUT.format(new Date(calendar.getTimeInMillis()))
        +" in ["+sourceTimeZone.getDisplayName()+"] to ["+TimeZone.getDefault().getDisplayName()
        +"] resulting in "+DEBUG_CALENDAR_OUTPUT_WITH_TIMEZONE.format(new Date(result.getTimeInMillis())));
    Log.d(TAG, "Original time in UTC = "+DEBUG_CALENDAR_OUTPUT.format(new Date(originalTimeInUtc)));
    Log.d(TAG, "Original time in source time zone = "+DEBUG_CALENDAR_OUTPUT.format(new Date(sourceTime)));
     */
    return result;
}