将日历日期转换为LocalDate [英] Convert Calendar date to LocalDate

查看:1276
本文介绍了将日历日期转换为LocalDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I decided to upgrade from 5.5 to Optaplanner 7.5 Nurseroster but have run into a number of silly issues. Below is one. The routine I previously used is below. Now however the new version requires LocalDate. I have an MySql database back-end and the users select the roster schedule planner via a calendar. Any suggestions will be appreciated.

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);
    } 

推荐答案

我尝试了以下代码从日历

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();

我已从此链接将日历转换为LocalDateTime

这篇关于将日历日期转换为LocalDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