Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

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

import java.util.Locale;

public class Main {
    /**
     * Convert the current time in milliseconds to readable string as given format.
     * Notice: The time is counted since January 1, 1970 00:00:00.0 UTC.
     * @param format
     *     y year
     *     M month in the year
     *     d day
     *     h hours(12)
     *     H hours(24)
     *     m minute
     *     s second
     *     S millisecond
     *     E weekday
     *     D days in the year
     */
    public static String convertMillisTime(long time_millis, String format) {
        if (time_millis <= 0) {
            return "";
        }

        String result = "";
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.US);
            result = dateFormat.format(new Date(time_millis));
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * Convert the current time in milliseconds to readable string
     * as default format ("yyyy-MM-dd HH:mm:ss").
     */
    public static String convertMillisTime(long time_millis) {
        return convertMillisTime(time_millis, "yyyy-MM-dd HH:mm:ss");
    }
}