Nebula CDateTime Widget(Date/Time Patterns and Formats)

There are two methods for setting which components of a date and/or a time you want your users to be able to select: 1. the easy way; or 2. the other way.

1. The Easy Way: Specify a Format, and let the JRE figure out the pattern

CDateTime.setFormat accepts a single bit-wise OR-ed int corresponding to the three types of java.util.DateFormat:

CDateTime.setFormat(CDT.DATE_XXX)DateFormat.getDateInstance(DateFormat.XXX)
CDateTime.setFormat(CDT.TIME_XXX)DateFormat.getTimeInstance(DateFormat.XXX)
CDateTime.setFormat(CDT.DATE_XXX | CDT.TIME_YYY)
DateFormat.getDateTimeInstance(DateFormat.XXX, DateFormat.YYY)

CDT.java contains the following style constants:

DATE_SHORTTIME_SHORT
DATE_MEDIUMTIME_MEDIUM
DATE_LONGTIME_LONG (there is no corresponding DateFormat)

Example:
CDateTime cdt = new CDateTime(parent, CDT.BORDER | CDT.DROP_DOWN);
cdt.setFormat(CDT.TIME_SHORT);


Note that date and time styles can also be used in the constructor:

new CDateTime(parent, CDT.BORDER | CDT.DROP_DOWN | CDT.TIME_SHORT);

2. The Other Way: Specify the exact pattern

Under the covers, CDateTime uses java.text.SimpleDateFormat to format its text and assist in the navigation of its fields. Because of this, any pattern that can be passed to SimpleDateFormat can be used (refer to the SimpleDateFormat Javadoc)

Example:
CDateTime cdt = new CDateTime(parent, CDT.BORDER | CDT.DROP_DOWN);
cdt.setPattern("'Meeting on' EEEE, MMMM d '@' h:mm 'in the'a");


Caveats and Special Considerations:

  • Due to the two parameter paradigm of SWT there is no way to pass a pattern into the constructor
  • Although inc/dec is supported, typing characters currently is not (ie: typing 'w' above will not select 'wednesday')
  • Secondary Calendar Fields are linked: changing the 'day of the week' field will change the 'date' field
  • There may not be a graphical compliment to every text field! (file an enhancement request)