Java Program to Get Individual Components of the Current Time

You are currently viewing Java Program to Get Individual Components of the Current Time

Java program to get individual components of the current time

// Java program to get individual component 
// of current time

import java.util.*;
import java.time.*;
import java.text.*;

public class Main {
  public static void main(String[] args) {
    Date date = new Date();
    DateFormat dtFormat = new SimpleDateFormat("hh:mm:ss");

    final String strDate = dtFormat.format(date);
    LocalTime time = LocalTime.parse(strDate);

    int hh = time.getHour();
    int mm = time.getMinute();
    int ss = time.getSecond();

    System.out.printf("Current Utc Time: %02d:%02d:%02d", hh, mm, ss);
  }
}

Output

Current Utc Time: 05:32:15

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply