logo image

How to get current Year, Month, Date, Hour, Minute & Second in Java



Using java.util.Calendar Class in Java, you can get current Year, Month, Date, Hour, Minute & Second in Java.

import java.util.Calendar;

public class GetTimeDetails {
	public static void main(String[] args) {
		Calendar calendar = Calendar.getInstance();

		System.out.println("Year: " + calendar.get(Calendar.YEAR));
		System.out.println("Month: " + calendar.get(Calendar.MONTH));
		System.out.println("Date: " + calendar.get(Calendar.DATE));

		System.out.println("Hour: " + calendar.get(Calendar.HOUR));
		System.out.println("Minute: " + calendar.get(Calendar.MINUTE));
		System.out.println("Second: " + calendar.get(Calendar.SECOND));
	}
}

Output

Year: 2017
Month: 8
Date: 7
Hour: 7
Minute: 45
Second: 54