Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6025009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:14:22+00:00 2026-05-23T04:14:22+00:00

I have two String variables – time1 and time2 . Both contain value in

  • 0

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);
clTime1.setMonth(now.get(Calendar.MONTH));
clTime1.setDate(now.get(Calendar.DAY_OF_MONTH));
System.out.println(clTime1.toString());
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:14:23+00:00Added an answer on May 23, 2026 at 4:14 am
    • Convert the two strings to Date
      objects (which are also time objects)
      Create a new Date object.
    • This will
      contain the current time.
    • Use the
      Date.before() and Date.after() methods to determine if
      you are in the time interval.

    EDIT: You should be able to use this directly (and no deprecated methods)

    public static final String inputFormat = "HH:mm";
    
    private Date date;
    private Date dateCompareOne;
    private Date dateCompareTwo;
    
    private String compareStringOne = "9:45";
    private String compareStringTwo = "1:45";
    
    SimpleDateFormat inputParser = new SimpleDateFormat(inputFormat, Locale.US);
    
    private void compareDates(){
        Calendar now = Calendar.getInstance();
    
        int hour = now.get(Calendar.HOUR);
        int minute = now.get(Calendar.MINUTE);
    
        date = parseDate(hour + ":" + minute);
        dateCompareOne = parseDate(compareStringOne);
        dateCompareTwo = parseDate(compareStringTwo);
    
        if ( dateCompareOne.before( date ) && dateCompareTwo.after(date)) {
            //yada yada
        }
    }
    
    private Date parseDate(String date) {
    
        try {
            return inputParser.parse(date);
        } catch (java.text.ParseException e) {
            return new Date(0);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two string variables which are both file paths. The code that worked
If I have two variables: Object obj; String methodName = getName; Without knowing the
I have an ugly problem. I have two string variables (className and staticMethod) store
In java i have two variables String string; int integerValue; I want to convert
I have two String.printable mysteries in the one question. First, in Python 2.6: >>>
I'm using .NET 3.5. I have two string arrays, which may share one or
I have two complex (i.e. objects with string, int, double, List and other home
Say I have two strings, String s1 = AbBaCca; String s2 = bac; I
I have a function which parses one string into two strings. In C# I
I have a class that contains two methods like these: public String getFoo(Int32 a)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.