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

  • SEARCH
  • Home
  • 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 3956090
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:19:11+00:00 2026-05-20T02:19:11+00:00

I have a window that contains a HH:mm time TextField in it, in 24

  • 0

I have a window that contains a HH:mm time TextField in it, in 24 hours format

I need to validate if the user entered any non valid hour, like 28:00, 99:00, 24:01.

What’s the best and simpler way to do that ?

some code below of what is currently doing that job wrong and giving errors in date parsed.
Today I get an random hour and an user hit 99:99 in that text field.

This code is not mine, but I gotta fix it.

I am stuck with it, tried to validate as a String is useless, and I cannot find a nice way to make it a Date without having to put year, month, etc… too.

Please forget about the return -1 instead of throwing an exception this is old code and this cannot be changed.

to help understand :
Statics.hF2 = SimpleDateFormat (HH:mm)
this.cmpHora.getText() = Is the field with the value
Statics.df_ddmmyy = Another date format
Statics.m2ms = converts minutes to milliseconds

//CODE

public long getDataEmLong ()
{
   try
   {
      Calendar hour= Calendar.getInstance();
      new GregorianCalendar().
      hour.setTime( Statics.hF2.parse( this.cmpHora.getText() ) );
      return Statics.df_ddmmyy.parse( this.cmpData.getText() ).getTime() + Statics.m2ms( hour.get( Calendar.HOUR_OF_DAY ) * 60 ) + Statics.m2ms( hour.get( Calendar.MINUTE ) );
   } catch ( Exception e )
   {
      e.printStackTrace();
      return -1;
   }

}

Cheers !

  • 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-20T02:19:11+00:00Added an answer on May 20, 2026 at 2:19 am

    Insert this in your class, and perform the validateTime method from inside your junk code.

    public boolean validateTime(String timeString) {
        if (timeString.length() != 5) return false;
        if (!timeString.substring(2, 3).equals(":")) return false;
        int hour = validateNumber(timeString.substring(0, 2));
        int minute = validateNumber(timeString.substring(3));
        if (hour < 0 || hour >= 24) return false;
        if (minute < 0 || minute >= 60) return false;
        return true;
    }
    
    public int validateNumber(String numberString) {
        try {
            int number = Integer.valueOf(numberString);
            return number;
        } catch (NumberFormatException e) {
            return -1;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a window that contains one QTMovieView. How do I make it so
I have a window that contains several rather complex views. Right now, I'm using
I'm running into a problem where, I have a Window that contains a child
I have a model window that contains expander control. This expander control when expanded
I have an editor which is in my window that contains a wrapping iframe
I have a window that uses a viewmodel. This screen contains 2 listviews on
i have a windows form that contains a bunch of textboxes. I need a
I have an app that has a main window that contains a bunch of
In my C# Windows Forms application, I have a user control that contains other
So I have a simple window which contains only a XamDataGrid that holds preview

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.