LocalDate、LocalTime、LocalDateTime 整理

/**
of - 静态工厂方法
parse - 静态工厂方法专注于解析
get - 获得某些东西的价值
is - 检查某些事情是否属实
with - 不可变的等价物
plus - 向对象添加金额
minus - 从对象中减去一定数量
to - 将此对象转换为另一种类型
at - 将此对象与另一个对象组合在一起
*/

// ---------------------------------LocalDate创建--------------------------------------------
System.out.println("----LocalDate----");
LocalDate localDate = LocalDate.now();
LocalDate localDate1 = LocalDate.now();
System.out.println(localDate);
LocalDate localDateParse = LocalDate.parse("2019-01-01");
System.out.println(localDateParse);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd");
LocalDate localDateParse1 = LocalDate.parse("2019年02月01", formatter);
System.out.println(localDateParse1);
LocalDate localDateOf = LocalDate.of(2019, 3, 1);
System.out.println(localDateOf);
LocalDate localDateOf1 = LocalDate.of(2019, Month.APRIL, 1);
System.out.println(localDateOf1);
LocalDate localDateOfYearDay = LocalDate.ofYearDay(2019, 121);
System.out.println(localDateOfYearDay);
// ---------------------------------LocalDate创建--------------------------------------------

// ---------------------------------LocalDate方法--------------------------------------------
System.out.println(LocalDate.MAX);
System.out.println(LocalDate.MIN);
System.out.println("0基于1970-01-01时间的转换参数加1日期就加1:" + LocalDate.ofEpochDay(0));
System.out.println("当前年:" + localDate.getYear());
System.out.println("当前月英文:" + localDate.getMonth());
System.out.println("当前月数字:" + localDate.getMonthValue());
System.out.println("当前天在一年中属于第几天:" + localDate.getDayOfYear());
System.out.println("当前天在一个月中属于第几天:" + localDate.getDayOfMonth());
System.out.println("当前天是星期几英文:" + localDate.getDayOfWeek());
System.out.println("返回日期所在的当月有多少天:" + localDate.lengthOfMonth());
System.out.println("返回日期所在的当年有多少天:" + localDate.lengthOfYear());
System.out.println("检查是否支持指定字段如下一行语句要先判断:" + localDate.isSupported(ChronoField.YEAR));
if (localDate.isSupported(ChronoField.YEAR)) {
System.out.println("根据字段获取值:" + localDate.get(ChronoField.YEAR));
}
System.out.println("检查年份是否为闰年:" + localDate.isLeapYear());
System.out.println("检查此日期是否在指定日期之后:" + localDate.isAfter(localDateParse));
System.out.println("检查此日期是否在指定日期之前:" + localDate.isBefore(localDateOf));
System.out.println("检查此日期是否等于指定日期:" + localDate.isEqual(localDate1));
System.out.println("使用指定的格式化程序格式化此日期:" + localDate.format(formatter));
System.out.println("等同于字符串比较:" + localDate.compareTo(localDateParse));
System.out.println("设置年份:" + localDate.withYear(2020));
System.out.println("设置月份:" + localDate.withMonth(6));
System.out.println("设置一年中的第几天:" + localDate.withDayOfYear(100));
System.out.println("设置当月的第几天:" + localDate.withDayOfMonth(10));
if (localDate.isSupported(ChronoField.YEAR)) {
System.out.println("指定的字段设置为新值:" + localDate.with(ChronoField.YEAR, 2020));
}
System.out.println("年份正数表示减去N年负数表示加上N年:" + localDate.minusYears(1));
System.out.println("月份正数表示减去N个月负数表示加上N个月:" + localDate.minusMonths(1));
System.out.println("周正数表示减去N周负数表示加上N周:" + localDate.minusWeeks(1));
System.out.println("天正数表示减去N天负数表示加上N天:" + localDate.minusDays(1));
System.out.println("年份正数表示加上N年负数表示减去N年:" + localDate.plusYears(1));
System.out.println("月份正数表示加上N月负数表示减去N月:" + localDate.plusMonths(1));
System.out.println("周正数表示加上N周负数表示减去N周:" + localDate.plusWeeks(1));
System.out.println("天正数表示加上N天负数表示减去N天:" + localDate.plusDays(1));

