gpt4 book ai didi

java - 将日历日期转换为LocalDate

转载 作者:行者123 更新时间:2023-12-04 11:52:51 25 4
gpt4 key购买 nike

我决定从5.5升级到Optaplanner 7.5 Nurseroster,但遇到了许多愚蠢的问题。下面是一个。我以前使用的例程如下。现在,新版本需要LocalDate。我有一个MySql数据库后端,用户通过日历选择花名册计划计划器。任何建议将不胜感激。

int shiftDateSize = maxDayIndex + 1;
List<ShiftDate> shiftDateList = new ArrayList<ShiftDate>(shiftDateSize);
//shiftDateMap = new HashMap<String, ShiftDate>(shiftDateSize);
long id = 0L;
int dayIndex = 0;
calendar.setTime(startDate);
for (int i = 0; i < shiftDateSize; i++) {
ShiftDate shiftDate = new ShiftDate();
shiftDate.setId(id);
shiftDate.setDayIndex(dayIndex);
**String dateString = dateFormat.format(calendar.getTime());**
shiftDate.setDateString(dateString);
**shiftDate.setDayOfWeek(DayOfWeek.valueOfCalendar(calendar.get(Calendar.DAY_OF_WEEK)));**
shiftDate.setShiftList(new ArrayList<Shift>());
shiftDateList.add(shiftDate);
shiftDateMap.put(dateString, shiftDate);
id++;
dayIndex++;
calendar.add(Calendar.DAY_OF_YEAR, 1);
}
nurseRoster.setShiftDateList(shiftDateList);
}

最佳答案

我已经尝试使用以下代码从日历中获取 LocalDate

Calendar calendar = Calendar.getInstance();
TimeZone tz = calendar.getTimeZone();
ZoneId zid = tz == null ? ZoneId.systemDefault() : tz.toZoneId();
LocalDate localDate = LocalDateTime.ofInstant(calendar.toInstant(), zid).LocalDate();

或简单地一行:
LocalDate localDate = LocalDateTime.ofInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId()).toLocalDate();

我从此链接 Convert Calendar to LocalDateTime遵循了这些步骤

关于java - 将日历日期转换为LocalDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48983572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com