-1

I have a textfield in Java eclipse interface for which i want to validate it to accept only time format, but I don't want seconds included.

How do I validate it to accept only this format hh:mm but from hours 8:00 morning till 16:00 afternoon.

P.s TextField variable name is txtOra.

YCF_L
  • 51,266
  • 13
  • 85
  • 129
Lorik Berisha
  • 233
  • 1
  • 2
  • 16
  • 1
    Welcome on Stack Overflow. I recommend you to [take the tour](https://stackoverflow.com/tour) and read the [Help Center](https://stackoverflow.com/help) for information on asking a good and well-received question. You are missing a couple points there. – Ben May 28 '18 at 12:32
  • Which graphic framework are you using? Is it swing, awt, or maybe android? Are you sure it is `TextField` or maybe it is `JTextField` (these are not the same and solution may depend on it). – Pshemo May 28 '18 at 12:36
  • 1
    Possibly related: https://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html, https://docs.oracle.com/javase/8/docs/technotes/guides/swing/1.4/ftf.html – Pshemo May 28 '18 at 12:39
  • [Check this](https://stackoverflow.com/questions/25873636/regex-pattern-for-exactly-hhmmss-time-string) Pattern: ```([8-9]|1[0-6]):([0-5][0-9])``` – Andry May 28 '18 at 12:40
  • This question has been asked and answered with variations many times. For example [Regex pattern for EXACTLY HH:MM:SS time String](https://stackoverflow.com/questions/25873636/regex-pattern-for-exactly-hhmmss-time-string). – Ole V.V. May 28 '18 at 13:04

3 Answers3

3

Just parse it with a DateTimeFormatter

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("H:mm");

LocalTime localTime = LocalTime.parse("16:16", dateTimeFormatter);

if parse does not throw an exception you have a valid time.

Then check for your constraints with LocalDate#isAfter and LocalDate#isBefore

Look it up here to find more patterns https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

Zarathustra
  • 2,783
  • 4
  • 30
  • 60
1

If you are using java.time you can use LocalTime where you can use isBefore and isAfter like so :

public static boolean checkDateIsCorrect(String input){
    //Format your date
    LocalTime time = LocalTime.parse(input, DateTimeFormatter.ofPattern("H:mm"));
    //Then check if the time is between 8:00 and 16:00
    return !time.isBefore(LocalTime.parse("08:00")) 
                || !time.isBefore(LocalTime.parse("16:00"));
}
YCF_L
  • 51,266
  • 13
  • 85
  • 129
  • `H` alone can accept 8 as well as 08 (so you don’t need optional parts). Your method rejects times lying on the limits 08:00 and 16:00, which I suspect wasn’t intended from the asker. – Ole V.V. May 28 '18 at 12:58
  • @OleV.V. I don't understand *Your method rejects times lying on the limits 08:00 and 16:00, which I suspect wasn’t intended from the asker.* – YCF_L May 28 '18 at 13:02
  • What is the expected result of `checkDateIsCorrect("8:00")`? What is the actual result? Same question for `"16:00"`. – Ole V.V. May 28 '18 at 13:06
  • @OleV.V. I got it, what about now? – YCF_L May 28 '18 at 13:07
  • If you want that way of solving it, I think you need 16:01. – Ole V.V. May 28 '18 at 13:08
  • Similar to [my answer here](https://stackoverflow.com/a/50564365/5772882) – Ole V.V. May 28 '18 at 13:11
  • Yes, your way not need to use `7:59` and `16:01` I like it I will go with that one instead thank you @OleV.V. – YCF_L May 28 '18 at 13:15
0

Use the following function to validate a time string with HH:MM (24 Hours Format)

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public boolean validate(final String time){
      String TIME24HOURS_PATTERN = 
                "([01]?[0-9]|2[0-3]):[0-5][0-9]";
      Pattern pattern = Pattern.compile(TIME24HOURS_PATTERN);
      Matcher matcher = pattern.matcher(time);
      return matcher.matches();

}
nandal
  • 2,434
  • 1
  • 16
  • 23