Java TimeUnit Usage createTimezoneString(String timezoneID)

Here you can find the source of createTimezoneString(String timezoneID)

Description

Taking a timezone ID, this method returns the offset in hours.

License

Apache License

Parameter

Parameter Description
timezoneID The ID of the timezone to support the hours offset for.

Return

A textual representation of the hours offset for the supported timezone.

Declaration

private static String createTimezoneString(String timezoneID) 

Method Source Code


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

import org.joda.time.DateTimeZone;

import java.util.concurrent.TimeUnit;

public class Main {
    /**//from   w  w  w.j av  a  2  s.  co  m
     * Taking a timezone ID, this method returns the offset in hours.
     *
     * @param timezoneID The ID of the timezone to support the hours offset for.
     * @return A textual representation of the hours offset for the supported timezone.
     */
    private static String createTimezoneString(String timezoneID) {
        StringBuilder builder = new StringBuilder();

        DateTimeZone timeZone = DateTimeZone.forID(timezoneID);
        long offsetInMilliseconds = timeZone.toTimeZone().getRawOffset();
        long offsetHours = TimeUnit.MILLISECONDS.toHours(offsetInMilliseconds);

        if (offsetHours == 0) {
            builder.append("Z");
        } else {
            if (offsetHours < 0) {
                // Negative
                builder.append("-");

                if (offsetHours < -9) {
                    builder.append(Math.abs(offsetHours));
                } else {
                    builder.append(0).append(Math.abs(offsetHours));
                }
            } else {
                // Positive
                if (offsetHours > 9) {
                    builder.append(offsetHours);
                } else {
                    builder.append(0).append(offsetHours);
                }
            }
            builder.append(":00");
        }

        return builder.toString();
    }
}

Related

  1. convertMillis(long millis)
  2. convertMillisecondsToHHMMSS(long milliseconds)
  3. convertNanosecondTimespanToHumanReadableFormat(long aTimespan, boolean aShortFormat, boolean aLongFormat)
  4. convertNanoToSeconds(long nanoTime)
  5. createTimeString(int year, int month, int day, int hours, int minutes, int seconds, String timezoneID)
  6. currentMillisFromNanotime()
  7. dateDiff(Date d1, Date d2)
  8. dateStart(final Calendar c)
  9. daysBetween(Calendar startDate, Calendar endDate)