Languages
[Edit]
EN

Java - print current date and time

0 points
Created by:
Palusbug
395

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
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
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