com.sky8the2flies.KOTH.util.TimeParser.java Source code

Java tutorial

Introduction

Here is the source code for com.sky8the2flies.KOTH.util.TimeParser.java

Source

package com.sky8the2flies.KOTH.util;

import org.apache.commons.lang.time.DurationFormatUtils;

/**
 * ======================================================================
 * Copyright sky8the2flies {c} 2014. All Rights Reserved.
 * Any code contained within this document, and any associated APIs with similar branding
 * are the sole property of sky8the2flies. Distribution, reproduction, taking snippets, or
 * claiming any contents as your own will break the terms of the License, and void and
 * agreements with you, the third party.
 * Thanks, sky8the2flies.
 * ======================================================================
 */
public class TimeParser {

    public static int parseToSeconds(String time) {
        int amount = 0;
        if (time.contains(":")) {
            String[] split = time.split(":");
            for (String bits : split) {
                amount = amount + parseTime(bits);
            }
        } else
            amount = parseTime(time);
        return amount;
    }

    public static String parseToTimeFormat(String time) {
        int millis = (parseToSeconds(time) * 1000);
        return DurationFormatUtils.formatDuration(millis, "H:mm:ss", true);
    }

    private static int parseTime(String time) {
        time = time.toLowerCase();
        String[] split;
        if (time.contains("w")) {
            split = time.split("w");
            return Integer.parseInt(split[0]) * 604800;
        } else if (time.contains("d")) {
            split = time.split("d");
            return Integer.parseInt(split[0]) * 86400;
        } else if (time.contains("h")) {
            split = time.split("h");
            return Integer.parseInt(split[0]) * 3600;
        } else if (time.contains("m")) {
            split = time.split("m");
            return Integer.parseInt(split[0]) * 60;
        } else if (time.contains("s")) {
            split = time.split("s");
            return Integer.parseInt(split[0]);
        }
        return 0;
    }
}