时间类
计算机中,把1970年1月1日00:00:00定为基准时间,你问为啥是这个时间,因为我说了不算呐。
用long类型值存储
long now = System.currentTimeMillis()
1. Date类
import java.util.Date;
public class TestDate {
public static void main(String[] args) {
long nowNum = System.currentTimeMillis(); //代表当前时刻的毫秒数
System.out.println(nowNum);
Date d1 = new Date(); //没有传参,代表当前时刻
System.out.println(d1);
System.out.println(d1.getTime());
Date d2 = new Date(1000L*3600*24*376*250); //以后的时刻
System.out.print(d2);
}
}
/*
1636380268809
Mon Nov 08 22:04:28 CST 2021
1636380268809
Mon May 14 08:00:00 CST 2227
*/
2. DateFormat类和SimpleDateFormat类
时间格式化类和简单时间格式化类
DateFormat类:
把时间对象转化成指定格式的字符串,反之,把指定格式的字符串转化为时间对象。DateFormat是一个抽象类,一般使用它的子类SimpleDateFormat类实现。
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class TestDateFormat {
public static void main(String[] args) throws ParseException {
//new出SimpleDateFormat对象
SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat s2 = new SimpleDateFormat("yyyy-MM-dd");
//将时间对象转换为字符串,默认当前时间
String daytime = s1.format(new Date());
System.out.println(daytime);
System.out.println(s2.format(new Date()));
System.out.println(new SimpleDateFormat("hh:mm:ss").format(new Date()));
//将符合指定格式的字符串转成时间对象,字符串格式需要和指定格式一致
String time = "2007-10-08";
Date date = s2.parse(time);
System.out.println("date1:"+date);
time = "2007-10-7 20:15:30";
date = s1.parse(time);
System.out.println("date2:"+date);
}
}
3. Calendar日历类
Calendar类是一个抽象类,为我们提供了关于日期计算的功能。
GregorianCalendar是Canlendar的一个具体子类。
注意:月份0-11表示,0对应1月
import java.util.*;
public class TestCalendar {
public static void main(String[] args) {
//得到相关日期元素
GregorianCalendar calendar = new GregorianCalendar(1999,10,9,22,10,50);
int year = calendar.get(Calendar.YEAR); //打印年份
int month = calendar.get(Calendar.MONTH); //打印月份
int day = calendar.get(Calendar.DAY_OF_MONTH); //打印日期
int day2 = calendar.get(Calendar.DATE); //同上同义
int date = calendar.get(Calendar.DAY_OF_WEEK);
//星期几,这里是1-7,周日是1,周一是2...
System.out.println(year);
System.out.println(month);
System.out.println(day);
System.out.println(day2);
System.out.println(date);
//设置日期
GregorianCalendar calendar2 = new GregorianCalendar();
calendar2.set(Calendar.YEAR, 29);
calendar2.set(Calendar.MONTH, Calendar.FEBRUARY);
calendar2.set(Calendar.DATE, 3);
calendar2.set(Calendar.HOUR_OF_DAY, 10);
calendar2.set(Calendar.MINUTE, 20);
calendar2.set(Calendar.SECOND, 23);
printCalendar(calendar2);
System.out.println();
//日期计算
GregorianCalendar calendar3 = new GregorianCalendar(1999,10,9,22,10,50);
calendar3.add(Calendar.MONTH, -7); //月份减7
calendar3.add(Calendar.DATE, 7); //增加7天
printCalendar(calendar3);
//日历对象和时间对象转化
Date d = calendar3.getTime();
GregorianCalendar calendar4 = new GregorianCalendar();
calendar4.setTime(new Date());
}
private static void printCalendar(Calendar calendar) {
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH)+1; //月份从0开始算
int day = calendar.get(Calendar.DAY_OF_MONTH);
int date = calendar.get(Calendar.DAY_OF_WEEK)-1; //星期几
String week = "" + ((date == 0)?"日":date);
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
System.out.print(year+" "+month+" "+day+" "+week+" "+hour+" "+minute+" "+second);
}
}