Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.text.SimpleDateFormat;

import java.util.Calendar;

public class Main {
    /**
     * Returns a string formatted time
     *
     * @return String of formatted time.
     */
    static public String getTimeAsString(Calendar c, String format) {
        if (format.isEmpty()) {
            format = "hh:mm Hours";
        }

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        return sdf.format(c.getTimeInMillis());
    }

    static public String getTimeAsString(int hourOfDay, int minute, String format) {
        return getTimeAsString(getTimeFromInt(hourOfDay, minute), format);
    }

    static public String getTimeAsString(long time, String format) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);

        return getTimeAsString(c, format);
    }

    static public Calendar getTimeFromInt(int hourOfDay, int minute) {
        Calendar c = Calendar.getInstance();

        c.set(Calendar.HOUR_OF_DAY, hourOfDay);
        c.set(Calendar.MINUTE, minute);

        return c;
    }
}