2016-07-24
1,Date类
package com.java1995; import java.util.Date; public class TestDate { public static void main(String[] args) { Date d1 = new Date(); System.out.println(d1); Date d2 = new Date(System.currentTimeMillis() - 20000000); System.out.println(d2); System.out.println(d1.before(d2)); } }
2,DateFormat类
package com.java1995; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class TestDateFormat { public static void main(String[] args) { Date date = new Date(); // 通过抽象类的静态方法获得实例 DateFormat df1 = DateFormat.getInstance(); // new 子类获得实例 DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); DateFormat df3 = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒"); DateFormat df4 = new SimpleDateFormat("yyyy-MM-dd"); DateFormat df5 = new SimpleDateFormat("hh:mm:ss"); System.out.println("通过抽象类的静态方法获得实例:" + df1.format(date)); System.out.println("yyyy-MM-dd hh:mm:ss " + df2.format(date)); System.out.println("yyyy年MM月dd日 hh时mm分ss秒 " + df3.format(date)); System.out.println("yyyy-MM-dd " + df4.format(date)); System.out.println("hh:mm:ss " + df5.format(date)); } }
【参考资料】