java8新特性之日期时间API
long times = System.currentTimeMillis();//返回的是当前时间与1970年1月1月1日0分0秒之间以毫秒为单位的时间差//称为时间戳System.out.println(times);
将java.util.Date 对象转换为java.sql.Date对象:
//将java.util.Date 对象转换为java.sql.Date对象Date date1 = new Date();java.sql.Date date2 = new java.sql.Date(date1.getTime());三、java.text.SimpleDateFormat
SimpleDateFormat是对日期Date类的格式化和解析。
两个操作:
1.格式化:
将日期转换为字符串:
SimpleDateFormat sfd = new SimpleDateFormat();Date date = new Date();System.out.println(date);String formateDate = sfd.format(date);System.out.println(formateDate);
也可以指定具体的格式化格式,查看具体的API格式。例:指定格式的格式化输出(调用带参数的构造器)
Date date = new Date();System.out.println(date);SimpleDateFormat sfd = new SimpleDateFormat('yyyy-MM-dd hh:mm:ss');String formateDate = sfd.format(date);System.out.println(formateDate);
2.解析:
将 字符串 转换为 日期。即格式化的逆过程。
String str = '2021/4/12 下午10:16'; SimpleDateFormat sfd = new SimpleDateFormat();Date date2 = sfd.parse(str);System.out.println(date2);
这个注意要抛异常(传入的str格式要与Date的原格式一致,或者说要与SimpleDateFormate当前识别的格式相同)。
练习:将字符串“2021-04-13” 转换为java.sql.Date类型对象。
分析:首先将字符串解析为Date类型的对象,然后在转为java.sql.Date类型对象。
public static void testExper() throws ParseException {String s = '2021-04-13';SimpleDateFormat sfd = new SimpleDateFormat('yyyy-MM-dd');Date date = sfd.parse(s);java.sql.Date date2 = new java.sql.Date(date.getTime());System.out.println(date2);}
常用实例化方法:
Calendar calendar = Calendar.getInstance();System.out.println(calendar.getClass()); //java.util.GregorianCalendar,其实还是子类类型的对象
常用方法:
1.get():获取常用的属性和信息。2.set():设置:相当于把本身的日期给改变了3.add():添加(增加时间、天数)4.getTime():日历类----> Date类5.setTime():Date类----> 日历类
Calendar calendar = Calendar.getInstance();//System.out.println(calendar.getClass()); //java.util.GregorianCalendar//get()int days = calendar.get(calendar.DAY_OF_MONTH);//获取当前日期是这个月的第几天System.out.println(days); //13//set()calendar.set(calendar.DAY_OF_MONTH, 22);//重新设置 days = calendar.get(calendar.DAY_OF_MONTH);//在重新获取System.out.println(days); //22//add()calendar.add(calendar.DAY_OF_MONTH, 3);days = calendar.get(calendar.DAY_OF_MONTH);System.out.println(days); //25//getTime():日历类----> Date类Date date = calendar.getTime();System.out.println(date); //Sun Apr 25 13:14:59 CST 2021//setTime():Date类----> 日历类Date date2 = new Date();calendar.setTime(date2);days = calendar.get(calendar.DAY_OF_MONTH);System.out.println(days); //13
新日期时间API出现的背景:
可变性:像日期和时间这样的类应该是不可变的。偏移性:Date中的年份是从1900开始的,而月份都是从0开始。格式化:格式化只对Date有用,Calendar则不行。
此外,他们也不是线程安全的;不能处理闰秒。
说明:LocalDateTime类相较于其他两个类使用频率较高。
(1)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);
(2)of():设置指定时间的年、月、日、时、分。没有偏移量。(实例化方法二)
//of():设置指定时间的年、月、日、时、分。没有偏移量。LocalDateTime localDateTime2 = LocalDateTime.of(2021, 4, 13, 15, 20);System.out.println(localDateTime2);
(3)getXxx():获取…(4)withXxx():修改(设置)…,这个方法不会改动原本的值。(5)plusXxx():添加(6)minusXxx():减
三、Instant(1)now():获取本初子午线对应的标准时间。(实例化方法一)
//now():获取本初子午线对应的标准时间Instant instant = Instant.now();System.out.println(instant);//添加时间的偏移量OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));System.out.println(offsetDateTime);
(2)toEpochMilli():获取毫秒数
long milli = instant.toEpochMilli();System.out.println(milli);
(3)ofEpochMilli():通过给定的毫秒数,获取Instant实例 (实例化方法二)
//通过给定的毫秒数,获取Instant实例Instant instant2 = Instant.ofEpochMilli(1618300028028l);System.out.println(instant2);四、DateTimeFormatter类
DateTimeFormatter类:格式化或解析日期、时间,类似于SimpleDateFormat。
(1)预定义的标准格式进行格式化:DateTimeFormatter.ISO_LOCAL_DATE_TIME
注意: 日期-----> 字符串
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;LocalDateTime localDateTime = LocalDateTime.now();String str = formatter.format(localDateTime);//注意类型变化System.out.println(localDateTime);System.out.println(str);
(2)本地化相关的格式。如:ofLocalizedDateTime()。
//FormatStyle.SHORT / FormatStyle.LONG / FormatStyle.MEDIUM :适用于LocalDateTime
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);LocalDateTime localDateTime = LocalDateTime.now();String str = formatter.format(localDateTime);System.out.println(localDateTime);System.out.println(str);
(3)自定义格式:ofPattern()
//自定义格式。如:DateTimeFormatter formatter = DateTimeFormatter.ofPattern('yyyy-MM-dd hh:mm:ss');String string = formatter.format(LocalDateTime.now());System.out.println(string);
到此这篇关于java8新特性之日期时间API的文章就介绍到这了,更多相关Java日期时间API内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!
相关文章: