You can split a clock value such as 22:12 into hour and minute values in OCL when you accept time text in a ViewModel textbox or prepare values for a derived, settable attribute.
Split an HH:mm string
Use split with a character array. OCL treats ':' as a String by default, while split expects a collection of characters. Convert the separator with toCharArray.
let parts = '22:12'.split(':'.toCharArray) in
(
Integer.parse(parts.at0(0)).asString + ' hours and ' +
Integer.parse(parts.at0(1)).asString + ' minutes'
)This expression returns:
22 hours and 12 minutes
| Expression | Meaning for 22:12
|
|---|---|
'22:12'.split(':'.toCharArray)
|
Splits the text at : into the two text parts 22 and 12.
|
parts.at0(0)
|
Gets the first part: 22.
|
parts.at0(1)
|
Gets the second part: 12.
|
Integer.parse(...)
|
Converts each text part to an Integer. |
at0 uses zero-based positions in this example: position 0 is the hour and position 1 is the minute.
Convert the parts to numeric values
Use parse when you need typed hour and minute values rather than display text. For example, the following expression produces a two-part text result after converting both values to Integer:
let parts = '08:05'.split(':'.toCharArray) in
(
Integer.parse(parts.at0(0)).asString + ':' +
Integer.parse(parts.at0(1)).asString
)Use the parsed Integers when you update the corresponding hour and minute portions of a stored date/time value. Keep the input contract explicit: this pattern expects two colon-separated numeric parts, such as 08:05.
Handle invalid textbox input
A textbox can contain incomplete or non-numeric text. Integer.parse returns NULL when text cannot be converted to an Integer. For example:
Integer.parse('hour').isNullThis returns true.
Check that the input has the expected two parts before you access at0(0) and at0(1), and check the parsed values before using them to update stored data. Inputs such as 22, 22:, and hour:minute do not meet the HH:mm contract.
For date/time conversion where the text may have more than one accepted format, use tryParse. It returns NULL for an invalid date or time rather than failing. strToDateTime is also available when you need to convert a String to a DateTime value.
Build a DateTime text value
If you need a DateTime representation rather than separate hour and minute values, combine the split parts with a date before converting it. This example builds text in the form 0001-01-01 HH:mm:
let parts = '22:12'.split(':'.toCharArray) in
(
'0001-01-01 ' + parts.at0(0) + ':' + parts.at0(1)
)Convert the resulting text with the appropriate date/time conversion operator for your model. Use tryParse when invalid user input must result in NULL, or strToDateTime when converting a String to DateTime.
Choose the right operation
| Need | Use | Example |
|---|---|---|
| Split a known colon-separated clock string | split | '22:12'.split(':'.toCharArray)
|
| Convert an hour or minute string to a number | Integer.parse | Integer.parse(parts.at0(0))
|
| Convert flexible date/time input safely | tryParse | Invalid input returns NULL. |
| Split on multiple or pattern-based separators | regExpSplit | Split text using a regular-expression pattern. |
| Work with a duration or TimeSpan text value | strToTime | Convert duration text for time arithmetic. |
