You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

com.codename1.ui.Calendar组件夏令时(DST)问题致日期选择错误

Codename One Calendar组件夏令时切换日期选择异常问题及修复方案

问题描述

使用Codename One的com.codename1.ui.Calendar组件时,若月份内发生夏令时(DST)切换,日期选择功能会出现异常。

当前setCurrentDay方法通过startDate += 86400000L;(添加固定24小时时长)来递增日期。夏令时切换时系统会调整时间(例如从凌晨1点变为3点),导致选中日期的时间出现偏移,进而引发以下错误行为:

  • 带有意外时间偏移的startDate与SELECTED_DAY对比时出错
  • selectedDays.contains(new Date(dates[j]))判断失效,因为dates[j]的时间可能偏移一小时

修复建议

建议不要手动添加固定的24小时时长,改用Calendar.add()方法来正确处理夏令时切换,确保日期计算遵循时区变化,避免时间偏移问题。

修正后的代码片段:

for (; j < components.length && (j - i + 1) <= lastDay; j++) {
    setDayEnabled(components[j], true);
    dates[j] = startDate;
    if (dates[j] == SELECTED_DAY) {
        setDayUIID(components[j], "CalendarSelectedDay");
        selected = components[j];
    } else {
        setDayUIID(components[j], "CalendarDay");
    }

    for (Map.Entry<String, Collection<Date>> entry : highlightGroup.entrySet()) {
        if (entry.getValue().contains(new Date(dates[j]))) {
            setDayUIID(components[j], entry.getKey());
        }
    }

    if (multipleSelectionEnabled) {
        if (selectedDays.contains(new Date(dates[j]))) {
            setDayUIID(components[j], selectedDaysUIID);
        }
    }
    updateButtonDayDate(components[j], yearNew, month, j - i + 1);
    //startDate += DAY; // 原有实现

    // 修复后的代码
    cal.setTime(new Date(startDate));
    cal.add(java.util.Calendar.DAY_OF_MONTH, 1);
    startDate = cal.getTime().getTime();
    // 修复代码结束
}

内容的提问来源于stack exchange,提问作者Oscar Naranjo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 20:22:44