Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Java

Idiom #261 Format time hours-minutes-seconds

Assign to the string x the value of fields (hours, minutes, seconds) of the date d, in format HH:MM:SS.

import static java.util.Calendar.getInstance;
import java.util.Date;
Date d = getInstance().getTime();
String x = "%tT".formatted(d);
import static java.lang.System.currentTimeMillis;
long d = currentTimeMillis();
String x = "%tH:%<tM:%<tS".formatted(d);
with Ada.Calendar.Formatting;
X : constant String :=
    Ada.Calendar.Formatting.Image (D) (12 .. 19);

Image returns date as well therefore the slice

New implementation...
< >
programming-idioms.org