Java 如何显示当前系统日期与时间

来源:百度知道 编辑:UC知道 时间:2024/09/25 13:57:47
就是编写一个程序,可以显示当前系统的日期与时间,应该挺简单,可我不会,请高手指教
麻烦写完整些,本人初学,谢谢

import java.text.*;
import java.util.*;
public final class Constants { // Session keys
public static final String getDate(){
Calendar cal = Calendar.getInstance();
java.text.SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String cdate = sdf.format(cal.getTime());
System.out.println("times is :"+cdate);
return cdate;
}
}

上面的代码用到了Java中的Calendar类,这是个时间类,后面用到了时间格式化的类。
“yyyy-MM-dd HH:mm:ss”代表要想得到的时间字符串的类型。

String cdate = sdf.format(cal.getTime());

通过格式化类的format方法格式化时间,得到想要的字符串。

http://www.leaninit.cn/article.asp?id=44

Date date = new Date();
date就是现在系统的时间
如果要转成某种格式输出可以这么写:
String st = new