标记(区分大小写)
日期格式化
展示系统时间
- 获取当前系统时间System.currentTimeMillis()
- 产生一个当前的毫秒,这个毫秒其实就是自自1970-1-01 00:00:00.000 到当前时刻的时间距离。(类型为long,单位为毫秒数)
- Date() 其实就是相当于 Date(System.currentTimeMillis());对应构造函数Date(long date),用来计算long秒与1970年1月1日之间的毫秒差。
代码
package com.tulun.program;
import java.text.SimpleDateFormat;
import java.util.Date;
public class testDate {
public static void main(String[] args) {
Date date=new Date();
System.out.println(date);
SimpleDateFormat sdf1=new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf3=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf4=new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
System.out.println(sdf1.format(date));
System.out.println(sdf2.format(date));
System.out.println(sdf3.format(date));
System.out.println(sdf4.format(date));
}
}
字符串转为日期
SimpleDateFormat sdf1=new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDate1="20151010";
String strDate2="20171027 10:00:00";
try {
Date date1=sdf1.parse(strDate1);
System.out.println(date1);
Date date2=sdf2.parse(strDate2);
System.out.println(date2);
} catch (ParseException e) {
e.printStackTrace();
}