package me.ycdev.androidlib.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateTimeUtils {
    /**
     * Generate file name from system time in the format "yyyyMMdd-HHmmss-SSS",
     * @param sysTime System time in milliseconds
     */
    public static String generateFileName(long sysTime) {
        return new SimpleDateFormat("yyyyMMdd-HHmmss-SSS", Locale.US).format(new Date(sysTime));
    }

    /**
     * Format the time usage to string like "1d17h37m3s728ms"
     * @param timeUsageMs
     * @return
     */
    public static String getReadeableTimeUsage(long timeUsageMs) {
        long millisecondsLeft = timeUsageMs % 1000;
        if (timeUsageMs == millisecondsLeft) {
            return millisecondsLeft + "ms";
        }

        long seconds = timeUsageMs / 1000;
        long secondsLeft = seconds % 60;
        if (secondsLeft == seconds) {
            return secondsLeft + "s" + millisecondsLeft + "ms";
        }

        long minutes = seconds / 60;
        long minutesLeft = minutes % 60;
        if (minutesLeft == minutes) {
            return minutesLeft + "m" + secondsLeft + "s" + millisecondsLeft + "ms";
        }

        long hours = minutes / 60;
        long hoursLeft = hours % 24;
        if (hoursLeft == hours) {
            return hoursLeft + "h" + minutesLeft + "m" + secondsLeft + "s" + millisecondsLeft + "ms";
        }

        long days = hours / 24;
        return days + "d" + hoursLeft + "h" + minutesLeft + "m" + secondsLeft + "s" + millisecondsLeft + "ms";
    }
}

@@ -29,16 +29,49 @@
 utils;%0A%0A
+import java.text.ParseException;%0A
 import j
@@ -479,24 +479,364 @@
 e));%0A    %7D%0A%0A
+    /**%0A     * Parse system time from string in the format %22yyyyMMdd-HHmmss-SSS%22,%0A     * @param timeStr Time string in the format %22yyyyMMdd-HHmmss-SSS%22%0A     */%0A    public static long parseFileName(String timeStr) throws ParseException %7B%0A        return new SimpleDateFormat(%22yyyyMMdd-HHmmss-SSS%22, Locale.US).parse(timeStr).getTime();%0A    %7D%0A%0A
     /**%0A    

Parse file name in time string