当前页面: 开发资料首页 → J2SE 专题 → 为什么我用Date得不到正确结果呢?
为什么我用Date得不到正确结果呢?
摘要: 为什么我用Date得不到正确结果呢?
我是新手,刚学Java,在我写下这段代码后:
import java.util.*;
public class Date{
public static void main(String[] args){
Date d=new Date();
System.out.println(d);
}
}
后运行,可得到的结果却是Date@11b86e7,而不是正确的日期,这是什么原因啊?请指教!
Date d=new Date();
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(formatter.format(date));
需要
import java.text.*;
可以把分给楼上的了。。。
toString
public String toString()
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
Date的toString()方法也可以返回一个字符串表示的日期时间,不过不支持国际化,楼上的就可以!
如果要显示到秒
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
学习一下。.