I have a DateTime object DT which stores current time. When I print DT, I want it to only print the time part, ie HH-MM-SS (H = hours, M = minutes, S = seconds) and ignore the date part.
How can I do this ? For that matter, is it even possible to create a date time object which will only contain HH-MM-SS and nothing related to date ? If that is true, then I can simply print it instead of extracting the HH-MM-SS part.
Thanks.
If you only want the time, you should use a
LocalTimeinstead of aDateTime. You can useDateTime.toLocalTime()to get the time part of an existingDateTime.If you actually want to keep the
DateTimebut only reveal the time part when formatting, you can create aDateTimeFormatterwith a pattern which only includes the time parts, but I’d usually consider this a design smell.