Created
February 6, 2019 10:38
-
-
Save alonsoir/ee499c7ae1c563aaec3ca42a2beaa52b to your computer and use it in GitHub Desktop.
CURRENT TIME IN HH:MM:SS WITH JAVA 8+ AND DATETIMEFORMATTERBUILDER
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.LocalTime; | |
import java.time.format.DateTimeFormatter; | |
import java.time.format.DateTimeFormatterBuilder; | |
import static java.time.temporal.ChronoField.HOUR_OF_DAY; | |
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR; | |
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE; | |
static String currentFormattedTime() { | |
DateTimeFormatter timeFormatter = new DateTimeFormatterBuilder() | |
.appendValue(HOUR_OF_DAY, 2) | |
.appendLiteral(':') | |
.appendValue(MINUTE_OF_HOUR, 2) | |
.optionalStart() | |
.appendLiteral(':') | |
.appendValue(SECOND_OF_MINUTE, 2) | |
.toFormatter(); | |
return LocalTime.now().format(timeFormatter); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment