在java中打印当前日期 [英] print current date in java
问题描述
我想在java中打印当前日期和时间。这是我在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
Date@15db9742
虽然我期待这样的输出:
While i am expecting a output like this:
Mon May 04 09:51:52 CDT 2009
Mon May 04 09:51:52 CDT 2009
代码有什么问题?
编辑:
我试图重命名类..编辑代码:
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$ javac DateDemo.java
sou @ sou-linux:〜/ Desktop / java $ java DateDemo
sou@sou-linux:~/Desktop/java$ java DateDemo
输出为:
Date @ 15db9742
Date@15db9742
推荐答案
您的类是一个自定义类,它产生 Object.toString
给出的输出。将类重命名为 Date
以外的其他内容,以便正确导入 java.util.Date
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
这篇关于在java中打印当前日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!