Getting current date and time with Calendar

java.util.Calendar represents a specific instant in time, with millisecond precision.

Getting current date and time with Calendar

The example uses java.util.Calendar to get the current date-time and formats it with java.text.SimpleDateFormat.
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class JavaCurrentDateTimeCalendar {

    public static void main(String[] args) {

        Date now = Calendar.getInstance().getTime(); 
        
        DateFormat df = new SimpleDateFormat("yyyy-MM-d HH:mm:ss");
        
        System.out.println(df.format(now));
    }
}
Output:
2019-07-11 17:30:59






Comments