How can I convert an integer date to a formatted date string?
I have a column containing dates as integers (like 20250401). How can I convert these to formatted date strings (YYYY-MM-DD) in Deephaven?
The best practice is to first format the integer as a proper date string, then use the built-in parseLocalDate
function:
from deephaven import new_table
from deephaven.column import int_col
# Sample table with integer dates
t = new_table([int_col("DateInt", [20230101, 20240215, 20250401, 20260630])])
# Best practice: Format as string, then parse as LocalDate
result = t.update(
[
"Year = (int)(DateInt / 10000)",
"Month = (int)((DateInt % 10000) / 100)",
"Day = (int)(DateInt % 100)",
"DateString = String.format(`%04d-%02d-%02d`, Year, Month, Day)",
"DateFormatted = parseLocalDate(DateString)",
]
)
This approach works by:
- Extracting the year by dividing the integer by 10000 (e.g., 20250401 / 10000 = 2025).
- Extracting the month by taking the remainder after dividing by 10000, then dividing by 100 (e.g., 20250401 % 10000 = 401, then 401 / 100 = 4).
- Extracting the day by taking the remainder after dividing by 100 (e.g., 20250401 % 100 = 1).
- Using
java.lang.String.format
to create a properly formatted ISO date string (YYYY-MM-DD
). - Using
parseLocalDate
to convert the string to a properLocalDate
object.
This is the recommended approach because:
- It creates proper
LocalDate
objects that can be used for further date operations. - It handles date validation automatically through
parseLocalDate
. - It follows Deephaven best practices by using built-in parsing functions.
- The resulting
LocalDate
objects work seamlessly with other time-based functions.
Note
These FAQ pages contain answers to questions about Deephaven Community Core that our users have asked in our Community Slack. If you have a question that is not in our documentation, join our Community and we'll be happy to help!