Languages
[Edit]
EN

Java - print current date and time

0 points
Created by:
Dirask Community
6300

In this article, we would like to show you how to print the current date and time in Java.

Quick solution:

import java.util.Date;

public class Example {
    private static Date date = new Date();  //create Date object

    public static void main(String[] args) {
        System.out.println(date.toString());  //print Date object
    }
}

 

Practical example

To print the current date and time in Java you need to:

  1. Create a new Date object without any arguments - this will assign the current date and time to the new object,
  2. use toString() method to print the new object.

Example code:

import java.util.Date;

public class Example {
    private static Date date = new Date();

    public static void main(String[] args) {
        System.out.println(date.toString());
    }
}

Output:

Mon May 24 17:23:44 CEST 2021
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join