System.out.println("将此日期与午夜时间相结合,以LocalDateTime 在此日期的开头创建一个日期:" + localDate.atStartOfDay());
System.out.println("将此日期与创建时间的时间相结合LocalDateTime:" + localDate.atTime(10,10));
System.out.println("将此日期与创建时间的时间相结合LocalDateTime:" + localDate.atTime(10,10,10));
System.out.println("将此日期与创建时间的时间相结合LocalDateTime:" + localDate.atTime(10,10,10,10));
System.out.println();
// ---------------------------------LocalDate方法--------------------------------------------

// ---------------------------------LocalTime创建--------------------------------------------
System.out.println("----LocalTime----");
LocalTime localTime = LocalTime.now();
System.out.println(localTime);
LocalTime localTimeOf = LocalTime.of(01, 10);
System.out.println(localTimeOf);
LocalTime localTimeOf1 = LocalTime.of(02, 20, 20);
System.out.println(localTimeOf1);
LocalTime localTimeOf2 = LocalTime.of(03, 30, 30, 30);
System.out.println(localTimeOf2);
LocalTime localTimeParse = LocalTime.parse("04:40:40.400");
System.out.println(localTimeParse);
LocalTime localTimeParse1 = LocalTime.parse("05:50:50.500", DateTimeFormatter.ISO_LOCAL_TIME);
System.out.println(localTimeParse1);

// ---------------------------------LocalTime创建--------------------------------------------

// ---------------------------------LocalTime方法--------------------------------------------
System.out.println("支持的最大值LocalTime:" + LocalTime.MAX);
System.out.println("最低支持LocalTime:" + LocalTime.MIN);
System.out.println("一天开始时的午夜时间" + LocalTime.MIDNIGHT);
System.out.println("中午的时间:" + LocalTime.NOON);
System.out.println("小时:" + localTime.getHour());
System.out.println("分钟:" + localTime.getMinute());
System.out.println("秒:" + localTime.getSecond());
System.out.println("纳秒:" + localTime.getNano());
if (localTime.isSupported(ChronoField.HOUR_OF_DAY)) {
System.out.println("中午的MILLI_OF_DAY:" + localTime.get(ChronoField.HOUR_OF_DAY));
}
System.out.println("设置小时:" + localTime.withHour(1));
System.out.println("设置分钟:" + localTime.withMinute(10));
System.out.println("设置秒:" + localTime.withSecond(10));
System.out.println("设置纳秒:" + localTime.withNano(10));
System.out.println("小时正数表示减去N小时负数表示加上N小时:" + localTime.minusHours(1));
System.out.println("分钟时正数表示减去N分钟负数表示加上N分钟:" + localTime.minusMinutes(10));
System.out.println("秒正数表示减去N秒负数表示加上N秒:" + localTime.minusSeconds(10));
System.out.println("纳秒正数表示减去N纳秒负数表示加上N纳秒:" + localTime.minusNanos(10));
System.out.println("小时正数表示加上N小时负数表示减去N小时:" + localTime.plusHours(1));
System.out.println("分钟正数表示加上N分钟负数表示减去N分钟:" + localTime.plusMinutes(10));
System.out.println("秒正数表示加上N秒负数表示减去N秒:" + localTime.plusSeconds(10));
System.out.println("纳秒正数表示加上N纳秒负数表示减去N纳秒:" + localTime.plusNanos(10));
System.out.println("提取时间为一天中的秒:" + localTime.toSecondOfDay());
System.out.println("提取时间为一天中的毫微秒:" + localTime.toNanoOfDay());
System.out.println();
// ---------------------------------LocalTime方法--------------------------------------------

// ---------------------------------LocalDateTime创建--------------------------------------------
System.out.println("----LocalDateTime----");
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
LocalDateTime localDateTimeOf = LocalDateTime.of(localDate, localTime);
System.out.println(localDateTimeOf);
LocalDateTime localDateTimeParse = LocalDateTime.parse("2000-01-01T10:15:30");
System.out.println(localDateTimeParse);
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(localDateTimeParse.format(df));
LocalDateTime localDateTimeParse1 = LocalDateTime.parse("2019-01-01T10:15:30",DateTimeFormatter.ISO_LOCAL_DATE_TIME);
System.out.println(localDateTimeParse1);

