Java Time Format formatTimeOffset(long offset)

Here you can find the source of formatTimeOffset(long offset)

Description

format Time Offset

License

Apache License

Declaration

public static String formatTimeOffset(long offset) 

Method Source Code

//package com.java2s;
/*//  w  w w  .  j a  v a  2s .  co m
 * Copyright 2006-2012 Andrew Williams.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    public static String formatTimeOffset(long offset) {
        if (offset == 0)
            return "no time";
        long tmp = offset;
        StringBuffer ret = new StringBuffer();

        long sec = tmp % 60;
        timeOffsetPrepend(sec, "second", "", ret);
        tmp = tmp / 60;

        long min = tmp % 60;
        if (min > 0) {
            timeOffsetPrepend(min, "minute", " and ", ret);
            tmp = tmp / 60;

            long hour = tmp % 24;
            if (hour > 0) {
                timeOffsetPrepend(hour, "hour", ", ", ret);
                tmp = tmp / 24;

                long day = tmp % 365;
                if (day > 0) {
                    timeOffsetPrepend(day, "day", ", ", ret);
                    tmp = tmp / 365;

                    long year = tmp;
                    if (year > 0) {
                        timeOffsetPrepend(year, "year", ", ", ret);
                    }
                }
            }
        }
        return ret.toString();
    }

    private static void timeOffsetPrepend(long num, String prefix, String postfix, StringBuffer s) {
        s.insert(0, postfix);
        if (num != 1)
            s.insert(0, "s");
        s.insert(0, prefix);
        s.insert(0, " ");
        s.insert(0, num);
    }
}

Related

  1. formatTimeInterval(final long time)
  2. formatTimeInterval(long in)
  3. formatTimeLength(long ms)
  4. formatTimeLikeTimer(long time, boolean appendMs)
  5. formatTimeNicely(long millis)
  6. formatTimePart2(long number, String unit)
  7. formatTimePeriod(long millis)
  8. formatTimePeriod(long timePeriod)
  9. formatTimePeriod(long timestamp)