一、用java.util.Date类实现,new Date();输出Thu Apr 06 14:16:52 CST 2017;
二、用System的currentTimeMillis方法获取;System.currentTimeMillis();输出时间轴格式时间,1491459619038,都需要用SimpleDateFormat格式化;
SimpleDateFormat函数的继承关系:
Java.lang.Object
+----java.text.Format
+----java.text.DateFormat
+----java.text.SimpleDateFormat
常用类:
JavaBean:
public class FormatDateTime {
public static String toLongDateString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");
return myFmt.format(dt);
}
public static String toShortDateString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yy年MM月dd日 HH时mm分");
return myFmt.format(dt);
}
public static String toLongTimeString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("HH mm ss SSSS");
return myFmt.format(dt);
}
public static String toShortTimeString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yy/MM/dd HH:mm");
return myFmt.format(dt);
}
public static void main(String[] args) {
Date now=new Date();
System.out.println(FormatDateTime.toLongDateString(now));
System.out.println(FormatDateTime.toShortDateString(now));
System.out.println(FormatDateTime.toLongTimeString(now));
System.out.println(FormatDateTime.toShortTimeString(now));
}
}
类中的
curren