?
Solved

Validating 4 digit string as time?

Posted on 1999-12-13
3
Medium Priority
?
194 Views
Last Modified: 2010-04-16
Hi,
Is there an easy way to validate if a four char string is a valid time?

ie. 08:00 is 8h00 but 80:00 is not valid?

Thnx in Advance
0
Comment
Question by:Veroland
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
boycey19 earned 300 total points
ID: 2276706
What you could do is use a substring of the 4 character string and cast that to an int and chaeck to see if it is greater than 24 for the hours and 59 for the minutes. Here's a sample.

String time = "08:00";

int hour = Integer.parseInt(time.substring(0, 2));

int hour = Integer.parseInt(time.substring(3, 5));

the validate the times using.

if(hour > 24)
  System.out.println("Invalid time");

if(min > 59)
  System.out.println("Invalid time");

0
 
LVL 3

Expert Comment

by:boycey19
ID: 2276710
oops mistake. Rename the second variable hour to min.

Soz

Boycey
0
 
LVL 7

Expert Comment

by:vladi21
ID: 2276714
not simple way:
use Regular expressions :)
http://www.quateams.com/oro/software/OROMatcher1.1.html

0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month5 days, 10 hours left to enroll

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question