java-date
1、Date
1.1 代码
package com.nash.java.pop.ch07_date;
import java.util.Date;
public class DateApp {
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));
}
}
1.2 结果
Sun Jan 26 15:21:40 CST 2020
Sun Jan 26 09:48:20 CST 2020
false
2、DateFormat
2.1 代码
package com.nash.java.pop.ch07_date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatApp {
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));
}
}
2.2 结果
通过抽象类的静态方法获得实例:20-1-26 下午3:22
yyyy-MM-dd hh:mm:ss 2020-01-26 03:22:55
yyyy年MM月dd日 hh时mm分ss秒 2020年01月26日 03时22分55秒
yyyy-MM-dd 2020-01-26
hh:mm:ss 03:22:55
网友评论