19

I want to print current date and time in java..This is the code I am trying from a java tutorial:

import java.util.*;
  
public class Date {
   public static void main(String args[]) {
       // Instantiate a Date object
       Date date = new Date();
        
       // display time and date using toString()
       System.out.println(date.toString());
   }
}

It compiles fine. But the output given is:

Date@15db9742

While i am expecting a output like this:

Mon May 04 09:51:52 CDT 2009

What is wrong with the code?

EDIT: I tried to rename the class.. The editted code:

import java.util.*;
  
public class DateDemo {
   public static void main(String args[]) {
       // Instantiate a Date object
       Date d = new Date();
        
       // display time and date using toString()
       System.out.println(d.toString());
   }
}

This is how I compiled the code and ran:

sou@sou-linux:~/Desktop/java$ javac DateDemo.java

sou@sou-linux:~/Desktop/java$ java DateDemo

The output is:

Date@15db9742

Community
  • 1
  • 1
SouvikMaji
  • 1,088
  • 3
  • 22
  • 39

9 Answers9

19

Your class is a custom class that is producing the output given by Object.toString. Rename the class to something else other than Date so that java.util.Date is correctly imported

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • 1
    Along with renaming the class, he will also need to delete previous written Date.java and Date.class before recompiling the new code to make it work as expected. – sactiw Aug 26 '16 at 16:22
14

You are getting the default toString() from Object because you created your own class named Date which is hiding the imported Date from java.util.. You can rename your class or you can use the canonical name java.util.Date like

public static void main(String args[]) {
    java.util.Date date = new java.util.Date();
    System.out.println(date);
}

Output here is

Mon Nov 03 10:57:45 EST 2014
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
13
public static void main(String[] args) {

    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    //get current date time with Date()
    Date date = new Date();
    System.out.println(dateFormat.format(date));

    //get current date time with Calendar()
    Calendar cal = Calendar.getInstance();
    System.out.println(dateFormat.format(cal.getTime()));

}
Kirk Backus
  • 4,776
  • 4
  • 32
  • 52
Ashu Phaugat
  • 632
  • 9
  • 23
2

Rename your class from Date to something else .. The following then works just as expected:

import java.util.Date;

public class ImplClass
{
   public static void main(String args[])
   {
       Date date = new Date();

       // display time and date using toString()
       System.out.println(date.toString());
   }

}

The output is in the desired format

Mon Nov 03 09:49:57 CST 2014
Chiseled
  • 2,280
  • 8
  • 33
  • 59
2
import java.util.Date;
public class Dte {

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

Change the class name Date to Dte or some thing and the code will work properly.

The output is : Mon Nov 03 21:27:15 IST 2014

GameDroids
  • 5,584
  • 6
  • 40
  • 59
vvkkumr
  • 81
  • 1
  • 6
2

You should not use the toString() method, because:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

Quote from docs.oracle: http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html).

The Date class is designed to use like everyone else has answered. Just wanted to explain/give you a reference to why.

Community
  • 1
  • 1
Daniel Kvist
  • 3,032
  • 5
  • 26
  • 51
  • java.util.Date class overrides toString() method of Object class to print the date in following format: dow mon dd hh:mm:ss zzz yyyy The real reason of 'why?' is that his Date object is not of java.util.Date class but his own Date class. – sactiw Aug 26 '16 at 15:23
2

Already, the root cause has been pointed out and the resolution has been suggested in the accepted answer. This answer is to introduce the modern Date-Time API to future visitors.

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Solution using java.time, the modern Date-Time API:

import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Instant instant = Instant.now();
        System.out.println(instant);
    }
}

Output from a sample run:

2021-07-03T14:08:02.594203Z

ONLINE DEMO

An Instant represents an instantaneous point on the timeline in UTC. The Z in the output is the timezone designator for a zero-timezone offset. It stands for Zulu and specifies the Etc/UTC timezone (which has the timezone offset of +00:00 hours).

Note: In case you have got an object of java.util.Date, you can convert it to Instant as shown below:

Date date = new Date(); // A sample date
Instant instant = date.toInstant();

How to display the current Date-Time in a particular timezone:

Use ZonedDateTime#now or ZonedDateTime#now(ZoneId) to display the current Date-Time in a particular timezone.

Demo:

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        // The current date-time in the JVM's timezone
        ZonedDateTime zdtDefaultTz = ZonedDateTime.now();
        System.out.println(zdtDefaultTz);

        // The current date-time in a specific timezone
        ZonedDateTime zdtNewYork = ZonedDateTime.now(ZoneId.of("America/New_York"));
        System.out.println(zdtNewYork);
    }
}

Output from a sample run:

2021-07-03T15:19:48.007549+01:00[Europe/London]
2021-07-03T10:19:48.010048-04:00[America/New_York]

ONLINE DEMO

How to display the current Date-Time in a particular timezone if you have got an instance of java.util.Date or Instant:

Convert Instant to ZonedDateTime representing Date-Time in the required timezone e.g.

ZonedDateTime zdt = instant.atZone(ZoneId.of("America/New_York"));

Demo:

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        Instant instant = Instant.now(); // new Date().toInstant()

        ZonedDateTime zdtDefaultTz = instant.atZone(ZoneId.systemDefault());
        System.out.println(zdtDefaultTz);

        ZonedDateTime zdtNewYork = instant.atZone(ZoneId.of("America/New_York"));
        System.out.println(zdtNewYork);

        ZonedDateTime zdtUtc = instant.atZone(ZoneId.of("Etc/UTC"));
        System.out.println(zdtUtc);
    }
}

Output:

2021-07-03T15:24:23.716050+01:00[Europe/London]
2021-07-03T10:24:23.716050-04:00[America/New_York]
2021-07-03T14:24:23.716050Z[Etc/UTC]

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

Your class name is Date so

Date date = new Date();

will create an object of your class and when you call date.toString() it will be the default Object.toString() method.
So if you have to use the java.util.Date class, rename your Date class to something else

Johny
  • 2,128
  • 3
  • 20
  • 33
1

You could keep the code as simple as ::

import java.util.*;
public class Today{
  public static void main(String args[]){
    System.out.println("System Date :: ");
    System.out.println(new Date());
  }
}

As well as change the class name to today or something else but not Date as it is a keyword