Java Time Format formatTime(Integer hour, Integer minute, Integer second, Integer ampm, Integer zoneOffset)

Here you can find the source of formatTime(Integer hour, Integer minute, Integer second, Integer ampm, Integer zoneOffset)

Description

format Time

License

Open Source License

Declaration

public static final String formatTime(Integer hour, Integer minute, Integer second, Integer ampm,
            Integer zoneOffset) 

Method Source Code

//package com.java2s;
/*/*w  ww .j  a va 2s . co m*/
Copyright 2009 Semantic Discovery, Inc. (www.semanticdiscovery.com)
    
This file is part of the Semantic Discovery Toolkit.
    
The Semantic Discovery Toolkit is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
The Semantic Discovery Toolkit 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 Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public License
along with The Semantic Discovery Toolkit.  If not, see <http://www.gnu.org/licenses/>.
*/

public class Main {
    public static final String formatTime(Integer hour, Integer minute, Integer second, Integer ampm,
            Integer zoneOffset) {
        final StringBuilder result = new StringBuilder();

        int hour24 = (hour == null) ? 0 : hour.intValue();
        if (ampm != null) {
            // am=0, pm=1
            hour24 %= 12;
            if (ampm == 1)
                hour24 += 12;
        }

        result.append(asDigits(hour24, 2)).append(':').append(minute == null ? "00" : asDigits(minute, 2));

        if (second != null) {
            result.append(':').append(asDigits(second, 2));
        }

        if (zoneOffset != null)
            result.append(' ').append(getTimezoneString(zoneOffset));

        return result.toString();
    }

    public static final String asDigits(int number, int minNumDigits) {
        final StringBuilder result = new StringBuilder();

        result.append(Integer.toString(number));
        final int len = result.length();
        for (int i = minNumDigits - len; i > 0; --i)
            result.insert(0, '0');

        return result.toString();
    }

    public static final String getTimezoneString(int zoneOffset) {
        final StringBuilder result = new StringBuilder();
        result.append("GMT");
        if (zoneOffset != 0) {
            final String offsetString = Integer.toString(zoneOffset);
            if (zoneOffset > 0)
                result.append('+');
            result.append(offsetString);
        }
        return result.toString();
    }
}

Related

  1. formatTime(int time)
  2. formatTime(int time)
  3. formatTime(int time)
  4. formatTime(int time)
  5. formatTime(int value)
  6. formatTime(Integer seconds)
  7. formatTime(long dtNanos)
  8. formatTime(long elapsedTime)
  9. formatTime(long elapsedTime)