Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.Calendar;

public class Main {
    private static final String[] AM_PM = { "AM", "PM" };

    /**
     * Takes in a duration in minutes and a start timestamp and returns
     * the formatted time that is timestamp + duration
     */
    public static String getTimeAfterStart(long startTime, int duration) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(startTime);
        calendar.add(Calendar.MINUTE, duration);

        int minute = calendar.get(Calendar.MINUTE);
        String minuteString = "";
        if (minute < 10)
            minuteString = "0" + minute;
        else
            minuteString = "" + minute;
        String timeString = calendar.get(Calendar.HOUR) + ":" + minuteString + " "
                + AM_PM[calendar.get(Calendar.AM_PM)];
        return timeString;
    }
}