-4

Im working on a To-Do list program.

I want to make it for the user possible to input a date, time and regarding. I already have the date. What Im missing is the time.

DateTimeFormatter formateTime = DateTimeFormatter.ofPattern("HH:mm"); 
LocalDate timeNotFormated = null;
String time = "22:22";   //* here is the user input for example 22:22
timeNotFormated = LocalDate.parse(time, formateTime);
System.out.println(timeNotFormated);

But I get many exceptions. Is there something Im missing something?

Exception observed:

Exception in thread "main" java.time.format.DateTimeParseException: Text '22:22' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {},ISO resolved to 22:22 of type java.time.format.Parsed

The exception happens in the following line:

timeNotFormated = LocalDate.parse(time, formateTime);
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 4
    You'd have to paste the exceptions you get, obviously. – rzwitserloot Jun 12 '22 at 15:27
  • ? Sry Im pretty new in Java, what do you mean exactly? – PizzaE4ter Jun 12 '22 at 15:30
  • 3
    You said you get exceptions when running your code. The [Comment by rzwitserloot](https://stackoverflow.com/questions/72593525/java-user-input-time/72593592#comment128232703_72593525) asks that you copy-paste the text of the exception messages into the body of your Question. Most of us here reading your Question are not clairvoyant. – Basil Bourque Jun 12 '22 at 15:37
  • 1
    work through a tutorial on using the Date/Time classes.. – kleopatra Jun 12 '22 at 15:44
  • The `LocalDate` class is for a date without time of day. For the opposite, a time of day without date, you need a `LocalTime`. – Ole V.V. Jun 12 '22 at 16:34

1 Answers1

2

tl;dr

See Java Tutorials by Oracle.

LocalTime.parse( "23:45" ) 

LocalTime

The class LocalTime represents a time-of-day only, without a date, and without a time zone or offset.

ISO 8601

Your example string input complies with the ISO 8601 standard format. The java.time classes use ISO 8601 formats by default when parsing/generating text. So no need to provide a formatting pattern.

Example code

LocalTime lt = LocalTime.parse( "22:22" ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • thanks, that helped a lot. How can I parse the LocalTime to a string back? And can I catch exceptions if the user input something like 25:72 – PizzaE4ter Jun 12 '22 at 15:40
  • 2
    @PizzaE4ter Time for you to learn how to ***read the documentation***. Asking Stack Overflow to write each line of code for your app will become very tedious for both you and us. I provided a link to the Javadoc. Start by scanning that linked page for methods whose name contains the word “String”. Then read its description to judge if it does what you want. – Basil Bourque Jun 12 '22 at 15:43
  • 1
    @PizzaE4ter As for handling exceptions, ***read the documentation***. The Javadoc lists exceptions thrown by each method. That might be a good place to start. – Basil Bourque Jun 12 '22 at 15:47
  • 1
    @PizzaE4ter Oracle provides at no cost [*The Java Tutorials*](https://docs.oracle.com/javase/tutorial/index.html) with a [chapter on the *java.time* classes](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). Always study these tutorials before posting here. – Basil Bourque Jun 12 '22 at 15:51