Java SE基础知识(13)

知识梳理:

在这里插入图片描述


在这里插入图片描述

package LocalDate;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.MonthDay;
import java.util.Locale;

public class demo1 {
    public static void main(String[] args) {
        //1.获取当前时间的日历对象(年、月、日)
        LocalDate now = LocalDate.now();
        System.out.println(now);
        //2.获取指定时间的日历对象
        LocalDate localDate = LocalDate.of(1999, 2, 12);
        System.out.println(localDate);
        System.out.println("--------------------------------------------------------");
        //3.get系列获取日历中的每一个属性值
        //获取年
        int year = localDate.getYear();
        System.out.println(year);
        //获取月
        //方式一:
        Month month = localDate.getMonth();
        System.out.println(month);
        System.out.println(month.getValue());
        //方式二:
        int monthValue = localDate.getMonthValue();
        System.out.println(monthValue);
        System.out.println("--------------------------------------------------------");
        //获取日
        int dayOfMonth = localDate.getDayOfMonth();
        System.out.println(dayOfMonth);
        //获取一年中的第几天
        int dayOfYear = localDate.getDayOfYear();
        System.out.println(dayOfYear);
        //获取一周中的第几天
        DayOfWeek dayOfWeek = localDate.getDayOfWeek();
        System.out.println(dayOfWeek);
        System.out.println(dayOfWeek.getValue());
        System.out.println("--------------------------------------------------------");
        //is开头的方法表示判断
        LocalDate localDate1 = LocalDate.of(1999, 12, 27);
        System.out.println(localDate.isBefore(localDate1));
        System.out.println(localDate.isAfter(localDate1));
        System.out.println("--------------------------------------------------------");
        //with开头的方法表示修改,只能修改年月日
        LocalDate localDate2 = localDate.withYear(2024);
        System.out.println(localDate2);
        System.out.println("--------------------------------------------------------");
        //minus开头的方法表示减少,只能减少年月日
        LocalDate localDate3 = localDate.minusYears(1);
        System.out.println(localDate3);
        System.out.println("--------------------------------------------------------");
        //plus表示增加时间,只能增加年月日
        LocalDate localDate4 = localDate.plusYears(1);
        System.out.println(localDate4);
        System.out.println("--------------------------------------------------------");

        //应用:判断今天是否是你的生日
        MonthDay monthDay = MonthDay.of(localDate.getMonthValue(), localDate.getDayOfMonth());
        MonthDay from = MonthDay.from(now);
        System.out.println("今天是你生日吗?"+monthDay.equals(from));
    }
}
package localtime;

import java.time.LocalTime;

public class Demo1 {
    public static void main(String[] args) {
        //获取本地时间的日历对象(时、分、秒)
        LocalTime now = LocalTime.now();
        System.out.println(now);
        System.out.println("-----------------------------");
        int hour = now.getHour();
        int minute = now.getMinute();
        int second = now.getSecond();
        int nano = now.getNano();
        System.out.println(hour + ":" + minute + ":" + second + ":" + nano);
        System.out.println("-----------------------------");
        LocalTime localTime = LocalTime.of(1, 23);
        LocalTime localTime1 = LocalTime.of(1, 2, 3);
        LocalTime localTime2 = LocalTime.of(1, 2, 3, 4);
        //is系列
        System.out.println(localTime1.isBefore(localTime2));
        System.out.println(localTime1.isAfter(localTime2));
        System.out.println("-----------------------------");
        //with系列
        LocalTime localTime3 = localTime1.withHour(2);
        LocalTime localTime4 = localTime1.withMinute(2);
        LocalTime localTime5 = localTime1.withSecond(2);
        //minus
        LocalTime localTime6 = localTime1.minusHours(2);
        //plus
        LocalTime localTime7 = localTime1.plusHours(2);
    }
}

package localdatetime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class Demo {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        System.out.println("----------------------------");
        int year = now.getYear();
        //方式一
        //int month = now.getMonthValue();
        //方式二
        int month = now.getMonth().getValue();
        int day = now.getDayOfMonth();
        int hour = now.getHour();
        int minute = now.getMinute();
        int second = now.getSecond();
        System.out.println(year + "年" + month + "月" + day + "日" + hour + "时" + minute + "分" + second + "秒");
        System.out.println("----------------------------");
        //转换后就可以使用各自的方法了
        LocalDate localDate = now.toLocalDate();
        System.out.println(localDate);
        LocalTime localTime = now.toLocalTime();
        System.out.println(localTime);
    }
}


在这里插入图片描述

package period;

import java.time.LocalDate;
import java.time.Period;

public class Demo {
    public static void main(String[] args) {
        //当前本地 年月日
        LocalDate now = LocalDate.now();
        //生日的 年月日
        LocalDate localDate = LocalDate.of(1999, 2, 12);
        //第二个参数减第一个参数
        Period between = Period.between(localDate, now);
        System.out.println("相差的时间间隔的对象:"+between);
        System.out.println("相差的年数:"+between.getYears());
        System.out.println("相差的月数:"+between.getMonths());
        System.out.println("相差的天数:"+between.getDays());
        System.out.println("相差总月份:"+between.toTotalMonths());
    }
}

package duration;

import java.sql.SQLOutput;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;

public class Demo {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDateTime = LocalDateTime.of(1999, 2, 12, 0, 0, 0);
        //第二个参数减第一个参数
        Duration between = Duration.between(localDateTime, now);
        System.out.println(between);
        System.out.println("相差的时间间隔对象"+between);
        System.out.println("相差的毫秒数"+between.toMillis());
        System.out.println("相差的分钟数"+between.toMinutes());
        System.out.println("相差的小时数"+between.toHours());
        System.out.println("相差的天数"+between.toDays());
        System.out.println("相差的周数"+between.toDays()/7);
        System.out.println("相差的月数"+between.toDays()/30);
        System.out.println("相差的年数"+between.toDays()/365);
        System.out.println("相差的季度数"+between.toDays()/365/3);
    }
}

package chronounit;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class Demo {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDateTime = LocalDateTime.of(1999, 2, 12, 0, 0, 0);
        System.out.println("相差的年数:" + ChronoUnit.YEARS.between(localDateTime, now));
        System.out.println("相差的月数:" + ChronoUnit.MONTHS.between(localDateTime, now));
        System.out.println("相差的周数:" + ChronoUnit.WEEKS.between(localDateTime, now));
        System.out.println("相差的日数:" + ChronoUnit.DAYS.between(localDateTime, now));
        System.out.println("相差的小时数:" + ChronoUnit.HOURS.between(localDateTime, now));
        System.out.println("相差的分钟数:" + ChronoUnit.MINUTES.between(localDateTime, now));
        System.out.println("相差的秒数:" + ChronoUnit.SECONDS.between(localDateTime, now));
        System.out.println("相差的毫秒数:" + ChronoUnit.MILLIS.between(localDateTime, now));
        System.out.println("相差的微秒数:" + ChronoUnit.MICROS.between(localDateTime, now));
        System.out.println("相差的纳秒数:" + ChronoUnit.NANOS.between(localDateTime, now));
        System.out.println("相差的半天数:" + ChronoUnit.HALF_DAYS.between(localDateTime, now));
        System.out.println("相差的十年数:" + ChronoUnit.DECADES.between(localDateTime, now));
        System.out.println("相差的世纪数:" + ChronoUnit.CENTURIES.between(localDateTime, now));
        System.out.println("相差的千年数:" + ChronoUnit.MILLENNIA.between(localDateTime, now));
        System.out.println("相差的纪元数:" + ChronoUnit.ERAS.between(localDateTime, now));
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值