Java 如何显示当前系统日期与时间
来源:互联网 发布:包师语 安河桥 知乎 编辑:程序博客网 时间:2024/05/22 07:49
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方法格式化时间,得到想要的字符串。