在JAVA中,常用的处理日期和时间的类主要有Date,Calendar,而在JDK1.8中,新增两个处理日期和时间的类,即LocalDate,LocalDataeTime下面我来介绍一下这两个类中常用方法的用法。
//获取当前日期
LocalDate now = LocalDate.now();
System.err.println("当前日期:"+now);
//打印:
当前日期:2019-06-06
// 将时间格式的字符转换为localDate对象
String str ="2008-02-02";
LocalDate localDate = LocalDate.parse(str, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
System.err.println(localDate);
//打印
2008-02-02
LocalDate localDate1 = LocalDate.of(2019, 3, 15);
System.err.println(localDate1);
//打印:
2019-03-15
String str ="2019-05-01";
LocalDate localDate = LocalDate.parse(str, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
LocalDate monday = localDate.with(DayOfWeek.MONDAY);
System.err.println(“(可能跨月)该年月的第一个周一为:”+monday);
//打印:
(可能跨月)该年月的第一个周一为:2019-04-29`
如果不跨月,就只取本月的第一个周一:
LocalDate monday = localDate.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
System.err.println("(不跨月)该年月的第一个周一为:"+monday);
//打印:
(不跨月)该年月的第一个周一为:2019-05-06
获取当前月份的最后一个周一:
LocalDate monday = localDate.with(TemporalAdjusters.lastInMonth(DayOfWeek.MONDAY));
System.err.println(monday);
//打印:
2019-05-27
获取当前月份第一个周日:
LocalDate sunday1 = localDate.with(DayOfWeek.SUNDAY);
LocalDate sunday2 = localDate.with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY));
System.err.println(sunday1);
System.err.println(sunday2);
//打印:上面两种方法都可以
2019-05-05
2019-05-05
获取当前月份最后一个周日:
LocalDate sunday = localDate.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY));
System.err.println(sunday);
//打印:
2019-05-26
通过上面的代码其实我们不难发现,这都是有规律的,我们仔细观察会发现,我们要获取一个月当中的第一个星期几时,用的是firstInMonth方法,后面跟上我们要查询的星期几,如果要获取一个月当中的最后一个星期几时,用的是lastMonth方法,后面同样跟上我们要查询的星期几。
如果需要跨月查询的话,用LocalDate monday = localDate.with(DayOfWeek.MONDAY);就好
LocalDate localDate1 = LocalDate.of(2019, 2, 8);
LocalDate localDate12 = LocalDate.of(2019, 3, 18);
long days = localDate12.toEpochDay() - localDate1.toEpochDay();
System.err.println(days);
//打印:
38
LocalDate localDate = LocalDate.of(2019, 2, 8);
Date day = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
System.err.println(day);
//打印:
Fri Feb 08 00:00:00 CST 2019
LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);//当天开始时间,如:2019-02-01 00:00:00
LocalDateTime today_end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);//当天结束时间,如:2019-02-01 23:59:59
//获取秒数
Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
//获取毫秒数
Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
//时间转字符串格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
String dateTime = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter);
//字符串转时间
String dateTimeStr = "2018-07-28 14:11:15";
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, df);
LocalDate nowLocalDate = LocalDate.now();
Date date = Date.from(nowLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
LocalDateTime localDateTime = LocalDateTime.now();
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
Date date = new Date();
LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
LocalDate localDate = LocalDate.now();
long timestamp = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
LocalDateTime localDateTime = LocalDateTime.now();
long timestamp = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
long timestamp = System.currentTimeMillis();
LocalDate localDate = Instant.ofEpochMilli(timestamp).atZone(ZoneId.systemDefault()).toLocalDate();
LocalDateTime localDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneId.systemDefault()).toLocalDateTime();
LocalDate date = new LocalDate();
//开始时间
LocalDateTime today_start = LocalDateTime.of(localDateStart, LocalTime.MIN);
Date start = Date.from(today_start.atZone(ZoneId.systemDefault()).toInstant());
//结束时间
LocalDateTime today_start = LocalDateTime.of(localDateStart, LocalTime.MAX);
//转为Date类型
Date start = Date.from(today_start.atZone(ZoneId.systemDefault()).toInstant());
LocalDate start = LocalDate.parse("2018-12-01");
LocalDate end = LocalDate.parse("2019-12-01");
//相差多少年
long year = start.until(end, ChronoUnit.YEARS);
//相差多少个月
long month = start.until(end, ChronoUnit.MONTHS);
相差多少天
long day = start.until(end, ChronoUnit.DAYS);
其实对于LocalDate的操作还有很多,我这里只列出了我在项目中用到一部分,以后项目中如果遇到了新的操作,我会继续更新在这里的。如果各位在这篇文章中没有找到想要的东西,就只有自己暂时去百度一下,对于LocalDate的操作百度上也是有很多的哈