Java TimeZone Create getTimeZoneCorrectedProgramId(String progID)

Here you can find the source of getTimeZoneCorrectedProgramId(String progID)

Description

Gets the time zone corrected program id of the given program id.

License

Open Source License

Parameter

Parameter Description
progID The id to get the time zone corrected for.

Return

The time zone corrected program id.

Declaration

public static String getTimeZoneCorrectedProgramId(String progID) 

Method Source Code

//package com.java2s;
/*/* ww w  . j  a  v  a 2 s. c o  m*/
 * TV-Browser
 * Copyright (C) 04-2003 Martin Oberhauser (martin@tvbrowser.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.util.TimeZone;

public class Main {
    /**
     * Gets the time zone corrected program id of the given program id.
     * If the current time zone is the same like the time zone of the given
     * id the given id will be returned.
     * <p>
     * @param progID The id to get the time zone corrected for.
     * @return The time zone corrected program id.
     * @since 2.7
     */
    public static String getTimeZoneCorrectedProgramId(String progID) {
        int index = progID.lastIndexOf('_');
        String timeString = progID.substring(index + 1);
        int hourIndex = timeString.indexOf(':');
        int offsetIndex = timeString.lastIndexOf(':');

        if (hourIndex != offsetIndex) {
            int timeZoneOffset = Integer.parseInt(timeString.substring(offsetIndex + 1));
            int currentTimeZoneOffset = TimeZone.getDefault().getRawOffset() / 60000;

            if (timeZoneOffset != currentTimeZoneOffset) {
                String[] hourMinute = timeString.split(":");
                int timeZoneDiff = currentTimeZoneOffset - timeZoneOffset;

                int hour = Integer.parseInt(hourMinute[0]) + (timeZoneDiff / 60);
                int minute = Integer.parseInt(hourMinute[1]) + (timeZoneDiff % 60);

                if (hour >= 24) {
                    hour -= 24;
                } else if (hour < 0) {
                    hour += 24;
                }

                hourMinute[0] = String.valueOf(hour);
                hourMinute[1] = String.valueOf(minute);
                hourMinute[2] = String.valueOf(currentTimeZoneOffset);

                StringBuilder newId = new StringBuilder(progID.substring(index + 1));
                newId.append(hourMinute[0]).append(":").append(hourMinute[1]).append(":").append(hourMinute[2]);

                return newId.toString();
            }
        } else {
            String[] hourMinute = timeString.split(":");
            StringBuilder newId = new StringBuilder(progID.substring(index + 1));
            newId.append(hourMinute[0]).append(":").append(hourMinute[1]).append(":")
                    .append(TimeZone.getDefault().getRawOffset() / 60000);

            return newId.toString();
        }

        return progID;
    }
}

Related

  1. getTimeZone(String timeZone)
  2. getTimeZone(String timeZone)
  3. getTimeZone(String timeZoneId)
  4. getTimezoneAbbreviation(String tzid, boolean dst)
  5. getTimeZoneChecked(String id)
  6. getTimeZoneDefault()
  7. getTimeZoneIds()
  8. getTimeZoneList()
  9. getTimezoneList()