String startDateDM5 = "2023-12-01 00:00:00"; String endDateDM5 = "2024-01-31 00:00:00";String date = "2025-3-04 00:00:00"; Date bizDate = new SimpleDateFormat("yyyy-MM-dd 00:00:00").parse(date); System.out.println(bizDate); Calendar calendarBiz = Calendar.getInstance(); calendarBiz.setTime(bizDate);怎么样让startDateDM5,endDateDM5的时间跟着bizdate的时间进行变动?
时间: 2024-03-05 12:51:45 浏览: 112
你可以使用Calendar类来进行时间的变动。
以下是一个示例代码:
```java
String startDateDM5 = "2023-12-01 00:00:00";
String endDateDM5 = "2024-01-31 00:00:00";
String date = "2025-3-04 00:00:00";
Date bizDate = new SimpleDateFormat("yyyy-MM-dd 00:00:00").parse(date);
System.out.println(bizDate);
Calendar calendarBiz = Calendar.getInstance();
calendarBiz.setTime(bizDate);
// 将startDateDM5的时间设置为bizDate的时间
Calendar calendarStartDate = Calendar.getInstance();
calendarStartDate.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startDateDM5));
calendarStartDate.set(Calendar.YEAR, calendarBiz.get(Calendar.YEAR));
calendarStartDate.set(Calendar.MONTH, calendarBiz.get(Calendar.MONTH));
calendarStartDate.set(Calendar.DAY_OF_MONTH, calendarBiz.get(Calendar.DAY_OF_MONTH));
startDateDM5 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendarStartDate.getTime());
// 将endDateDM5的时间设置为bizDate的时间
Calendar calendarEndDate = Calendar.getInstance();
calendarEndDate.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endDateDM5));
calendarEndDate.set(Calendar.YEAR, calendarBiz.get(Calendar.YEAR));
calendarEndDate.set(Calendar.MONTH, calendarBiz.get(Calendar.MONTH));
calendarEndDate.set(Calendar.DAY_OF_MONTH, calendarBiz.get(Calendar.DAY_OF_MONTH));
endDateDM5 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendarEndDate.getTime());
System.out.println("New startDateDM5: " + startDateDM5);
System.out.println("New endDateDM5: " + endDateDM5);
```
在这个示例中,我们首先将bizDate设置为指定的时间,然后使用Calendar类将startDateDM5和endDateDM5的时间设置为bizDate的时间。具体来说,我们首先将startDateDM5和endDateDM5解析为Date对象,然后使用Calendar类将它们的时间设置为bizDate的时间,最后再将它们转换为字符串。
阅读全文