龙空技术网

jdk8Date LocalDateTime类学习笔记

小丛的知识窝 234

前言:

如今同学们对“oracle格式化日期eee”大致比较重视,小伙伴们都需要分析一些“oracle格式化日期eee”的相关内容。那么小编也在网摘上网罗了一些有关“oracle格式化日期eee””的相关内容,希望朋友们能喜欢,咱们一起来学习一下吧!

jdk8日期Date LocalDate LocalTime LocalDateTime类学习笔记jdk8以前Date

-- public Date():创建当前系统的此刻日期时间对象。

-- public long getTime():返回自 1970 年 1 月 1 日 00:00:00以来走过的总的毫秒数。 时间记录的两种方式: a.Date日期对象。 b.时间毫秒值:从1970-01-01 00:00:00开始走到此刻的总的毫秒值。 1s = 1000ms

Date d = new Date(); b.时间毫秒值: long time = d.getTime();Date d = new Date(); System.out.println(d); // 获取当前时间毫秒值 long time = d.getTime(); System.out.println(time);

输出结果

有参构造器的使用

public static void main(String[] args) { // 1、得到日期对象 Date d = new Date(); System.out.println(d); // 2、得到时间毫秒值 对时间毫秒值进行计算 long time = d.getTime() + 100 * 1000; //3、将时间毫秒值通过 有参构造器转换成日期对象 Date d1 = new Date(time); System.out.println(d1); // 4、也可以给日期对象设置新的时间毫秒值 d.setTime(time); System.out.println(d); }

输出结果

SimpleDateFormat时间日期格式化工具

DateFormat作用:

Date日期对象或者时间毫秒值的时间形式不便于我们查看当前日期,我们可以通过SimpleDateFormat来定制时间格式

1.可以把“日期对象”或者“时间毫秒值”格式化成我们喜欢的时间形式。(格式化时间)

2.可以把字符串的时间形式解析成日期对象。(解析字符串时间)

DateFormat使用方法

DateFormat是一个抽象类,不能直接使用,要找它的子类:SimpleDateFormat

SimpleDateFormat简单日期格式化类

构造器:

public SimpleDateFormat(String pattern): 指定时间的格式创建简单日期格式化对象。

方法:

public String format(Date date):可以把日期对象格式化成我们喜欢的时间形式,返回的是字符串!public String format(Object time):可以把时间毫秒值格式化成我们喜欢的时间形式,返回的是字符串!public Date parse(String date) throws ParseException: 把字符串的时间解析成日期对象

public static void main(String[] args) { // 1、得到此刻日期对象 Date d = new Date(); System.out.println(d); // 2、创建简单日期格式化对象,用于格式化时间成为字符串形式 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE a"); String result = sdf.format(d); System.out.println(result); }
输出结果jdk8以后LocalDate
public static void main(String[] args) {// 1、获取计算机当前日期的对象。LocalDate nowDate = LocalDate.now();System.out.println("本机的当前日期:" + nowDate);//本机的当前日期int year = nowDate.getYear();//获取当前日期的年System.out.println("年year:" + year);int month = nowDate.getMonthValue(); //获取当前日期的月System.out.println("月month:" + month);int day = nowDate.getDayOfMonth();//当前日期是本月的第几天(实在不知道怎么表达了)System.out.println("日day:" + day);int dayOfYear = nowDate.getDayOfYear(); //当天日期所在的日是这一年中的第几天System.out.println("dayOfYear:" + dayOfYear);System.out.println(nowDate.getDayOfWeek());//获取星期,返回的是英文星期System.out.println(nowDate.getDayOfWeek().getValue());//获取星期返货的是数字星期System.out.println(nowDate.getMonth());//获取月份,返回的是英文月份System.out.println(nowDate.getMonth().getValue());//获取月份,返回的是数值月份System.out.println("------------------------设定自定义日期------------------------");LocalDate customDate = LocalDate.of(1949, 10, 1); //创建一个自定义的日期对象System.out.println(customDate);//直接传入对应的年月日System.out.println(LocalDate.of(1949, Month.OCTOBER, 1));//月份也可以使用枚举类型}

输出结果

LocalTime

public static void main(String[] args) {// 1、获取计算机当前时间对象。LocalTime nowTime = LocalTime.now();System.out.println("计算机的当前时间:" + nowTime);//计算机的当前时间:int hour = nowTime.getHour();//获取计算机当前时间的时System.out.println("时hour:" + hour);//hour:int minute = nowTime.getMinute();//获取计算机当前时间的分System.out.println("分minute:" + minute);//minute:int second = nowTime.getSecond();//获取计算机当前时间的秒System.out.println("秒second:" + second);//second:int nano = nowTime.getNano();//获取计算机当前时间的纳秒System.out.println("纳秒nano:" + nano);//nano:System.out.println("-----");System.out.println(LocalTime.of(8, 20));//返回时间对象八点二十分,并输出System.out.println(LocalTime.of(8, 20, 30));//返回时间对象八点二十分30秒,并输出System.out.println(LocalTime.of(8, 20, 30, 150));//返回时间对象八点二十分钟50纳秒,并输出LocalTime mTime = LocalTime.of(8, 20, 30, 150); //返回时间对象System.out.println("---------------");System.out.println(LocalDateTime.of(1949, 10, 1, 8, 20));//可以返回指定时间对象System.out.println(LocalDateTime.of(1949, Month.OCTOBER, 1, 8, 20));System.out.println(LocalDateTime.of(1949, 10, 1, 8, 20, 30));System.out.println(LocalDateTime.of(1949, Month.OCTOBER, 1, 8, 20, 30));System.out.println(LocalDateTime.of(1949, 10, 1, 8, 20, 30, 150));System.out.println(LocalDateTime.of(1949, Month.OCTOBER, 1, 8, 20, 30, 150));}

输出结果

LocalDateTime

public static void main(String[] args) {//返回一个该计算机当前的包含日期和时间对象LocalDateTime nowDateTime = LocalDateTime.now();System.out.println("计算机的当前日期是:" + nowDateTime);//可以直接输出日期和时间System.out.println(nowDateTime.getYear()+"年");//返回当前的年System.out.println(nowDateTime.getMonthValue()+"月");//返回当前的月System.out.println(nowDateTime.getDayOfMonth()+"日");//返回当前的日System.out.println(nowDateTime.getHour()+"时");//放回当前时间的时System.out.println(nowDateTime.getMinute()+"分");//返回当前时间的分System.out.println(nowDateTime.getSecond()+"秒");//返回当前时间的秒System.out.println(nowDateTime.getNano()+"纳秒");//返回当前时间的纳秒//返回当前日期是一年中的第几天System.out.println("dayOfYear:" + nowDateTime.getDayOfYear());//dayOfYear:249//返回英文的星期System.out.println(nowDateTime.getDayOfWeek());//THURSDAYSystem.out.println(nowDateTime.getDayOfWeek().getValue());//返回阿拉伯数字的星期//设定一个周日的日历LocalDateTime myDateTime = LocalDateTime.of(2021, 3, 14,0,0,0,0);//星期日用阿拉伯数字7表示System.out.println(myDateTime.getDayOfWeek().getValue());//返回当前的月份,返回值是英文System.out.println(nowDateTime.getMonth());//SEPTEMBER//返回当前月份用阿拉伯数字表示System.out.println(nowDateTime.getMonth().getValue());//9//从包含日期和时间的LocalDateTime对象中提取出只包含日期的LocalDate对象LocalDate ld = nowDateTime.toLocalDate();System.out.println("今天是"+ld);//从包含日期和时间的LocalDateTime对象中提取出只包含日期的LocalTime对象LocalTime lt = nowDateTime.toLocalTime();System.out.println("现在是"+lt.getHour()+"时");System.out.println(lt.getMinute()+"分");System.out.println(lt.getSecond()+"秒");}

输出结果

LocalDateTime格式化输出

public static void main(String[] args) {// 获取计算机当前的日期时间 对象LocalDateTime ldt = LocalDateTime.now();System.out.println(ldt);// 创建 解析/格式化 对象DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EEE a");// 可以把日期时间对象作为参数给 解析格式化对象String ldtStr1 = dtf.format(ldt);System.out.println(ldtStr1);//2021-03-15 20:43:45 周一 下午// 逆向格式化//也可以用日期时间对象调用format方法 将格式对象作为参数传入String ldtStr = ldt.format(dtf);System.out.println(ldtStr);//2021-03-15 20:43:45 周一 下午//将字符串日期时间解析成 日期时间对象DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");String dateStr = "2021-03-20 20:43:45";// 解析当前字符串时间成为本地日期时间对象LocalDateTime ldt1 = LocalDateTime.parse(dateStr, dtf1);System.out.println(ldt1);}

输出结果

日期的加减比较操作

注意

LocalTime.now();返回的时间对象在进行加减操作时,原有时间不改变,会返回新的时间对象

public static void main(String[] args) {//获取计算机当前时间的对象LocalTime nowTime = LocalTime.now();System.out.println(nowTime);//输出当前的时间//在当前时间减少一小时LocalTime minOneHour = nowTime.minusHours(1);System.out.println("减少一小时的时间是"+minOneHour);//时间的加减会返回一个新的对象,不会改变 nowTime的时间System.out.println("减小一小时后,nowTime的时间不会变还是"+nowTime);System.out.println(nowTime.minusMinutes(1));//一分钟前System.out.println(nowTime.minusSeconds(1));//一秒前System.out.println(nowTime.minusNanos(1));//一纳秒前System.out.println(nowTime.plusHours(1));//一小时后System.out.println(nowTime.plusMinutes(1));//一分钟后System.out.println(nowTime.plusSeconds(1));//一秒后System.out.println(nowTime.plusNanos(1));//一纳秒后//两个时间的计算System.out.println("---------------两个时间的计算---------------");LocalDate myDate = LocalDate.of(1949, 10, 1);LocalDate nowDate = LocalDate.now();//判断两个日期是否为相同System.out.println("今天是1949-10-01吗? " + nowDate.equals(myDate));//今天是1949-10-01吗? falseSystem.out.println(myDate + "是否在" + nowDate + "之前? " + myDate.isBefore(nowDate));//1949-10-01是否在当前时间之前? trueSystem.out.println(myDate + "是否在" + nowDate + "之后? " + myDate.isAfter(nowDate));//1949-10-01是否在当前时间之后? falseSystem.out.println("---------------判断当前日期是是否为生日------------");// 判断当前日期是是否为生日LocalDate birDate = LocalDate.of(1999, 10, 1);LocalDate nowDate1 = LocalDate.now();//设定一个2021年10月1日LocalDate nowDate2 = LocalDate.of(2021, 10, 1);//获取出生日期的月和日MonthDay birMd = MonthDay.of(birDate.getMonthValue(), birDate.getDayOfMonth());System.out.println("获取出生日期的月和日"+birMd);//从日期中获取月和日MonthDay nowMd = MonthDay.from(nowDate1);MonthDay nowMd2 = MonthDay.from(nowDate2);System.out.println("从日期中获取月和日"+nowMd);System.out.println("今天是设定日期的生日吗? " + birMd.equals(nowMd));//今天是你的生日吗? falseSystem.out.println("如果今天是10月1日是生日吗? " + birMd.equals(nowMd2));//今天是你的生日吗? false}

输出结果

Instant

Instant表示当前瞬时时间点,可以和Date LocalDateTime等时间类相互转化

public static void main(String[] args) {//获取当前的时间戳Instant instant = Instant.now();System.out.println("当前时间戳是:" + instant);//Instant可以转化为Date类Date date = Date.from(instant);System.out.println("当前时间戳是:" + date);//Date类可以转化为Instant类Instant instant1 = date.toInstant();System.out.println("当前时间戳是:" + instant1);//Instant可以转化成LocalDate类 默认返回的不是计算机所在时区的时间,需要传入第二个参数转化为当前时区的时间LocalDate ld = LocalDate.ofInstant(instant , ZoneId.systemDefault());System.out.println(ld);}

输出结果

period方法

public static void main(String[] args) {// 获取计算机当前的日期LocalDate today = LocalDate.now();System.out.println(today);////设定一个出生日期LocalDate birthDate = LocalDate.of(1955, 10, 1);System.out.println(birthDate);//将第二个参数传入的日期和第一个参数传入的日期做差 返回一个差值对象Period period = Period.between(birthDate, today);//第二个参数减第一个参数//返回两个日期的差值System.out.println("两个参数相差"+period.getYears()+"年"+period.getMonths()+"月"+period.getDays()+"天");}

输出结果

duration方法

public static void main(String[] args) {// 获取计算机当前的日期LocalDateTime today = LocalDateTime.now();System.out.println(today);// 出生的日期时间对象LocalDateTime birthDate = LocalDateTime.of(1999,03,15,20,00,00);System.out.println(birthDate);Duration duration = Duration.between(birthDate , today );//第二个参数减第一个参数System.out.println("相差"+duration.toDays()+"天");//两个时间差的天数System.out.println("相差"+duration.toHours()+"小时");//两个时间差的小时数System.out.println("相差"+duration.toMinutes()+"分");//两个时间差的分钟数System.out.println("相差"+duration.toMillis()+"毫秒");//两个时间差的毫秒数System.out.println("相差"+duration.toNanos()+"纳秒");//两个时间差的纳秒数}输出结果
ChronoUnit枚举类

ChronoUnit类可用于在单个时间单位内测量一段时间,这个工具类是最全的了,可以用于比较所有的时间单位

public static void main(String[] args) { // 获取计算机当前的日期 LocalDateTime today = LocalDateTime.now(); System.out.println(today); // 出生的日期时间对象 LocalDateTime birthDate = LocalDateTime.of(1999,03,15,20,00,00); System.out.println(birthDate); System.out.println("相差的年数:" + ChronoUnit.YEARS.between(birthDate, today)); System.out.println("相差的月数:" + ChronoUnit.MONTHS.between(birthDate, today)); System.out.println("相差的周数:" + ChronoUnit.WEEKS.between(birthDate, today)); System.out.println("相差的天数:" + ChronoUnit.DAYS.between(birthDate, today)); System.out.println("相差的时数:" + ChronoUnit.HOURS.between(birthDate, today)); System.out.println("相差的分数:" + ChronoUnit.MINUTES.between(birthDate, today)); System.out.println("相差的秒数:" + ChronoUnit.SECONDS.between(birthDate, today)); System.out.println("相差的毫秒数:" + ChronoUnit.MILLIS.between(birthDate, today)); System.out.println("相差的微秒数:" + ChronoUnit.MICROS.between(birthDate, today)); System.out.println("相差的纳秒数:" + ChronoUnit.NANOS.between(birthDate, today)); System.out.println("相差的半天数:" + ChronoUnit.HALF_DAYS.between(birthDate, today)); System.out.println("相差的十年数:" + ChronoUnit.DECADES.between(birthDate, today)); System.out.println("相差的世纪(百年)数:" + ChronoUnit.CENTURIES.between(birthDate, today)); System.out.println("相差的千年数:" + ChronoUnit.MILLENNIA.between(birthDate, today)); System.out.println("相差的纪元数:" + ChronoUnit.ERAS.between(birthDate, today)); }

输出结果

标签: #oracle格式化日期eee