2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > LocalDate LocalTime LocalDateTime常用方法

LocalDate LocalTime LocalDateTime常用方法

时间:2023-09-30 00:51:42

相关推荐

LocalDate LocalTime LocalDateTime常用方法

LocalDate 常用方法

public class DemoLocalDate {public static void main(String[] args) {// 当前日期:-10-16LocalDate localDate = LocalDate.now();// 获取localDate对象 根据参数设置日期,参数分别为年,月,日LocalDate date2 = LocalDate.of(, 10, 1);LocalDate date3 = LocalDate.parse("-9-10");// 获取当前日期年份:System.out.println(localDate.getYear());// 获取当前日期月份对象:OCTOBERSystem.out.println(localDate.getMonth());// 获取当前日期月份:10System.out.println(localDate.getMonthValue());// 获取该日期是当前周的第几天:3System.out.println(localDate.getDayOfWeek().getValue());// 获取该日期是当前月的第几天:16System.out.println(localDate.getDayOfMonth());// 获取该日期是当前年的第几天:289System.out.println(localDate.getDayOfYear());// 将参数中的"年份"替换localTime中的"年份":-10-16System.out.println(localDate.withYear());// 将参数中的"月份"替换localTime中的"月份":-12-16System.out.println(localDate.withMonth(12));// 将参数中的"日期"替换localTime中的"日期":-10-01System.out.println(localDate.withDayOfMonth(1));// 判断是否是闰年:falseSystem.out.println(localDate.isLeapYear());// 获取当前年份有多少天:365System.out.println(localDate.lengthOfYear());// 获取当前月份有多少天:31System.out.println(localDate.lengthOfMonth());// 比较该日期与other日期的大小,返回正数,那么当前对象时间较晚(数字较大):15System.out.println(pareTo(LocalDate.of(, 10, 1)));// 比较该日期是否比参数日期早(true为早):trueSystem.out.println(localDate.isBefore(LocalDate.of(,10,18)));// 比较该日期是否比参数日期晚(true为晚):falseSystem.out.println(localDate.isAfter(LocalDate.of(,10,18)));// 比较两个日期是否相等:trueSystem.out.println(localDate.isEqual(LocalDate.now()));// 当前日期增加指定的年数:-10-16System.out.println(localDate.plusYears(1));// 当前日期增加指定的月份:-12-16System.out.println(localDate.plusMonths(2));// 当前日期增加指定的周数:-10-30System.out.println(localDate.plusWeeks(2));// 当前日期增加指定的天数:-10-23System.out.println(localDate.plusDays(7));// 当前日期减少指定的年数:-10-16System.out.println(localDate.minusYears(1));// 当前日期减少指定的月份:-06-16System.out.println(localDate.minusMonths(4));// 当前日期减少指定的周数:-10-09System.out.println(localDate.minusWeeks(1));// 当前日期减少指定的天数:-10-01System.out.println(localDate.minusDays(15));// LocalDate 转 StringDateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");String dateString = localDate.format(dateTimeFormatter);// String 转 LocalDateString str = "-10-01";DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");LocalDate date = LocalDate.parse(str, fmt);// Date 转 LocalDateDate now = new Date();// 先将java.util.Date转换为ZonedDateTimeInstant 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 System.out.println(now);// -10-16System.out.println(toLocalDate);// LocalDate 转 DateZoneId 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:18LocalTime 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时间的大小,返回正数,那么当前对象时间较晚(数字较大):17System.out.println(pareTo(LocalTime.of(15,29, 1)));//比较该时间是否比参数时间早(true为早):falseSystem.out.println(localTime.isBefore(LocalTime.of(15,29, 1)));//比较该时间是否比参数时间晚(true为晚):trueSystem.out.println(localTime.isAfter(LocalTime.of(15,29, 1)));//比较两个时间是否相等(true为早):trueSystem.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 LocalDateTimeMAX:支持的最大本地日期时间(不包括时区)LocalDateTime max = LocalDateTime.MAX;// +999999999-12-31T23:59:59.999999999// static LocalDateTimeMIN:支持的最小本地日期时间(不包括时区)LocalDateTime min = LocalDateTime.MIN;// -999999999-01-01T00:00// static LocalDateTimenow():获取本地当前的日期时间(不含时区)LocalDateTime now = LocalDateTime.now(); // -10-17T15:44:03.640// static LocalDateTimenow(Clock clock):从指定的时钟获取当前日期时间:这里用了UTC(世界标准时间)LocalDateTime nowClock = LocalDateTime.now(Clock.systemUTC()); // -10-17T07:57:42.404// static LocalDateTimenow(ZoneId zone):从指定时区中的系统时钟获取当前日期时间LocalDateTime nowZoneId = LocalDateTime.now(ZoneId.systemDefault()); // -10-17T16:09:04.127// static LocalDateTimeof(int year, int month, int dayOfMonth, int hour, int minute)// 从年,月,日,小时和分钟获取LocalDateTime的实例,并将秒和毫秒设置为零。LocalDateTime dateTime = LocalDateTime.of(, 10, 12, 6, 30); // -10-12T06:30// static LocalDateTimeof(int year, int month, int dayOfMonth, int hour, int minute, int second)// static LocalDateTimeof(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)// static LocalDateTimeof(int year, Month month, int dayOfMonth, int hour, int minute)LocalDateTime localDateTime = LocalDateTime.of(, Month.OCTOBER, 12, 6, 30); // -10-12T06:30// static LocalDateTimeof(int year, Month month, int dayOfMonth, int hour, int minute, int second)// static LocalDateTimeof(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)// static LocalDateTimeof(LocalDate date, LocalTime time):根据日期和时间获取LocalDateTime的实例LocalDateTime ldt = LocalDateTime.of(LocalDate.now(), LocalTime.now()); // -10-17T16:33:42.714// static LocalDateTimeofEpochSecond(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 LocalDateTimeofInstant(Instant instant, ZoneId zone)// 从Instant和区域ID获取LocalDateTime的实例。LocalDateTime ofInstant = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()); // -10-17T16:54:50.941// static LocalDateTimeparse(CharSequence text)// 将字符串转化成日期时间对象LocalDateTime parse = LocalDateTime.parse("-10-17T16:54:50.941"); // -10-17T16:54:50.941// static LocalDateTimeparse(CharSequence text, DateTimeFormatter formatter)// 使用特定格式化程序将文本字符串转成LocalDateTime对象LocalDateTime parse1 = LocalDateTime.parse("-10-17T16:54:50.941+01:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME); // -10-17T16:54:50.941// LocalDateTime plus(long amountToAdd, TemporalUnit unit)// 在该日期时间基础上增加或减少一定时间,参数1:时间量,可以是负数,参数2:时间单位LocalDateTime plus = parse.plus(1L, ChronoUnit.HOURS); // -10-17T17:54:50.941// LocalDateTime plus(TemporalAmount amountToAdd):在该时间基础上增加一定时间LocalDateTime plus1 = parse.plus(Period.ofDays(1)); // -10-18T16:54:50.941// LocalDateTime plusDays(long days):增加指定天数LocalDateTime plus2 = parse.plusDays(1L); // -10-18T16:54:50.941// LocalDateTimeplusHours(long hours):增加指定小时数// LocalDateTimeplusMinutes(long minutes):增加指定分钟数// LocalDateTimeplusMonths(long months):增加指定月份数// LocalDateTimeplusNanos(long nanos):增加指定毫秒数// LocalDateTimeplusSeconds(long seconds):增加指定秒数// LocalDateTimeplusWeeks(long weeks):增加指定周数// LocalDateTimeplusYears(long years):增加指定年数}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。