Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.util.Log;

import java.util.Locale;

public class Main {
    private static final String TAG = "Helper";

    /**
     * Converts time to a string, e.g. "1:59:30.1"
     * or "3.6" or "5:33.2".
     * Rolled my own because JDK 7's DateFormat class seemed
     * to require some unnatural contortions. JDK 8 has a much
     * richer library.
     *
     * @param milliseconds Time in millseconds since start of meeting
     * @return String formatted time interval string in "H:MM:SS.m" format.
     */
    public static String timeToHMMSSm(long milliseconds) {
        Log.v(TAG, "timeToHMMSSm(" + milliseconds + ")");

        double seconds = (milliseconds % 60_000) / 1000.0;
        int minutes = (int) (milliseconds / 60_000) % 60;
        int hours = (int) (milliseconds / 3600_000);

        String hms;
        if (hours >= 1) {
            hms = String.format(Locale.getDefault(), "%d:%02d:%04.1f", hours, minutes, seconds);
        } else if (minutes >= 1) {
            hms = String.format(Locale.getDefault(), "%d:%04.1f", minutes, seconds);
        } else {
            hms = String.format(Locale.getDefault(), "%1.1f", seconds);
        }
        return hms;
    }
}