Easy Tutorial
❮ Net Connected Java9 Optional Class Improvements ❯

Java Example - Get Current Time

Java Examples

The following example demonstrates how to use the Date class and the format(date) method of the SimpleDateFormat class to output the current time:

Main.java File

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void main(String[] args) {

        SimpleDateFormat sdf = new SimpleDateFormat(); // Format the time
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss a"); // a is the marker for am/pm
        Date date = new Date(); // Get the current time
        System.out.println("Current time: " + sdf.format(date)); // Output the formatted current time (24-hour format)
    }
}

The above code outputs the following result:

Current time: 2015-03-27 21:27:28 PM

Java Examples

❮ Net Connected Java9 Optional Class Improvements ❯