Skip to content

Instantly share code, notes, and snippets.

@alonsoir
Created February 6, 2019 10:38
Show Gist options
  • Save alonsoir/ee499c7ae1c563aaec3ca42a2beaa52b to your computer and use it in GitHub Desktop.
Save alonsoir/ee499c7ae1c563aaec3ca42a2beaa52b to your computer and use it in GitHub Desktop.
CURRENT TIME IN HH:MM:SS WITH JAVA 8+ AND DATETIMEFORMATTERBUILDER
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