Date
1、获取当前时间
Date date = new Date();
2、 从1970-0-0年到创建Date对象的的毫秒数
Date date = new Date();
long dateLong = date.getTime()
3、比较时间大小
Date date1 = new Date();
Date date2 = new Date(date1.getTime()-1000);
System.out.println(date1.after(date2));//判断date1是否在date2的之后
System.out.println(date1.before(date2));//判断date1是否在date2的之前
//比较时间大小。大于返回1,相等返回0,小于返回-1
System.out.println(date1.compareTo(date2));
Calendar
1、创建当前时间的calendar对象
Calendar calendar = Calendar.getInstance();
2、获取calendar的年月日周
System.out.println("获取calendar的年份"+calendar.get(Calendar.YEAR));
System.out.println("获取calendar月份,从零开始"+calendar.get(Calendar.MONTH));
System.out.println("获取星期,从星期天开始"+calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("获取今天是多少号"+calendar.get(Calendar.DATE));
System.out.println("这个月的第几星期"+calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("今年的第几星期"+calendar.get(Calendar.WEEK_OF_YEAR));
3、 将calendar与date相互转化
Date date=new Date();
Calendar cal=Calendar.getInstance();
cal.setTime(date);//设置为calendar
System.out.println(calendar.getTime());//通过calendar的get方法输出date格式
4、设置年、月、日、时、分、秒、毫秒
set(int field, int value);
set(int year, int month, int day);
set(int year, int month, int day, int hour, int minute);
set(int year, int month, int day, int hour, int minute, int second);
Calendar calendar = Calendar.getInstance();
calendar.set(2019,2,23);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("通过set的"+simpleDateFormat.format(calendar.getTime()));