LocalDate 常用方法
public class DemoLocalDate {
public static void main(String[] args) {
// 当前日期:2019-10-16
LocalDate localDate = LocalDate.now();
// 获取localDate对象 根据参数设置日期,参数分别为年,月,日
LocalDate date2 = LocalDate.of(2019, 10, 1);
LocalDate date3 = LocalDate.parse("2019-9-10");
// 获取当前日期年份:2019
System.out.println(localDate.getYear());
// 获取当前日期月份对象:OCTOBER
System.out.println(localDate.getMonth());
// 获取当前日期月份:10
System.out.println(localDate.getMonthValue());
// 获取该日期是当前周的第几天:3
System.out.println(localDate.getDayOfWeek().getValue());
// 获取该日期是当前月的第几天:16
System.out.println(localDate.getDayOfMonth());
// 获取该日期是当前年的第几天:289
System.out.println(localDate.getDayOfYear());
// 将参数中的"年份"替换localTime中的"年份":2022-10-16
System.out.println(localDate.withYear(2022));
// 将参数中的"月份"替换localTime中的"月份":2019-12-16
System.out.println(localDate.withMonth(12));
// 将参数中的"日期"替换localTime中的"日期":2019-10-01
System.out.println(localDate.withDayOfMonth(1));
// 判断是否是闰年:false
System.out.println(localDate.isLeapYear());
// 获取当前年份有多少天:365
System.out.println(localDate.lengthOfYear());
// 获取当前月份有多少天:31
System.out.println(localDate.lengthOfMonth());
// 比较该日期与other日期的大小,返回正数,那么当前对象时间较晚(数字较大):15
System.out.println(localDate.compareTo(LocalDate.of(2019, 10, 1)));
// 比较该日期是否比参数日期早(true为早):true
System.out.println(localDate.isBefore(LocalDate.of(2019,10,18)));
// 比较该日期是否比参数日期晚(true为晚):false
System.out.println(localDate.isAfter(LocalDate.of(2019,10,18)));
// 比较两个日期是否相等:true
System.out.println(localDate.isEqual(LocalDate.now()));
// 当前日期增加指定的年数:2020-10-16
System.out.println(localDate.plusYears(1));
// 当前日期增加指定的月份:2019-12-16
System.out.println(localDate.plusMonths(2));
// 当前日期增加指定的周数:2019-10-30
System.out.println(localDate.plusWeeks(2));
// 当前日期增加指定的天数:2019-10-23
System.out.println(localDate.plusDays(7));
// 当前日期减少指定的年数:2018-10-16
System.out.println(localDate.minusYears(1));
// 当前日期减少指定的月份:2019-06-16
System.out.println(localDate.minusMonths(4));
// 当前日期减少指定的周数:2019-10-09
System.out.println(localDate.minusWeeks(1));
// 当前日期减少指定的天数:2019-10-01
System.out.println(localDate.minusDays(15));
// LocalDate 转 String
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String dateString = localDate.format(dateTimeFormatter);
// String 转 LocalDate
String str = "2010-10-01";
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date = LocalDate.parse(str, fmt);
// Date 转 LocalDate
Date now = new Date();
// 先将java.util.Date转换为ZonedDateTime
Instant instant = now.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zonedDateTime = instant.atZone(zoneId);
// 使用它的toLocalDate()方法从ZonedDateTime获取LocalDate。
LocalDate toLocalDate = zonedDateTime.toLocalDate();
// Wed Oct 16 15:26:41 CST 2019
System.out.println(now);
// 2019-10-16
System.out.println(toLocalDate);
// LocalDate 转 Date
ZoneId id = ZoneId.systemDefault();
LocalDate now1 = LocalDate.now();
ZonedDateTime dateTime = now1.atStartOfDay(id);
Date date1 = Date.from(dateTime.toInstant());
System.out.println(date1);
}
}
LocalTime 常用方法
public class DemoLocalTime {
public static void main(String[] args) {
// 15:29:18
LocalTime localTime = LocalTime.now();
// 获取当前时间
System.out.println(localTime);
//根据参数设置时间,参数分别为时,分,秒
System.out.println(LocalTime.of(12,35,59));
//根据参数设置时间,参数分别为时,分
System.out.println(LocalTime.of(12,35));
//获取当前时间的小时数
System.out.println(localTime.getHour());
//获取当前时间的分钟数
System.out.println(localTime.getMinute());
//获取当前时间的秒数
System.out.println(localTime.getSecond());
//将参数中的"小时"替换localTime中的"小时"
System.out.println(localTime.withHour(1));
//将参数中的"分钟"替换localTime中的"分钟"
System.out.println(localTime.withMinute(1));
//将参数中的"秒"替换localTime中的"秒"
System.out.println(localTime.withSecond(1));
//比较该时间与other时间的大小,返回正数,那么当前对象时间较晚(数字较大):17
System.out.println(localTime.compareTo(LocalTime.of(15,29, 1)));
//比较该时间是否比参数时间早(true为早):false
System.out.println(localTime.isBefore(LocalTime.of(15,29, 1)));
//比较该时间是否比参数时间晚(true为晚):true
System.out.println(localTime.isAfter(LocalTime.of(15,29, 1)));
//比较两个时间是否相等(true为早):true
System.out.println(localTime.equals(LocalTime.now()));
//将当前时间减一小时
System.out.println(localTime.minusHours(1));
//将当前时间减一分钟
System.out.println(localTime.minusMinutes(1));
//将当前时间减一秒
System.out.println(localTime.minusSeconds(10));
//将当前时间加一小时
System.out.println(localTime.plusHours(1));
//将当前时间加一分钟
System.out.println(localTime.plusMinutes(1));
//将当前时间加一秒
System.out.println(localTime.plusSeconds(10));
}
}
LocalDateTime 常用方法
public class DemoLocalDateTime {
public static void main(String[] args) {
// static LocalDateTime MAX:支持的最大本地日期时间(不包括时区)
LocalDateTime max = LocalDateTime.MAX;// +999999999-12-31T23:59:59.999999999
// static LocalDateTime MIN:支持的最小本地日期时间(不包括时区)
LocalDateTime min = LocalDateTime.MIN;// -999999999-01-01T00:00
// static LocalDateTime now():获取本地当前的日期时间(不含时区)
LocalDateTime now = LocalDateTime.now(); // 2019-10-17T15:44:03.640
// static LocalDateTime now(Clock clock):从指定的时钟获取当前日期时间:这里用了UTC(世界标准时间)
LocalDateTime nowClock = LocalDateTime.now(Clock.systemUTC()); // 2019-10-17T07:57:42.404
// static LocalDateTime now(ZoneId zone):从指定时区中的系统时钟获取当前日期时间
LocalDateTime nowZoneId = LocalDateTime.now(ZoneId.systemDefault()); // 2019-10-17T16:09:04.127
// static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute)
// 从年,月,日,小时和分钟获取LocalDateTime的实例,并将秒和毫秒设置为零。
LocalDateTime dateTime = LocalDateTime.of(2019, 10, 12, 6, 30); // 2019-10-12T06:30
// static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)
// static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)
// static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute)
LocalDateTime localDateTime = LocalDateTime.of(2019, Month.OCTOBER, 12, 6, 30); // 2019-10-12T06:30
// static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second)
// static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)
// static LocalDateTime of(LocalDate date, LocalTime time):根据日期和时间获取LocalDateTime的实例
LocalDateTime ldt = LocalDateTime.of(LocalDate.now(), LocalTime.now()); // 2019-10-17T16:33:42.714
// static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset)
// 使用从1970-01-01T00:00:00Z的纪元开始的秒数获取LocalDateTime的实例,参数1:从纪元开始的秒数,参数2:毫秒数,参数3:时区
LocalDateTime time = LocalDateTime.ofEpochSecond(55L, 99999, ZoneOffset.UTC); // 1970-01-01T00:00:55.000099999
// static LocalDateTime ofInstant(Instant instant, ZoneId zone)
// 从Instant和区域ID获取LocalDateTime的实例。
LocalDateTime ofInstant = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()); // 2019-10-17T16:54:50.941
// static LocalDateTime parse(CharSequence text)
// 将字符串转化成日期时间对象
LocalDateTime parse = LocalDateTime.parse("2019-10-17T16:54:50.941"); // 2019-10-17T16:54:50.941
// static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter)
// 使用特定格式化程序将文本字符串转成LocalDateTime对象
LocalDateTime parse1 = LocalDateTime.parse("2019-10-17T16:54:50.941+01:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME); // 2019-10-17T16:54:50.941
// LocalDateTime plus(long amountToAdd, TemporalUnit unit)
// 在该日期时间基础上增加或减少一定时间,参数1:时间量,可以是负数,参数2:时间单位
LocalDateTime plus = parse.plus(1L, ChronoUnit.HOURS); // 2019-10-17T17:54:50.941
// LocalDateTime plus(TemporalAmount amountToAdd):在该时间基础上增加一定时间
LocalDateTime plus1 = parse.plus(Period.ofDays(1)); // 2019-10-18T16:54:50.941
// LocalDateTime plusDays(long days):增加指定天数
LocalDateTime plus2 = parse.plusDays(1L); // 2019-10-18T16:54:50.941
// LocalDateTime plusHours(long hours):增加指定小时数
// LocalDateTime plusMinutes(long minutes):增加指定分钟数
// LocalDateTime plusMonths(long months):增加指定月份数
// LocalDateTime plusNanos(long nanos):增加指定毫秒数
// LocalDateTime plusSeconds(long seconds):增加指定秒数
// LocalDateTime plusWeeks(long weeks):增加指定周数
// LocalDateTime plusYears(long years):增加指定年数
}
}