用java打印当前日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26717733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
print current date in java
提问by SouvikMaji
I want to print current date and time in java..This is the code I am trying from a java tutorial:
我想在 java 中打印当前日期和时间..这是我在 java 教程中尝试的代码:
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
日期@15db9742
While i am expecting a output like this:
虽然我期待这样的输出:
Mon May 04 09:51:52 CDT 2009
2009 年 5 月 4 日星期一 09:51:52 CDT
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
sou@sou-linux:~/Desktop/java$ javac DateDemo.java
sou@sou-linux:~/Desktop/java$ java DateDemo
The output is:
输出是:
Date@15db9742
日期@15db9742
采纳答案by Reimeus
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
您的类是一个自定义类,它产生由Object.toString
. 将类重命名为其他名称,Date
以便java.util.Date
正确导入
回答by Chiseled
Rename your class from Date to something else .. The following then works just as expected:
将您的班级从 Date 重命名为其他名称..然后按预期工作:
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
回答by Elliott Frisch
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
您正在获取默认值toString()
,Object
因为您创建了自己的名为的类,该类Date
隐藏了Date
从java.util.
. 您可以重命名您的课程,也可以使用规范名称,java.util.Date
例如
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
回答by Johny
Your class name is Date so
你的班级名称是 Date 所以
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
将创建您的类的一个对象,当您调用date.toString()
它时,它将是默认Object.toString()
方法。
因此,如果您必须使用java.util.Date
该类,请将您的Date
类重命名为其他名称
回答by Ashu Phaugat
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()));
}
回答by vvkkumr
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.
将类名更改Date
为Dte
或一些东西,代码将正常工作。
The output is : Mon Nov 03 21:27:15 IST 2014
输出是: Mon Nov 03 21:27:15 IST 2014
回答by Daniel Kvist
You should not use the toString() method, because:
您不应该使用 toString() 方法,因为:
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())
class?Object? 的?toString? 方法返回一个字符串,该字符串由对象是其实例的类的名称、at 符号字符“@”和对象哈希码的无符号十六进制表示组成。换句话说,此方法返回一个等于以下值的字符串:
getClass().getName() + '@' + Integer.toHexString(hashCode())
Quote from docs.oracle: http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html).
来自 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.
Date 类旨在像其他人一样使用。只是想解释/给你一个参考原因。
回答by Saiman Naidu
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
以及将班级名称更改为今天或其他名称但不是日期,因为它是一个关键字
回答by Shiva
You can try this,
你可以试试这个
import java.util.Date;
public class CurrentDate
{
public static void main(String[] args)
{
Date currentDate = new Date();
System.out.println("Curent Date and Time - " + currentDate.toString());
}
}
Output:
输出:
Mon May 04 09:51:52 CDT 2009
Also, you can refer links below on date and time formatting. Quite useful.
此外,您可以参考以下有关日期和时间格式的链接。相当有用。
https://docs.oracle.com/javase/tutorial/datetime/iso/datetime.html
https://docs.oracle.com/javase/tutorial/datetime/iso/datetime.html
https://docs.oracle.com/javase/7/docs/api/java/sql/Date.html
https://docs.oracle.com/javase/7/docs/api/java/sql/Date.html