Today, for the first time, I am trying to get the right time through the API. I don’t understand‚ please help.
I am using the http://worldtimeapi.org/api/timezone/ site to determine the exact time. But I didn’t understand how to use it in JAVA and what codes to write. Please send me a snippet of code or codes. I don’t know what codes to write. Thank you!
Advertisement
Answer
What are you trying to accomplish exactly? Where did you run into issues?
- If you want to get the current time, you can use the default Java libraries such as
java.time
for this (seejava.time.LocalDateTime
orjava.LocalTime
). Since you’re not guaranteed to get the correct time with an API anyway – say the server is overloaded and response takes a long time. See https://mkyong.com/java/java-how-to-get-current-date-time-date-and-calender/ - If your goal is to just learn to send HTTP requests you can do so with the
java.net
package. See https://www.baeldung.com/java-http-request or https://www.baeldung.com/java-9-http-client if using a more modern version of Java