Class Calendar

All Implemented Interfaces:
Animation, Editable, ActionSource, StyleListener, Iterable<Component>

public class Calendar extends Container implements ActionSource

Date widget for selecting a date/time value.

To localize strings for month names use the values "Calendar.Month" using the 3 first characters of the month name in the resource localization e.g. "Calendar.Jan", "Calendar.Feb" etc...

To localize strings for day names use the values "Calendar.Day" in the resource localization e.g. "Calendar.Sunday", "Calendar.Monday" etc...

Note that we recommend using the com.codename1.ui.spinner.Picker class which is superior when running on the device for most use cases.

Form hi = new Form("Calendar", new BorderLayout());
Calendar cld = new Calendar();
cld.addActionListener((e) -> Log.p("You picked: " + new Date(cld.getSelectedDay())));
hi.add(BorderLayout.CENTER, cld);
  • Constructor Details

    • Calendar

      public Calendar(long time)

      Creates a new instance of Calendar set to the given date based on time since epoch (the Date convention)

      Parameters
      • time: time since epoch
    • Calendar

      public Calendar()
      Constructs a calendar with the current date and time
    • Calendar

      public Calendar(long time, TimeZone tmz)

      Creates a new instance of Calendar set to the given date based on time since epoch (the Date convention)

      Parameters
      • time: time since epoch

      • tmz: a reference timezone

    • Calendar

      public Calendar(Image leftArrowImage, Image rightArrowImage)

      Constructs a calendar with the current date and time with left and right images set

      Parameters
      • leftArrowImage: an image for calendar left arrow

      • rightArrowImage: an image for calendar right arrow

    • Calendar

      public Calendar(long time, TimeZone tmz, Image leftArrowImage, Image rightArrowImage)

      Creates a new instance of Calendar set to the given date based on time since epoch (the Date convention)

      Parameters
      • time: time since epoch

      • tmz: a reference timezone

      • leftArrowImage: an image for calendar left arrow

      • rightArrowImage: an image for calendar right arrow

  • Method Details

    • getSelectedDay

      public long getSelectedDay()

      Returns the time for the current calendar.

      Returns

      the time for the current calendar.

    • getDate

      public Date getDate()

      Return the date object matching the current selection

      Returns

      the date object matching the current selection

    • setDate

      public void setDate(Date d)

      Sets the current date in the view and the selected date to be the same.

      Parameters
      • d: new date
    • setYearRange

      public void setYearRange(int minYear, int maxYear)

      Sets the Calendar min and max years

      Parameters
      • minYear: the min year

      • maxYear: the max year

    • setSelectedDate

      public void setSelectedDate(Date d)

      This method sets the Calendar selected day

      Parameters
      • d: the selected day
    • getCurrentDate

      public Date getCurrentDate()

      Returns the currently viewed date (as opposed to the selected date)

      Returns

      the currently viewed date

    • setCurrentDate

      public void setCurrentDate(Date d)

      Sets the Calendar view on the given date, only the the month and year are being considered.

      Parameters
      • d: the date to set the calendar view on.
    • getTimeZone

      public TimeZone getTimeZone()

      Gets the Calendar timezone

      Returns

      Calendar TimeZone

    • setTimeZone

      public void setTimeZone(TimeZone tmz)

      Sets the Calendar timezone, if not specified Calendar will use the default timezone

      Parameters
      • tmz: the timezone
    • getMonthViewSelectedStyle

      public Style getMonthViewSelectedStyle()

      Gets the selected style of the month view component within the calendar

      Returns

      the style of the month view

    • setMonthViewSelectedStyle

      public void setMonthViewSelectedStyle(Style s)

      Sets the selected style of the month view component within the calendar

      Parameters
      • s: style for the month view
    • getMonthViewUnSelectedStyle

      public Style getMonthViewUnSelectedStyle()

      Gets the un selected style of the month view component within the calendar

      Returns

      the style of the month view

    • setMonthViewUnSelectedStyle

      public void setMonthViewUnSelectedStyle(Style s)

      Sets the un selected style of the month view component within the calendar

      Parameters
      • s: style for the month view
    • addActionListener

      public void addActionListener(ActionListener l)

      Fires when a change is made to the month view of this component

      Parameters
      • l: listener to add
      Specified by:
      addActionListener in interface ActionSource
    • removeActionListener

      public void removeActionListener(ActionListener l)

      Fires when a change is made to the month view of this component

      Parameters
      • l: listener to remove
      Specified by:
      removeActionListener in interface ActionSource
    • addMonthChangedListener

      public void addMonthChangedListener(ActionListener l)

      Fires when a new month is selected

      Parameters
      • l: listener to add
    • removeMonthChangedListener

      public void removeMonthChangedListener(ActionListener l)

      Fires when a new month is selected

      Parameters
      • l: listener to remove
    • addDayActionListener

      public void addDayActionListener(ActionListener l)

      Adds an ActionListener to the day buttons. This is different from Calendar.addActionListener and will only fire when an active day is selected.

      Parameters
      • l: listener to add
    • removeDayActionListener

      public void removeDayActionListener(ActionListener l)

      Removes ActionListener from day buttons

      Parameters
      • l: listener to remove
    • addDataChangedListener

      public void addDataChangedListener(DataChangedListener l)

      Allows tracking selection changes in the calendar in real time

      Parameters
      • l: listener to add
    • removeDataChangedListener

      public void removeDataChangedListener(DataChangedListener l)

      Allows tracking selection changes in the calendar in real time

      Parameters
      • l: listener to remove
    • addDataChangeListener

      public void addDataChangeListener(DataChangedListener l)

      Allows tracking selection changes in the calendar in real time

      Parameters
      • l: listener to add
      Deprecated

      use #addDataChangedListener(DataChangedListener) instead

    • removeDataChangeListener

      public void removeDataChangeListener(DataChangedListener l)

      Allows tracking selection changes in the calendar in real time

      Parameters
      • l: listener to remove
      Deprecated

      use #removeDataChangedListener(DataChangedListener) instead

    • isChangesSelectedDateEnabled

      public boolean isChangesSelectedDateEnabled()

      This flag determines if selected date can be changed by selecting an alternative date

      Returns

      true if enabled

    • setChangesSelectedDateEnabled

      public void setChangesSelectedDateEnabled(boolean changesSelectedDateEnabled)

      This flag determines if selected date can be changed by selecting an alternative date

      Parameters
      • changesSelectedDateEnabled: @param changesSelectedDateEnabled if true pressing on a date will cause the selected date to be changed to the pressed one
    • createDay

      protected Button createDay()

      This method creates the Day Button Component for the Month View

      Returns

      a Button that corresponds to the Days Components

      Deprecated

      override createDayComponent() instead

    • createDayTitle

      protected Label createDayTitle(int day)

      This method creates the Day title Component for the Month View

      Parameters
      • day: the relevant day values are 0-6 where 0 is Sunday.
      Returns

      a Label that corresponds to the relevant Day

    • updateButtonDayDate

      protected void updateButtonDayDate(Component dayButton, int year, int currentMonth, int day)

      This method updates the Button day.

      Parameters
      • dayButton: the button to be updated

      • year: the current year

      • currentMonth: the current month

      • day: the new button day

    • updateButtonDayDate

      protected void updateButtonDayDate(Component dayButton, int currentMonth, int day)

      This method updates the Button day.

      Parameters
      • dayButton: the button to be updated

      • currentMonth: the current month

      • day: the new button day

    • updateButtonDayDate

      protected void updateButtonDayDate(Button dayButton, int year, int currentMonth, int day)

      This method updates the Button day.

      Parameters
      • dayButton: the button to be updated

      • year: the current year

      • currentMonth: the current month

      • day: the new button day

      Deprecated

      override the method that accepts a generic component

    • updateButtonDayDate

      protected void updateButtonDayDate(Button dayButton, int currentMonth, int day)

      This method updates the Button day.

      Parameters
      • dayButton: the button to be updated

      • currentMonth: the current month

      • day: the new button day

      Deprecated

      override the method that accepts a generic component

    • isTwoDigitMode

      public boolean isTwoDigitMode()

      When set to true days will be rendered as 2 digits with 0 preceding single digit days

      Returns

      the twoDigitMode

    • setTwoDigitMode

      public void setTwoDigitMode(boolean twoDigitMode)

      When set to true days will be rendered as 2 digits with 0 preceding single digit days

      Parameters
      • twoDigitMode: the twoDigitMode to set
    • getSelectedDays

      public Collection<Date> getSelectedDays()

      Gets the dates selected on the calendar or null if no date is selected

      Returns

      the selected days

    • setSelectedDays

      public void setSelectedDays(Collection<Date> selectedDays)

      Sets the dates to be selected on the calendar

      Parameters
      • selectedDays: the multipleDateSelection to set
    • setSelectedDays

      public void setSelectedDays(Collection<Date> selectedDays, String uiid)

      Sets the dates to be selected on the calendar with a custom uiid. To use default uiid "CalendarMultipleDay", call this method without the "uiid parameter"

      Parameters
      • selectedDays: the multipleDateSelection to set

      • uiid: a custom uiid to be used in the dates selected

    • getSelectedDaysUIID

      public String getSelectedDaysUIID()
      Returns

      selectedDays uiid

    • setSelectedDaysUIID

      public void setSelectedDaysUIID(String uiid)

      Sets the selectedDays UIID to the given uiid. being considered.

      Parameters
      • uiid: the uiid to change to
    • highlightDate

      public void highlightDate(Date date, String uiid)

      Highlights a date on the calendar using the supplied uiid. (Selected dates uiid takes precedence over highlighted dates uiid)

      Parameters
      • date: the date to be highlighted

      • uiid: a custom uiid to be used in highlighting the date

    • highlightDates

      public void highlightDates(Collection<Date> dates, String uiid)

      Highlights dates on the calendar using the supplied uiid. (Selected dates uiid takes precedence over highlighted dates uiid)

      Parameters
      • dates: the dates to be highlighted

      • uiid: a custom uiid to be used in highlighting the dates

    • unHighlightDates

      public void unHighlightDates(Collection<Date> dates)

      Un-highlights dates on the calendar by removing the highlighting uiid. (selectedDaysUIID uiid will be applied to any of the dates that are part of selectedDays)

      Parameters
      • dates: the dates to be un-highlighted
    • unHighlightDate

      public void unHighlightDate(Date date)

      Un-highlights dates on the calendar by removing the highlighting uiid. (selectedDaysUIID uiid will be applied to the date if it is part of selectedDays)

      Parameters
      • date: the date to be un-highlighted
    • isMultipleSelectionEnabled

      public boolean isMultipleSelectionEnabled()

      If true multiple days can be selected on a calendar and "getSelectedDays()" will return the dates selected

      Returns

      the multipleSelectionEnabled

    • setMultipleSelectionEnabled

      public void setMultipleSelectionEnabled(boolean multipleSelectionEnabled)

      When set to true multiple days can be selected on a calendar and "getSelectedDays()" will return the dates selected

      Parameters
      • multipleSelectionEnabled: the multipleSelectionEnabled to set
    • createDayComponent

      protected Component createDayComponent()

      Creates a day within the Calendar, this method is protected allowing Calendar to be subclassed to replace the rendering logic of individual day buttons.

      Returns

      a button representing the day within the Calendar

    • bindDayListener

      protected void bindDayListener(Component cmp, ActionListener l)

      Since a day may be any component type, developers should override this method to add support for binding the click listener to the given component.

      Parameters
      • l: listener interface

      • cmp: day component returned by createDayComponent()

    • setDayText

      protected void setDayText(Component cmp, String text)

      Since a day may be any component type, developers should override this method to add support for setting the displayed string.

      Parameters
      • text: the text set the component to

      • cmp: day component returned by createDayComponent()

    • getDayText

      protected String getDayText(Component cmp)

      Since a day may be any component type, developers should override this method to add support for removing the click listener from the given component.

      Parameters
      • cmp: day component returned by createDayComponent
      Returns

      the day text

    • setDayUIID

      protected void setDayUIID(Component cmp, String uiid)

      Since a day may be any component type, developers should override this method to add support for setting the right component's UIID.

      Parameters
      • cmp: day component returned by createDayComponent()

      • uiid: the text set the component to

    • setDayEnabled

      protected void setDayEnabled(Component cmp, boolean enable)

      Since a day may be any component type, developers should override this method to add support for enabling or disabling the right component.

      Parameters
      • cmp: day component returned by createDayComponent()

      • enable: the text set the component to