Convert Calendar to LocalDate
Asked Answered
B

5

15

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);
    } 
Balsam answered 26/2, 2018 at 7:39 Comment(2)
Possible duplicate of Convert java.util.Date to java.time.LocalDateObverse
@Convert(converter = LocalDateTimeConverter.class) try itDutyfree
V
22

I have tried the following code to get the LocalDate from Calendar

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

or simply in one line:

LocalDate localDate = LocalDateTime.ofInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId()).toLocalDate();

I have followed these steps from this link Convert Calendar to LocalDateTime

Variolite answered 18/1, 2019 at 9:58 Comment(0)
C
10

In Java 8 you can just do

LocalDateTime.ofInstant(cal.toInstant(), ZoneId.systemDefault())
Complainant answered 26/8, 2020 at 10:43 Comment(0)
R
9

Java 9

import java.util.Calendar;
import java.time.LocalDate;
import java.time.ZoneId;

LocalDate.ofInstant(calendar.toInstant(), ZoneId.systemDefault());
Ratchford answered 22/10, 2020 at 13:16 Comment(1)
Android requires API 34 for this.Muskogee
S
4

The following code probably works too.

Calendar cal = Calendar.getInstance();
Date input = cal.getTime();
LocalDate da = input.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

This first gets the date in Date type and then converts it to LocalDate format as far as I know.

Spore answered 5/5, 2019 at 14:28 Comment(0)
V
0

If you're reading the LocalDate(Time) from your SQL database and is the only API connection point (so Calendar isn't exposed anywhere), then don't use Calendar at all.

Use a Hibernate/JPA or JDBC version that supports LocalDate(Time) and read that directly from the database. They've been supporting it for years, but your hibernate/JPA or JDBC version connecting to MySQL might be older.

It is error prone to read a date from the database as a Calendar and then convert the Calendar into a LocalDate(Time). java.util.Calendar is inherently broken, that's why LocalDate(Time) was created. IIRC, errors included dependence on OS settings (timezone, DST rules, etc), making it's behavior machine dependent.

If Calendar is exposed in your API, then this answer won't work, of course.

Vedavedalia answered 27/8, 2020 at 9:25 Comment(1)
Since Hibernate supported LocalDate other features regarding java.util.Date are broken. This is not funny: hibernate.atlassian.net/browse/HHH-11875Xenocryst

© 2022 - 2024 — McMap. All rights reserved.