// ---------------------------------LocalDateTime创建--------------------------------------------

// ---------------------------------LocalDateTime方法--------------------------------------------
System.out.println("LocalDate-LocalTime全部方法");
System.out.println("获取当前秒数:"+localDateTime.toEpochSecond(ZoneOffset.of("+8")));
System.out.println("获取当前时间毫秒数:"+localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli());
System.out.println(localDateTime.toLocalDate());
System.out.println(localDateTime.toLocalTime());

// ---------------------------------LocalDateTime方法--------------------------------------------

// ---------------------------------ChronoUnit方法--------------------------------------------
//两个时间按所给单位的差值
long decadesBetween = ChronoUnit.DECADES.between(localDateTime, localDateTimeParse);//以十年为单位
long yearsBetween = ChronoUnit.YEARS.between(localDateTime, localDateTimeParse);//年
long monthsBetween = ChronoUnit.MONTHS.between(localDateTime, localDateTimeParse);//月
long weeksBetween = ChronoUnit.WEEKS.between(localDateTime, localDateTimeParse);//周
long daysBetween = ChronoUnit.DAYS.between(localDateTime, localDateTimeParse);//天
long hoursBetween = ChronoUnit.HOURS.between(localDateTime, localDateTimeParse);//小时
long minutesBetween = ChronoUnit.MINUTES.between(localDateTime, localDateTimeParse);//分
long secondsBetween = ChronoUnit.SECONDS.between(localDateTime, localDateTimeParse);//秒
long millisBetween = ChronoUnit.MILLIS.between(localDateTime, localDateTimeParse);//毫秒
long microsBetween = ChronoUnit.MICROS.between(localDateTime, localDateTimeParse);//微秒
long nanosBetween = ChronoUnit.NANOS.between(localDateTime, localDateTimeParse);//纳秒

System.out.println("decadesBetween:"+decadesBetween);
System.out.println("yearsBetween:"+yearsBetween);
System.out.println("monthsBetween:"+monthsBetween);
System.out.println("weeksBetween:"+weeksBetween);
System.out.println("daysBetween:"+daysBetween);
System.out.println("hoursBetween:"+hoursBetween);
System.out.println("minutesBetween:"+minutesBetween);
System.out.println("secondsBetween:"+secondsBetween);
System.out.println("millisBetween:"+millisBetween);
System.out.println("microsBetween:"+microsBetween);
System.out.println("nanosBetween:"+nanosBetween);

ChronoUnit chronoUnit = ChronoUnit.DAYS;
System.out.println("检查此单位是否为日期单位:"+chronoUnit.isDateBased());
System.out.println("检查此单位是否为时间单位:"+chronoUnit.isTimeBased());
System.out.println("返回具有指定名称的此类型的枚举常量:"+ChronoUnit.valueOf(chronoUnit.name()));
System.out.println();
// ---------------------------------ChronoUnit方法--------------------------------------------

// ---------------------------------ZoneId方法--------------------------------------------
System.out.println("----ZoneId----");
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId);
System.out.println();
// ---------------------------------ZoneId方法--------------------------------------------

// ---------------------------------Clock方法--------------------------------------------
System.out.println("----Clock----");
Clock clock = Clock.systemDefaultZone();
System.out.println(clock);
Clock clock1 = Clock.systemUTC();
System.out.println(clock1);
Clock clock2 = Clock.tickMinutes(zoneId);
System.out.println(clock2);
System.out.println(clock.getZone());
System.out.println("毫秒数:"+clock.millis());
System.out.println();
// ---------------------------------Clock方法--------------------------------------------

