Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
483 views
in Technique[技术] by (71.8m points)

time - print current date in java

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...