一、jdk8之前的常用日期时间API
1.System类中的currentTimeMills();
1 @Test 2 public void test() { 3 //时间戳:1970 00:00:00到当前时间的毫秒数 4 long time = System.currentTimeMillis(); 5 System.out.println(time);//当前时间戳 6 } 7 8 }
2.Java.Utils.Date 和 子类 java.sql.Date
java.util.Date:
- 两个构造:new Date() 和 new Date(1612613682684l),
- 两个方法:toString():显示当前的年月日时分秒;getTime():当前Date对象的毫秒数(时间戳)
java.sql.Date;对应数据库中的时间;是 java.util.Date 的子类;
如何实例化?
sql.Date--》util.Date 直接赋值(多态)
util.Date--》sql.Date()
Date 的 before 和 after
Date1.after(Date2),当Date1大于Date2时,返回TRUE,当小于等于时,返回false;
即Date2比Date1小的true/false,当Date2日期比Date1小的时候为true,否则为false
Date1.before(Date2),当Date1小于Date2时,返回TRUE,当大于等于时,返回false;
@Test public void test() { //构造器一:创建当前时间的Date对象 Date date1 = new Date(); System.out.println(date1.toString()); System.out.println(date1.getTime()); //构造器二:创建指定时间的Date对象(过时了) Date date2 = new Date(1612613682684l); System.out.println(date2.toString()); //创建java.sql.Date 对象 java.sql.Date date3 = new java.sql.Date(1612613682684l); System.out.println(date3.toString()); Date date4 = new Date(); // java.sql.Date date5 = (java.sql.Date)date4; java.sql.Date date5 = new java.sql.Date(date4.getTime()); System.out.println(date5); }
3.SimpleDateFormat
SimpleDateFormat的使用:SimpleDateFormat对日期Date类的格式化和解析
1.两个操作:
- 1.1格式化: 日期==》字符串
- 1.2解析: 字符串==》日期
2.simpleDateFormate的实例化
@Test public void testSimpleDateFormat() throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat(); //1.格式化:日期==》字符串 Date date = new Date(); System.out.println(date); String str = sdf.format(date); System.out.println(str); //2.解析 String str2= "2/25/21 8:20 PM"; Date date2 = sdf.parse(str); System.out.println(date2); /指定格式/ SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String strDate = sdf1.format(date); System.out.println(strDate); System.out.println(sdf1.parse("2021-02-25 08:28:03")); }
练习:
- 字符串转换为 java.sql.Date;
- 练习2 计算 1990-01-01 三天打鱼两天晒网 到 2020-9-9是打鱼还是晒网
1 @Test 2 public void testExer() throws ParseException { 3 String birth = "1991-09-28"; 4 SimpleDateFormat fromate = new SimpleDateFormat("yyyy-MM-dd"); 5 Date date = fromate.parse(birth); 6 java.sql.Date sqlDate = new java.sql.Date(date.getTime()); 7 System.out.println(sqlDate); 8 }
1 @Test 2 public void TestTotalDays() throws ParseException { 3 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 4 Date date1 = sdf.parse("1990-01-01 00:00:00"); 5 Date date2 = sdf.parse("2020-9-8 00:00:00"); 6 long l1 = date1.getTime(); 7 long l2 = date2.getTime(); 8 double days = (l1-l2)/(1000*60*60*24); 9 days = Math.abs(days); 10 if(days%5 == 1 ||days%5 == 2||days%5 == 3 ) { 11 System.out.println("打鱼"); 12 }else if(days%5 == 4 ||days%5 == 0) { 13 System.out.println("晒网"); 14 } 15 System.out.println(days); 16 17 }
4.Calendar
实例化
方式一:创建子类对象GregorianCalendar;
方式二:调用静态方法getInstance;(其实核心也是方式一创建);
问:为什么Calendar不能直接实例化?
答:Calendar是一个抽象类;抽象类不能实例化。抽象类有构造器,但是不能实例化。抽象类存在的意义为了实现多态;那么就设计到了子类类继承;子类继承了抽象类,那么子类需要实例化;那么父类构造器是必要的;所以抽象类就有了构造器;
常用方法:
get set add
getTime,setTime...
说明:set操作会改变当前calendar对象的值;23行的calendar日期已经和10行的日期已经不一样了;
1 @Test 2 public void testCalendar() { 3 //实例化: 4 //方式一:创建其子类对象GregorianCalendar 5 //方式二:调用他的静态方法;其实也是方式一的类创建的 6 Calendar calendar = Calendar.getInstance(); 7 System.out.println(calendar.getClass()); 8 //常用方法: 9 //get 10 int dayInWeek = calendar.get(Calendar.DAY_OF_WEEK); 11 System.out.println("本周的第几天:"+dayInWeek);//周天是1 周一是2 周二是3 12 int month = calendar.get(Calendar.MONTH); 13 System.out.println("本月:"+month);//1月是0 14 int daysInMonth = calendar.get(Calendar.DAY_OF_MONTH); 15 System.out.println("本月的第几天:"+daysInMonth); 16 int daysInYear = calendar.get(Calendar.DAY_OF_YEAR); 17 System.out.println("本年的第几天:"+daysInYear); 18 int weekInYear = calendar.get(Calendar.WEEK_OF_YEAR); 19 System.out.println("本年的第几周:"+weekInYear); 20 //set 修改calendar的值 21 calendar.set(Calendar.DAY_OF_MONTH, 22); 22 daysInMonth = calendar.get(Calendar.DAY_OF_MONTH); 23 System.out.println("本月的第几天:"+daysInMonth); 24 //add 25 calendar.add(Calendar.DAY_OF_MONTH, 3); 26 daysInMonth = calendar.get(Calendar.DAY_OF_MONTH); 27 System.out.println("本月的第几天:"+daysInMonth); 28 29 calendar.add(Calendar.DAY_OF_MONTH, -3); 30 daysInMonth = calendar.get(Calendar.DAY_OF_MONTH); 31 System.out.println("本月的第几天:"+daysInMonth); 32 //getTime() 33 Date date = calendar.getTime(); 34 //setTime() 35 Date date1 = new Date(); 36 calendar.setTime(date1); 37 38 }
二、jdk8的常用日期时间:localdate,localTime、localdatetime
1.实例化和常用方法
实例化:
now()/now(Zoneld zone)
of()
get操作:
getDayOfMonth() getDayOfYear()
getDayOfWeek()
getMonth()
getMonthValue()/getYear()
getHour()/getMinute()/getSecond
set操作
withDayOfMonth() withdayOfYear witchMonth witchYear
其他:
plusDays() plusWeeks() plusMonths() plusYears plusHours
minusMonths() minusWeeks() minusDays minusYears() minusHours
1 @Test 2 public void testSimpleDateFormat() throws ParseException { 3 //实例化 4 //now 5 LocalDate localDate = LocalDate.now(); 6 LocalTime localTime = LocalTime.now(); 7 LocalDateTime localDateTime = LocalDateTime.now(); 8 System.out.println(localDate); 9 System.out.println(localTime); 10 System.out.println(localDateTime); 11 //of 12 localDate = LocalDate.of(2020, 10, 6); 13 localTime = LocalTime.of(20, 31, 32); 14 localDateTime = LocalDateTime.of(2020, 10, 6, 20, 31, 32); 15 System.out.println(localDate); 16 System.out.println(localTime); 17 System.out.println(localDateTime); 18 19 20 int DayOfYear = localDateTime.getDayOfYear(); 21 int DayOfMonth = localDateTime.getDayOfMonth(); 22 DayOfWeek DayOfWeek = localDateTime.getDayOfWeek(); 23 Month month = localDateTime.getMonth(); 24 int days = localDateTime.getMonthValue(); 25 System.out.println(DayOfYear); 26 System.out.println(DayOfMonth); 27 System.out.println(DayOfWeek); 28 System.out.println(month); 29 System.out.println(days); 30 31 LocalDateTime localDateTime1 = localDateTime.withDayOfYear(156); 32 LocalDateTime localDateTime2 = localDateTime.withDayOfMonth(1); 33 System.out.println(localDateTime);//不可变; 34 System.out.println(localDateTime1); 35 System.out.println(localDateTime2); 36 37 localDateTime = localDateTime.plusDays(3); 38 System.out.println(localDateTime); 39 40 localDateTime = localDateTime.minusDays(2); 41 System.out.println(localDateTime); 42 }
2.格式化和解析
- 方式一、预定义格式: ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME;
- 方式二、本地格式化: ofLocalizedDateTime();
- 方式三(常用)、自定义格式: ofPattern("yyyyy-MM-dd hh:mm:ss")
@Test public void testDateTimeFormatter() { LocalDateTime localDateTime = LocalDateTime.now(); DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ISO_LOCAL_DATE_TIME; String formate = dateTimeFormatter1.format(localDateTime); TemporalAccessor localDateTime1 = dateTimeFormatter1.parse("2021-03-04T21:31:57.895"); System.out.println(formate); System.out.println(localDateTime1); DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG); LocalDate localDate = LocalDate.now(); String formate2 = dateTimeFormatter2.format(localDate); TemporalAccessor localDateTime2 = dateTimeFormatter2.parse("March 4, 2021"); System.out.println("formate2:"+formate2); System.out.println(localDateTime2); DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd"); String formate3 = dateTimeFormatter3.format(localDateTime); System.out.println("formate3:"+formate3); System.out.println(dateTimeFormatter3.parse("2021-03-04")); }
3.其他:instant
1 //instant 2 @Test 3 public void testInstant() { 4 //实例 5 Instant instant = Instant.now(); 6 System.out.println(instant);//格林尼治时间 7 OffsetDateTime offsetTime = instant.atOffset( ZoneOffset.ofHours(8)); 8 System.out.println(offsetTime);//当前东八区的时间 9 System.out.println(instant.toEpochMilli());//转当前时间到 1970年1-1的毫秒数 10 11 Instant instant1 = Instant.ofEpochMilli(0);//时间戳转 instant 12 System.out.println(instant1); 13 14 }