Skip to content

Instantly share code, notes, and snippets.

@gauravkukade
Created May 15, 2021 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gauravkukade/8ca11fd8b4652f33932b66a518c5775d to your computer and use it in GitHub Desktop.
Save gauravkukade/8ca11fd8b4652f33932b66a518c5775d to your computer and use it in GitHub Desktop.
A java program to get local time using the java.time.LocalTime class. Check the blog post at https://coderolls.com/get-current-date-time-in-java/
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
* A java program to get local time using the java.time.LocalTime class
* @author Gaurav Kukade at coderolls.com
*/
public class LocalTimeExample {
public static void main(String[] args) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime localTime = LocalTime.now();
// print localTime object
System.out.println(localTime); // 13:11:46.140359217
// print formatted localTime
System.out.println(dateTimeFormatter.format(localTime)); // 13:11:46
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment