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 java.util.Calendar;

public class Main {
    /**
     * Determines if the current time is between fromMillis and toMillis
     * Does so by calculating an inverse time range based on the minutes
     *
     * @param currentMillis current time in milliseconds
     * @param fromMillis    start time in milliseconds
     * @param toMillis      end time in milliseconds
     * @return whether or not the currentMillis is between fromMillis and toMillis
     */
    static boolean isTimeInRange(long currentMillis, long fromMillis, long toMillis) {
        final Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(currentMillis);

        final int currentMinuteOfDay = calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE);
        calendar.setTimeInMillis(fromMillis);

        final int fromMinuteOfDay = calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE);
        calendar.setTimeInMillis(toMillis);

        final int toMinuteOfDay = calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE);

        if (fromMinuteOfDay <= toMinuteOfDay) {
            return (currentMinuteOfDay >= fromMinuteOfDay && currentMinuteOfDay < toMinuteOfDay);
        } else {
            return (currentMinuteOfDay >= fromMinuteOfDay || currentMinuteOfDay < toMinuteOfDay);
        }
    }
}