2

I want the user to input hours and minutes for a Local.Time from 00 to 23 and from 00 to 59, I scanned this as an int. It works but for values from 00 to 09 the int ignores the 0 and places then as a 0,1,2...9 instead of 00,01,02,03...09; this breaks the Local.Time since, for example "10:3"; is not a valid format for time.

I have read I can format this as a String, but I don't think that helps me since I need an int value to build the LocalTime and subsequent opperations with it.

There is a way of formatting this while kepping the variable as an int?? Can I code this differently to bypass this?? Am I wrong about how these classes work??

I am pretty new to these concepts

Here is the code I am using

int hours;
int minutes;

System.out.println("Input a number for the hours (00-23): ");
hours = scan.nextInt();

System.out.println("Input a number for the minutes (00-59): ");
minutes = scan.nextInt();

LocalTime result = LocalTime.parse(hours + ":" + minutes);

I tried using the NumberFormat class but it returns an error when trying to declare its variables (something like it is an abstract variable and cannot be instanced)

I also tried using the String format but I don't really know what to do with that string after that, it asks me for a int and not a string to build this method

  • 5
    Why not use `LocalTime.of(int hour, int minute)` instead of `parse`? https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html#of-int-int- – chptr-one Dec 02 '22 at 09:20

3 Answers3

5

First: an int doesn't differentiate between 09 and 9. They are the same value: the number nine.

Next: if you already have numbers, then going back to a string to produce a date is an anti-pattern: you are losing type checking by this. So instead of using LocalTime.parse and constructing the input from int values, you should simply use LocalTime.of(hours, minutes):

LocalTime result = LocalTime.of(hours, minutes);
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 1
    This is the most simple and frankly I don't know what I was ussing parse, I just saw it somewhere else; what is the intent of the parse method then? is just to use it with a formatter? – Daleo dorito Dec 02 '22 at 09:41
  • 2
    `parse` makes sense when you already get a `String` from somewhere, for example in some JSON API. It's just another tool in the box: it's not bad, but each tool has specific situations where it's the best choice. `parse` just happened to be the wrong choice for your specific situation. – Joachim Sauer Dec 02 '22 at 09:43
  • Some other users in some other situation might prefer to enter the time as `10:03` or even `10:3` rather than two numbers after each other. In that case use `DateTimeFormatter.ofPattern("H:m")` and the two-arg `LocalTime.parse(CharSequence, DateTimeFormatter)` method (explained better in [azro’s answer](https://stackoverflow.com/a/74653463/5772882)). – Ole V.V. Dec 02 '22 at 21:55
5

tl;dr Use LocalTime.of(hours, minutes), it's most straight-forward

Alternative: Parse with a suitable DateTimeFormatter:

public static void main(String[] args) {
    // single-digit example values
    int hours = 9;
    int minutes = 1;
    // define a formatter that parses single-digit hours and minutes
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("H:m");
    // use it as second argument in LocalTime.parse
    LocalTime result = LocalTime.parse(hours + ":" + minutes, dtf);
    // see the result
    System.out.println(result);
}

Output:

09:01
deHaar
  • 17,687
  • 10
  • 38
  • 51
1

Fix

  • use the proper DateTimeFormatter

    LocalTime.parse(hours + ":" + minutes, DateTimeFormatter.ofPattern("H:m"));
    
  • Build the expected default format

    LocalTime.parse(String.format("%02d:%02d", hours, minutes));
    

Improve

Use a more appropriate method

LocalTime.of(hours, minutes);
azro
  • 53,056
  • 7
  • 34
  • 70