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方法格式化时间,得到想要的字符串。