net.kamhon.ieagle.util.TimeUtil.java Source code

Java tutorial

Introduction

Here is the source code for net.kamhon.ieagle.util.TimeUtil.java

Source

/*
 * Copyright 2012 Eng Kam Hon (kamhon@gmail.com)
 * 
 * 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.
 */
package net.kamhon.ieagle.util;

import java.text.DecimalFormat;

import net.kamhon.ieagle.exception.ValidatorException;

import org.apache.commons.lang.StringUtils;

public class TimeUtil {
    public static String toTime(int hours, int minutes, int seconds) {
        DecimalFormat df = new DecimalFormat("00");

        return df.format(hours) + ":" + df.format(minutes) + ":" + df.format(seconds);
    }

    public static String toTime(int hours, int minutes) {
        return toTime(hours, minutes, 0);
    }

    public static int[] parseTime(String time) {
        String[] ss = StringUtils.split(time, ":");
        if (ss == null || ss.length != 3) {
            throw new ValidatorException("Invalid time format");
        }

        return new int[] { Integer.parseInt(ss[0]), Integer.parseInt(ss[1]), Integer.parseInt(ss[2]) };
    }
}