You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

components-datefield.asciidoc 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. ---
  2. title: Date and Time Input with DateField
  3. order: 13
  4. layout: page
  5. ---
  6. [[components.datefield]]
  7. = Date and Time Input with [classname]#DateField#
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/data-input/dates/popup-date-field"]
  11. endif::web[]
  12. The [classname]#DateField# component provides the means to display and input
  13. date and time. The field comes in two variations: [classname]#PopupDateField#,
  14. with a numeric input box and a popup calendar view, and
  15. [classname]#InlineDateField#, with the calendar view always visible. The
  16. [classname]#DateField# base class defaults to the popup variation.
  17. The example below illustrates the use of the [classname]#DateField# baseclass,
  18. which is equivalent to the [classname]#PopupDateField#. We set the initial time
  19. of the date field to current time by using the default constructor of the
  20. [classname]#java.util.Date# class.
  21. [source, java]
  22. ----
  23. // Create a DateField with the default style
  24. DateField date = new DateField();
  25. // Set the date and time to present
  26. date.setValue(new Date());
  27. ----
  28. The result is shown in <<figure.components.datefield.basic>>.
  29. [[figure.components.datefield.basic]]
  30. .[classname]#DateField# ([classname]#PopupDateField#) for Selecting Date and Time
  31. image::img/datefield-example1.png[width=35%, scaledwidth=60%]
  32. [[components.datefield.popupdatefield]]
  33. == [classname]#PopupDateField#
  34. The [classname]#PopupDateField# provides date input using a text box for the
  35. date and time. As the [classname]#DateField# defaults to this component, the use
  36. is exactly the same as described earlier. Clicking the handle right of the date
  37. opens a popup view for selecting the year, month, and day, as well as time. Also
  38. the Down key opens the popup. Once opened, the user can navigate the calendar
  39. using the cursor keys.
  40. The date and time selected from the popup are displayed in the text box
  41. according to the default date and time format of the current locale, or as
  42. specified with [methodname]#setDateFormat()#. The same format definitions are
  43. used for parsing user input.
  44. [[components.datefield.popupdatefield.format]]
  45. === Date and Time Format
  46. The date and time are normally displayed according to the default format for the
  47. current locale (see
  48. <<dummy/../../../framework/components/components-features#components.features.locale,"Locale">>).
  49. You can specify a custom format with [methodname]#setDateFormat()#. It takes a
  50. format string that follows the format of the [classname]#SimpleDateFormat# in
  51. Java.
  52. [source, java]
  53. ----
  54. // Display only year, month, and day in ISO format
  55. date.setDateFormat("yyyy-MM-dd");
  56. ----
  57. The result is shown in <<figure.components.datefield.popupdatefield.format>>.
  58. [[figure.components.datefield.popupdatefield.format]]
  59. .Custom Date Format for [classname]#PopupDateField#
  60. image::img/datefield-formatting.png[width=35%, scaledwidth=60%]
  61. The same format specification is also used for parsing user-input date and time,
  62. as described later.
  63. ifdef::web[]
  64. [[components.datefield.popupdatefield.malformed]]
  65. === Handling Malformed User Input
  66. A user can easily input a malformed or otherwise invalid date or time.
  67. [classname]#DateField# has two validation layers: first on the client-side and
  68. then on the server-side.
  69. The validity of the entered date is first validated on the client-side,
  70. immediately when the input box loses focus. If the date format is invalid, the
  71. [literal]#++v-datefield-parseerror++# style is set. Whether this causes a
  72. visible indication of a problem depends on the theme. The built-in
  73. [literal]#++reindeer++# theme does not shown any indication by default, making
  74. server-side handling of the problem more convenient.
  75. [source, css]
  76. ----
  77. .mydate.v-datefield-parseerror .v-textfield {
  78. background: pink;
  79. }
  80. ----
  81. The [methodname]#setLenient(true)# setting enables relaxed interpretation of
  82. dates, so that invalid dates, such as February 30th or March 0th, are wrapped to
  83. the next or previous month, for example.
  84. The server-side validation phase occurs when the date value is sent to the
  85. server. If the date field is set in immediate state, it occurs immediately after
  86. the field loses focus. Once this is done and if the status is still invalid, an
  87. error indicator is displayed beside the component. Hovering the mouse pointer
  88. over the indicator shows the error message.
  89. You can handle the errors by overriding the
  90. [methodname]#handleUnparsableDateString()# method. The method gets the user
  91. input as a string parameter and can provide a custom parsing mechanism, as shown
  92. in the following example.
  93. [source, java]
  94. ----
  95. // Create a date field with a custom parsing and a
  96. // custom error message for invalid format
  97. PopupDateField date = new PopupDateField("My Date") {
  98. @Override
  99. protected Date handleUnparsableDateString(String dateString)
  100. throws Property.ConversionException {
  101. // Try custom parsing
  102. String fields[] = dateString.split("/");
  103. if (fields.length >= 3) {
  104. try {
  105. int year = Integer.parseInt(fields[0]);
  106. int month = Integer.parseInt(fields[1])-1;
  107. int day = Integer.parseInt(fields[2]);
  108. GregorianCalendar c =
  109. new GregorianCalendar(year, month, day);
  110. return c.getTime();
  111. } catch (NumberFormatException e) {
  112. throw new Property.
  113. ConversionException("Not a number");
  114. }
  115. }
  116. // Bad date
  117. throw new Property.
  118. ConversionException("Your date needs two slashes");
  119. }
  120. };
  121. // Display only year, month, and day in slash-delimited format
  122. date.setDateFormat("yyyy/MM/dd");
  123. // Don't be too tight about the validity of dates
  124. // on the client-side
  125. date.setLenient(true);
  126. ----
  127. The handler method must either return a parsed [classname]#Date# object or throw
  128. a [classname]#ConversionException#. Returning [parameter]#null# will set the
  129. field value to [parameter]#null# and clear the input box.
  130. endif::web[]
  131. ifdef::web[]
  132. [[components.datefield.popupdatefield.error-customization]]
  133. === Customizing the Error Message
  134. In addition to customized parsing, overriding the handler method for unparseable
  135. input is useful for internationalization and other customization of the error
  136. message. You can also use it for another way for reporting the errors, as is
  137. done in the example below:
  138. [source, java]
  139. ----
  140. // Create a date field with a custom error message for invalid format
  141. PopupDateField date = new PopupDateField("My Date") {
  142. @Override
  143. protected Date handleUnparsableDateString(String dateString)
  144. throws Property.ConversionException {
  145. // Have a notification for the error
  146. Notification.show(
  147. "Your date needs two slashes",
  148. Notification.TYPE_WARNING_MESSAGE);
  149. // A failure must always also throw an exception
  150. throw new Property.ConversionException("Bad date");
  151. }
  152. };
  153. ----
  154. If the input is invalid, you should always throw the exception; returning a
  155. [parameter]#null# value would make the input field empty, which is probably
  156. undesired.
  157. endif::web[]
  158. [[components.datefield.popupdatefield.prompt]]
  159. === Input Prompt
  160. Like other fields that have a text box, [classname]#PopupDateField# allows an
  161. input prompt that is visible until the user has input a value. You can set the
  162. prompt with [methodname]#setInputPrompt#.
  163. [source, java]
  164. ----
  165. PopupDateField date = new PopupDateField();
  166. // Set the prompt
  167. date.setInputPrompt("Select a date");
  168. // Set width explicitly to accommodate the prompt
  169. date.setWidth("10em");
  170. ----
  171. The date field doesn't automatically scale to accommodate the prompt, so you
  172. need to set it explicitly with [methodname]#setWidth()#.
  173. The input prompt is not available in the [classname]#DateField# superclass.
  174. [[components.datefield.popupdatefield.css]]
  175. === CSS Style Rules
  176. [source, css]
  177. ----
  178. .v-datefield, v-datefield-popupcalendar {}
  179. .v-textfield, v-datefield-textfield {}
  180. .v-datefield-button {}
  181. ----
  182. The top-level element of [classname]#DateField# and all its variants have
  183. [literal]#++v-datefield++# style. The base class and the
  184. [classname]#PopupDateField# also have the
  185. [literal]#++v-datefield-popupcalendar++# style.
  186. In addition, the top-level element has a style that indicates the resolution,
  187. with [literal]#++v-datefield-++# basename and an extension, which is one of
  188. [literal]#++full++#, [literal]#++day++#, [literal]#++month++#, or
  189. [literal]#++year++#. The [literal]#++-full++# style is enabled when the
  190. resolution is smaller than a day. These styles are used mainly for controlling
  191. the appearance of the popup calendar.
  192. The text box has [literal]#++v-textfield++# and
  193. [literal]#++v-datefield-textfield++# styles, and the calendar button
  194. [literal]#++v-datefield-button++#.
  195. Once opened, the calendar popup has the following styles at the top level:
  196. [source, css]
  197. ----
  198. .v-datefield-popup {}
  199. .v-popupcontent {}
  200. .v-datefield-calendarpanel {}
  201. ----
  202. The top-level element of the floating popup calendar has
  203. [literal]#++.v-datefield-popup++# style. Observe that the popup frame is outside
  204. the HTML structure of the component, hence it is not enclosed in the
  205. [literal]#++v-datefield++# element and does not include any custom styles.
  206. // NOTE: May be changed in #5752.
  207. The content in the [literal]#++v-datefield-calendarpanel++# is the same as in
  208. [classname]#InlineDateField#, as described in <<components.datefield.calendar>>.
  209. [[components.datefield.calendar]]
  210. == [classname]#InlineDateField#
  211. The [classname]#InlineDateField# provides a date picker component with a month
  212. view. The user can navigate months and years by clicking the appropriate arrows.
  213. Unlike with the pop-up variant, the month view is always visible in the inline
  214. field.
  215. [source, java]
  216. ----
  217. // Create a DateField with the default style
  218. InlineDateField date = new InlineDateField();
  219. // Set the date and time to present
  220. date.setValue(new java.util.Date());
  221. ----
  222. The result is shown in <<figure.components.datefield.inlinedatefield>>.
  223. [[figure.components.datefield.inlinedatefield]]
  224. .Example of the [classname]#InlineDateField#
  225. image::img/datefield-inlinedatefield.png[width=35%, scaledwidth=60%]
  226. The user can also navigate the calendar using the cursor keys.
  227. === CSS Style Rules
  228. [source, css]
  229. ----
  230. .v-datefield {}
  231. .v-datefield-calendarpanel {}
  232. .v-datefield-calendarpanel-header {}
  233. .v-datefield-calendarpanel-prevyear {}
  234. .v-datefield-calendarpanel-prevmonth {}
  235. .v-datefield-calendarpanel-month {}
  236. .v-datefield-calendarpanel-nextmonth {}
  237. .v-datefield-calendarpanel-nextyear {}
  238. .v-datefield-calendarpanel-body {}
  239. .v-datefield-calendarpanel-weekdays,
  240. .v-datefield-calendarpanel-weeknumbers {}
  241. .v-first {}
  242. .v-last {}
  243. .v-datefield-calendarpanel-weeknumber {}
  244. .v-datefield-calendarpanel-day {}
  245. .v-datefield-calendarpanel-time {}
  246. .v-datefield-time {}
  247. .v-select {}
  248. .v-label {}
  249. ----
  250. The top-level element has the [literal]#++v-datefield++# style. In addition, the
  251. top-level element has a style name that indicates the resolution of the
  252. calendar, with [literal]#++v-datefield-++# basename and an extension, which is
  253. one of [literal]#++full++#, [literal]#++day++#, [literal]#++month++#, or
  254. [literal]#++year++#. The [literal]#++-full++# style is enabled when the
  255. resolution is smaller than a day.
  256. The [literal]#++v-datefield-calendarpanel-weeknumbers++# and
  257. [literal]#++v-datefield-calendarpanel-weeknumber++# styles are enabled when the
  258. week numbers are enabled. The former controls the appearance of the weekday
  259. header and the latter the actual week numbers.
  260. The other style names should be self-explanatory. For weekdays, the
  261. [literal]#++v-first++# and [literal]#++v-last++# styles allow making rounded
  262. endings for the weekday bar.
  263. [[components.datefield.resolution]]
  264. == Date and Time Resolution
  265. In addition to display a calendar with dates, [classname]#DateField# can also
  266. display the time in hours and minutes, or just the month or year. The visibility
  267. of the input components is controlled by __time resolution__, which you can set
  268. with [methodname]#setResolution()#. The method takes as its parameters the
  269. lowest visible component, [parameter]#DateField.Resolution.DAY# for just dates
  270. and [parameter]#DateField.Resolution.MIN# for dates with time in hours and
  271. minutes. Please see the API Reference for the complete list of resolution
  272. parameters.
  273. [[components.datefield.locale]]
  274. == DateField Locale
  275. The date and time are displayed according to the locale of the user, as reported
  276. by the browser. You can set a custom locale with the [methodname]#setLocale()#
  277. method of [classname]#AbstractComponent#, as described in
  278. <<dummy/../../../framework/components/components-features#components.features.locale,"Locale">>.
  279. Only Gregorian calendar is supported.
  280. ifdef::web[]
  281. [[components.datefield.weeknumbers]]
  282. == Week Numbers
  283. You can enable week numbers in a date field with
  284. [methodname]#setShowISOWeekNumbers()#. The numbers are shown in a column on the
  285. left side of the field.
  286. [source, java]
  287. ----
  288. df.setShowISOWeekNumbers(true);
  289. ----
  290. The supported numbering is defined in the ISO 8601 standard. Note that the ISO
  291. standard applies only to calendar locales where the week starts on Monday. This
  292. is not the case in many countries, such as Americas (North and South), many
  293. East-Asian countries, and some African countries, where the week starts on
  294. Sunday, nor in some North African and Middle-Eastern countries, where the week
  295. begins on Saturday. In such locales, the week numbers are not displayed.
  296. endif::web[]