// ---------------------------------Instant方法--------------------------------------------
System.out.println("----Instant----");
Instant clockInstant = clock.instant();
System.out.println(clockInstant);
Instant instant = Instant.now();
System.out.println(instant);
Instant instantParse = Instant.parse("2007-12-03T10:15:30.00Z");
System.out.println(instantParse);
Instant instantofEpochMilli = Instant.ofEpochMilli(System.currentTimeMillis());
System.out.println(instantofEpochMilli);
Instant instantofEpochSecond = Instant.ofEpochSecond(System.currentTimeMillis()/1000);
System.out.println(instantofEpochSecond);
System.out.println("毫秒数:"+instant.toEpochMilli());
System.out.println();
// ---------------------------------Instant方法--------------------------------------------

// ---------------------------------DayOfWeek方法--------------------------------------------
System.out.println("----DayOfWeek----");
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
System.out.println(dayOfWeek.getValue());
LocalDate lastMonday = localDate.minusDays(dayOfWeek.getValue() - 1);
System.out.println("当周第一天是:" + lastMonday.getDayOfMonth());
DayOfWeek dayOfWeek1 = DayOfWeek.of(1);
System.out.println(dayOfWeek1);
ValueRange vr = dayOfWeek.range(ChronoField.DAY_OF_WEEK);
System.out.println("取值范围:["+vr.getMinimum()+","+vr.getMaximum()+"]");
System.out.println();
// ---------------------------------DayOfWeek方法--------------------------------------------

// ---------------------------------WeekFields方法--------------------------------------------
System.out.println("----WeekFields----");
WeekFields weekFieldsOf = WeekFields.of(Locale.CHINA);
System.out.println(weekFieldsOf);
WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY, 7);//设置星期一为每周的第一天
System.out.println("当前是一年中第多少周:" + localDate.get(weekFields.weekOfWeekBasedYear()));
System.out.println(weekFieldsOf.getFirstDayOfWeek());
System.out.println(weekFieldsOf.getMinimalDaysInFirstWeek());
System.out.println(localDate.get(weekFieldsOf.dayOfWeek()));
System.out.println(localDate.get(weekFieldsOf.weekBasedYear()));
System.out.println(localDate.get(weekFieldsOf.weekOfMonth()));
System.out.println(localDate.get(weekFieldsOf.weekOfWeekBasedYear()));
System.out.println(localDate.get(weekFieldsOf.weekOfYear()));

// ---------------------------------WeekFields方法--------------------------------------------

 

ChronoField常量

ALIGNED_DAY_OF_WEEK_IN_MONTH 一个月内对齐的星期几。
ALIGNED_DAY_OF_WEEK_IN_YEAR 一年内对齐的星期几。
ALIGNED_WEEK_OF_MONTH 一个月内对齐的一周。
ALIGNED_WEEK_OF_YEAR 一年内对齐的一周。
AMPM_OF_DAY 每天上午。
CLOCK_HOUR_OF_AMPM 上午时钟小时。
CLOCK_HOUR_OF_DAY 时钟-小时。
DAY_OF_MONTH 这个月的日子。
DAY_OF_WEEK 星期几,例如星期二。
DAY_OF_YEAR 这一天。
EPOCH_DAY 该纪元日,基于1970-01-01(ISO)的Java时代。
ERA 时代。
HOUR_OF_AMPM 上午的时间。
HOUR_OF_DAY 一天的时间。
INSTANT_SECONDS 即时纪元。
MICRO_OF_DAY 微观的一天。
MICRO_OF_SECOND 微秒。
MILLI_OF_DAY 毫无疑问。
MILLI_OF_SECOND 毫秒。
MINUTE_OF_DAY 分钟。
MINUTE_OF_HOUR 分钟。
MONTH_OF_YEAR 一年中的月份,例如三月。
NANO_OF_DAY 纳米的一天。
NANO_OF_SECOND 纳秒秒。
OFFSET_SECONDS 与UTC/Greenwich的偏差。
PROLEPTIC_MONTH 基于预感月,从0年开始按月计数。
SECOND_OF_DAY 第二天。
SECOND_OF_MINUTE 第二分钟。
YEAR 即将到来的一年,如2012年。
YEAR_OF_ERA 这个时代的一年。

 

转载于:https://www.cnblogs.com/QQ80565970/p/10280708.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值