1.jdk8之前API
-
1.System 类
System.currentTimeMillis()//返回系统时间距离1970年1月1日的毫秒数- 计算世界时间的主要标准:
- UTC :世界同一时间
- GMT:格林威治时间
- CTS:中央标准时间
- 计算世界时间的主要标准:
-
2.Date 类
-
java.util.Date
- java.sql.Date
1.两个构造器的使用
构造器一:Date(); 创建对应当前时间的Date对象
Date date =new Date(); System.out.println(date.toString()); //Fri Aug 21 10:00:54 CST 2020 System.out.println(date1.getTime()); //1597975619239 构造器二:Date(long date);创建指定毫秒数的Date对象
Date date2 =new Date(1597975619239L); System.out.println(date2.toString()); //Fri Aug 21 10:06:59 CST 20202.两个方法的使用
>toString() :显示年,月,日,时,分,秒
>getTime() :获取当前Date对象对应的毫秒数。(1970-1-1 0时0分0秒)
-
-
3.java.text.SimpleDateFomat类
-
SimpleDateFoma:对日期Date类的格式化
-
两个操作:
- 1.格式化:日期 --->字符串
- 2.解析:格式化的逆过程,字符串 --->日期
-
SimpleDateFoma的实例化
1.格式化 :使用默认的构造器 SimpleDateFormat sFormat =new SimpleDateFormat(); //格式化日期 Date date =new Date(); String formate= sFormat.format(date); System.out.println(formate); // 20-8-21 上午10:36 2.解析 String string ="20-8-21 上午10:39"; //特定的格式 try { Date date1=sFormat.parse(string); System.out.println(date1); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } //Fri Aug 21 10:39:00 CST 20201.格式化:使用指定格式的方式进行格式化 SimpleDateFormat sFormat =new SimpleDateFormat("yyyy/MM/dd"); //格式化日期 Date date =new Date(); String formate= sFormat.format(date); System.out.println(formate); //2020/08/21 2.解析 sFormat.parse("2019/01/01")//可以解析,格式与SimpleDateFormat的一致-
例:把20200821转换成2020-08-21
SimpleDateFormat sFormat =new SimpleDateFormat("yyyyMMdd"); //先转换成Date Date date = sFormat.parse("20200820"); System.out.println(date); //再转换成String SimpleDateFormat sFormat1 =new SimpleDateFormat("yyyy-MM-dd"); System.out.println(sFormat1.format(date)); //Thu Aug 20 00:00:00 CST 2020 //2020-08-20
-
-
4.java.util.Calendar类(抽象类)
-
获取Calendar实例的方法
-
使用Calender.getInstance()方法
Calendar calendar=Calendar.getInstance(); -
调用它的子类GergorianCalendar的构造器
GergorianCalendar calendar=new GergorianCalendar()
-
-
常用方法
-
get() 获取常用属性
//获取这个月的第几天 int day=calendar.get(Calendar.DAY_OF_MONTH); System.out.println(day); //获取这年的第几天 System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); //21 //234 -
set() 更改属性calendar的属性
calendar.set(Calendar.DAY_OF_MONTH,22); System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); //22 -
add() 与set类似,在原有的基础上加
calendar.add(Calendar.DAY_OF_MONTH,3); System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); //25 -
getTime() 把calendar类转换成date类
Date date = calendar.getTime(); System.out.println(date); //Sat Aug 25 11:21:54 CST 2020 -
setTime() 把date类转换成calendar类
Date date2 = new Date(); calendar.setTime(date2);
-
-
-
2.jdk8API
1.LocalDate,LocalTime,LocalDateTime
-
实例化 now() 根据当前时间创建对象
LocalDate localDate = LocalDate.now();//日期 LocalTime localTime = LocalTime.now();//时间 LocalDateTime localDateTime = LocalDateTime.now();//日期+时间 System.out.println(localDate); System.out.println(localTime); System.out.println(localDateTime); //2020-08-21 //13:51:10.924 //2020-08-21T13:51:10.924 -
of() 根据指定时间创建对象(LocalDateTime用的比较频繁,其他两个用法相同)
LocalDateTime localDateTime1 =LocalDateTime.of(2020,10,6,13,12,12);//指定时间 System.out.println(localDateTime); //2020-10-06T13:12:12 -
getXxx() 获取响应的属性值
2020-08-21T13:51:10.924 System.out.println(localDateTime.getDayOfMonth()); System.out.println(localDateTime.getDayOfYear()); System.out.println(localDateTime.getDayOfWeek()); System.out.println(localDateTime.getMonthValue()); System.out.println(localDateTime.getMinute()); System.out.println(localDateTime.getMonth()); //21 //234 //FRIDAY //8 //59 //AUGUST -
withXxx() 设置指定的时分秒 //体现不可变性
2020-08-21T13:51:10.924 LocalDateTime localDateTime2 = localDateTime.withDayOfMonth(2); System.out.println(localDateTime2); System.out.println(localDateTime); //2020-08-02T14:03:40.957 //2020-08-21T14:03:40.957 -
plusXxx() 加上指定的时分秒
2020-08-21T13:51:10.924 LocalDateTime localDateTime3 = localDateTime.plusMonths(2); System.out.println(localDateTime3); System.out.println(localDateTime); //2020-10-21T14:06:23.316 //2020-08-21T14:06:23.316 -
minusXxx() 减去指定的时分秒
2020-08-21T13:51:10.924 LocalDateTime localDateTime3 = localDateTime.minusMonths(2); System.out.println(localDateTime3); System.out.println(localDateTime); //2020-6-21T14:06:23.316 //2020-08-21T14:06:23.316
2.instant类
- 实例化 now()