LocalDate的用法与String互转
·
一、LocalDate常用用法
1.1、申明定义
LocalDate formatDate = LocalDate.of(2020, 2, 5); // 自定义
LocalDate today = LocalDate.now(); // 获取当前日期
1.2、getX() 获取年月日等
注意:获取月份使用getMonthValue()
System.out.println(formatDate.getMonth()); // FEBRUARY 获取所在月份(英文)
System.out.println(formatDate.getMonthValue()); // 2 获取所在月份(英文)
System.out.println(formatDate.getDayOfMonth()); // 5 获取所在天
1.3、plusX()、minusX()
LocalDate nextDay = formatDate.plusDays(1);
System.out.println(nextDay); // 2020-02-06 明天
LocalDate nextWeek = formatDate.plusWeeks(1);
System.out.println(nextWeek); // 2020-02-12 下周当天
LocalDate lastMonth = formatDate.minusMonths(1);
System.out.println(lastMonth); // 2020-01-05 上月当天
1.4、扩展用法
常用于报表中计算同比环比等日期
// 同环比日期计算
LocalDate firstWeekDay = formatDate.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
System.out.println(firstWeekDay); // 2020-02-03 本周第一天
LocalDate firstMonthDay = formatDate.with(TemporalAdjusters.firstDayOfMonth());
System.out.println(firstMonthDay); // 2020-02-01 本月第一天
LocalDate firstYearDay = formatDate.with(TemporalAdjusters.firstDayOfYear());
System.out.println(firstYearDay); // 2020-01-01 本年第一天
// 判断两个日期前后
boolean param = formatDate.isBefore(today);
System.out.println(param); // true 判断a是否早于b
// 计算两个日期的间隔天数
LocalDate start = LocalDate.parse("2019-12-01");
LocalDate end = LocalDate.parse("2020-02-05");
long days = start.until(end, ChronoUnit.DAYS);
System.out.println("days: " + days); // days: 66
二、LocalDate与String互转
2.1、LocalDate转String
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate formatDate = LocalDate.of(2020, 2, 5);
String dateStr = formatDate.format(df);
System.out.println("LocalDate => String: " + dateStr);
2.2、String转LocalDate
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate dateParam = LocalDate.parse(dateStr, df);
System.out.println("String => LocalDate: " + dateParam);
2.3 固定格式输出时间字符串
/**
* 时间截取--日
*/
private String timeSubStringDay(LocalDateTime sendTime) {
String localDateTimeStr = sendTime.format(DateTimeFormatter.ofPattern("MM-dd HH:mm"));
return localDateTimeStr;
}
三、代码实践
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
/**
* LocalDate日期转换
*
* @author: Tansj
* @since: 2020/02/08
*/
public class Test {
public static void main(String[] args) {
LocalDate formatDate = LocalDate.of(2020, 2, 5);
LocalDate today = LocalDate.now();
System.out.println(formatDate); // 2020-02-05 当天
System.out.println(today); // 2020-02-08 本日当天
boolean param = formatDate.isBefore(today);
System.out.println(param); // true 判断a是否早于b
System.out.println("=========================================");
System.out.println(formatDate.getMonth()); // FEBRUARY 获取所在月份(英文)
System.out.println(formatDate.getMonthValue()); // 2 获取所在月份(数字)
System.out.println(formatDate.getDayOfMonth()); // 5 获取所在天
System.out.println("=========================================");
LocalDate nextDay = formatDate.plusDays(1);
System.out.println(nextDay); // 2020-02-06 明天
LocalDate nextWeek = formatDate.plusWeeks(1);
System.out.println(nextWeek); // 2020-02-12 下周当天
LocalDate lastMonth = formatDate.minusMonths(1);
System.out.println(lastMonth); // 2020-01-05 上月当天
System.out.println("=========================================");
LocalDate firstWeekDay = formatDate.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
System.out.println(firstWeekDay); // 2020-02-03 本周第一天
LocalDate firstMonthDay = formatDate.with(TemporalAdjusters.firstDayOfMonth());
System.out.println(firstMonthDay); // 2020-02-01 本月第一天
LocalDate firstYearDay = formatDate.with(TemporalAdjusters.firstDayOfYear());
System.out.println(firstYearDay); // 2020-01-01 本年第一天
System.out.println("=========================================");
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String dateStr = formatDate.format(df);
System.out.println("LocalDate => String: " + dateStr); // LocalDate => String: 2020-02-05
LocalDate dateParam = LocalDate.parse(dateStr, df);
System.out.println("String => LocalDate: " + dateParam); // String => LocalDate: 2020-02-05
LocalDate start = LocalDate.parse("2019-12-01");
LocalDate end = LocalDate.parse("2020-02-05");
long days = start.until(end, ChronoUnit.DAYS);
System.out.println("days: " + days); // days: 66
}
}
获取日期中的字符串 如"02-03"
String time = hydrologyVO.getSendTime().format(DateTimeFormatter.ofPattern("MM-dd"));
一、LocalDateTime转换至String互转
LocalDateTime localDateTime=LocalDateTime.parse(dates,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
String localDateTimeStr=LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
二、LocalDateTime转换至Long互转
Long localDateTimeLong=Timestamp.valueOf(LocalDateTime.now()).getTime();
LocalDateTime localDateTimeLongTime=LocalDateTime.ofInstant(Instant.ofEpochMilli(localDateTimeLong)
public static void main(String[] args) {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.now();
String dateStr = localDateTime.format(fmt);
System.out.println(dateStr);
}
输入string类型的日期:2023-12-04 输出这天的开始时间和结束时间:
2023-12-04 00:00:00 和 2023-12-04 23:59:59
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String dateString = "2023-12-04";
// 将字符串类型的日期转换为LocalDate对象
LocalDate date = LocalDate.parse(dateString);
// 获取一天的开始时间(00:00:00)
LocalTime startTime = LocalTime.MIN;
// 获取一天的结束时间(23:59:59)
LocalTime endTime = LocalTime.MAX;
// 使用自定义的格式化器对日期和时间进行格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println("一天的开始时间:" + date.atTime(startTime).format(formatter));
System.out.println("一天的结束时间:" + date.atTime(endTime).format(formatter));
}
}
现在,代码将按照指定的格式("yyyy-MM-dd HH:mm:ss")输出一天的开始时间和结束时间:
一天的开始时间:2023-12-04 00:00:00
一天的结束时间:2023-12-04 23:59:59
今天开始时间和结束时间
LocalDate now = LocalDate.now();
LocalDateTime startOfDay = now.atStartOfDay();
LocalDateTime endOfDay = now.atTime(LocalTime.MAX);
更多推荐
已为社区贡献3条内容
所有评论(0)