How to compare current time with time range?

postgres date comparison
python datetime
compare date with current date in java
compare date with current date in java 8
how to compare current date and time with another date and time in android
java date format
extract date from timestamp python
how to compare time in java

I have two String variables - time1 and time2. Both contain value in the format HH:MM. How can I check:

  1. If the current time is within time1 and time2?
  2. time1 will happen in the nearest hour?

Upd. I've implemented the following to convert time1 to Date format. But it uses depreciated methods:

Date clTime1 = new Date();

SimpleDateFormat timeParser = new SimpleDateFormat("HH:mm", Locale.US);
try {
  clTime1 = timeParser.parse(time1);
} catch (ParseException e) {

Calendar now = Calendar.getInstance();
clTime1.setYear(now.get(Calendar.YEAR) - 1900);

7.1. datetime — Basic date and time types, Any log message with a parsed timestamp outside of that window is automatically re-stamped with the current time. You must contact Sumo Logic  Next, convert the time to numerical value, by dividing the time (in hour) by 24 hours. The reason is that the underlying value of time is a numerical value, value 1 means 24:00 on date 0/1/1900. Click here to see my previous post to learn more. Therefore, the condition in Excel formula should be expressed as.

SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");

Date EndTime = dateFormat.parse("10:00");

Date CurrentTime = dateFormat.parse(dateFormat.format(new Date()));

if (CurrentTime.after(EndTime))
    System.out.println("timeeee end ");

Don't forget to surrounded with a try catch block

Timestamps, Time Zones, Time Ranges, and Date Formats, Display Current Date in Java; SimpleDateFormat: Parse and Format Dates; Compare Dates Example. Let us first understand the parameters  When you go over 24 hours, Excel counts it as the next day and then the TIMEVALUE is the time the next day (i.e. 00:58:36 and 02:11:18 in your examples) and can, therefore, be before 0900. You could do DATEVALUE(K7)+TIMEVALUE(K7) to ensure that you count the day part too

this is what i used and it worked for me:

public static boolean isTimeWith_in_Interval(String valueToCheck, String endTime, String startTime) {
    boolean isBetween = false;
    try {
        Date time1 = new SimpleDateFormat("HH:mm:ss").parse(endTime);

        Date time2 = new SimpleDateFormat("HH:mm:ss").parse(startTime);

        Date d = new SimpleDateFormat("HH:mm:ss").parse(valueToCheck);

        if (time1.after(d) && time2.before(d)) {
            isBetween = true;
    } catch (ParseException e) {
    return isBetween;

Java Date & Time: SimpleDateFormat, Current Date & Compare, current_timestamp, timestamp with time zone, Current date and time (start of current Each time period is considered to represent the half-open interval start​  For example, I have two columns of times which are start time and end time in column A and column B separately, now I want to check if the time in column C is between the time range in column A and column B as below screenshot shown: The following formulas may help you to identify if the specific time is between two times, please do as this:

Look into the Calendar class. It has the methods to support what you are asking. Date is deprecated and not recommended to use.

Here is the link to the API. Calendar

About the usage. First you need to call Calendar.getInstance() to create a calendar object. Next you need to Set the two fields using cal.set(Calendar.HOUR, your hours) and Calendar.MINUTES the same way. Next you can call the compare function, before or after functions to get the desired info. Also you can get an instance with the current time in the current locale.

Documentation: 9.1: Date/Time Functions and Operators, Generates time spans, ranges, floors and ceilings for time frames ranging from microsecond to year Defaults to now in the current Arrow object's timezone. public static bool IsBetween(this DateTime now, TimeSpan start, TimeSpan end) { var time = now.TimeOfDay; // If the start time and the end time is in the same day. if (start <= end) return time >= start && time <= end; // The start time and end time is on different days. return time >= start || time <= end; } share.

For example if you want to compare time between 11pm to 6am for calculating extra night fare for any vechicle. then following code will help you.

// Code

package com.example.timedate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import android.os.Bundle;
import android.text.format.DateFormat;
import android.text.format.Time;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    TextView tv;
    Button bt;
    int hour,min;
    String AM_PM;
    protected void onCreate(Bundle savedInstanceState) {
        tv = (TextView)findViewById(;
        bt = (Button)findViewById(;

        final String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());*/

        bt.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                Calendar c = Calendar.getInstance();
                hour = c.get(Calendar.HOUR_OF_DAY);
                min = c.get(Calendar.MINUTE);
                int ds = c.get(Calendar.AM_PM);

                Toast.makeText(MainActivity.this, ""+hour+":"+min+AM_PM, Toast.LENGTH_SHORT).show();
                if((hour==11&&AM_PM.matches("pm")) || (hour<7&&AM_PM.matches("am"))  || (hour==12&&AM_PM.matches("am")))
                    Toast.makeText(MainActivity.this, "Time is between the range", Toast.LENGTH_SHORT).show();
                    Toast.makeText(MainActivity.this, "Time is not between the range", Toast.LENGTH_SHORT).show();



    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;


Arrow: Better dates & times for Python, See Section 11.2, “Date and Time Data Types”, for a description of the range of Functions that return the current date or time each are evaluated only once per  You can use the time () method of datetime objects to get the time of day, which you can use for comparison without taking the date into account: >>> this_morning = datetime.datetime(2009, 12, 2, 9, 30) >>> last_night = datetime.datetime(2009, 12, 1, 20, 0) >>> this_morning.time() < last_night.time() True. share.

MySQL 8.0 Reference Manual :: 12.6 Date and Time , Overview of Datetime and Interval Data Types and Time Zone Support TIMESTAMP WITH LOCAL TIME ZONE does not store time zone information internally, When you compare date and timestamp values, Oracle Database converts the  compare time values - current time with time in cell range? I have a time in a cell that might be one of a few times, like 10:30 or 18:30 or 02:30. I need to write an if statement that returns true if the current time on the computer is within 2 hours of the time in the cell (so for 18:30, it returns true if its 16:30 to 18:29), otherwise just

Datetime Data Types and Time Zone Support, If a set of incoming timestamps has no time zone specified, then Snowflake loads those strings assuming the timestamps represent local time in the set time zone. The function returns the day of the week as an integer value in the range 1-7, where For example, without explicit casting, comparing string values produces​  How can I compare current time with two given time ranges in a text box.C# Solution 1. Do not keep dates and times as strings; always use DateTime objects, and your comparisons and calculations Solution 2. Richard is spot on about the storage: always use DateTime. Note that you will want to

Using Dates and Timestamps, TimeSpan represents a time interval (duration of time or elapsed The DateTime is set to the current local date and time on this computer. I am having trouble grasping how to properly check from a bash script if the current time is between 23:00 and 06:30. I am trying to run an infinite loop to check the time now, and to do something if the time range is between 11pm and 6:30 am.

  • If I use java.sql.Time, then I can easily get time value from my strings - Time.valueOf(time1).getTime(). But how should I get current time in the same format (long)?
  • @CaspNZ, I've tried to do so, but didn't get how to convert string in my format (not like 20081013T160000Z) to Date. Can you help me with this?
  • @DArkO - it looks like some of the Date constructors are deprecated, but using a parser in my example above isn't deprecated - I think this is still supported.
  • @CaspNZ, ok, I have my strings now as Date: Jan 1, 1970 + my time1. How can I get current time with date equal to Jan 1, 1970 also?
  • make a new inputParser with a string of 'yyyy-MM-dd' and parse in '1970-01-01'. As you've left the time off, it'll fill it with the current time for you.
  • That does seem a little hackish, however. Another way would be to create a new empty date and extract the current Date and Year values from it and add them to your initial dates when you parse them in (you'd have to change your input format string).
  • If checking for a time within a range, you'll need to account for the overflow scenario. That is, the start time is later in the day than the stop time. Actually this situation is true regardless of the coding approach. I'm starting to like your solution for recurring daily time range checks.