DateTime, Date, DayOfWeek, Month, Year, YearMonth, TimeZone...
To represent instants with date and time information, there are two classes:
DateTimeclass is used to represent instants in UTC time. This class is
inlineand it is represented internally as a
Doublein a way that it is allocation-free on all targets including JS.
DateTimeTzclass is used to represent instants with an offset in a TimeZone. It includes a
DateTimeand a offset. And it is different from
Dateclass represent the Year+Month+Day part of an instant
Timeclass represent the Hour+Minute+Second+Millisecond part of an instant
val utc = DateTime.now() val local = DateTimeTz.nowLocal()
To get the current UTC Unix TimeStamp:
val unix = DateTime.now().unixMillis val unix = DateTime.nowUnix()
To construct a UTC date from an Unix TimeStamp:
val date = DateTime.fromUnix(unix)
val time = DateTime.now() val year: Year = time.year val year: Int = time.yearInt val month: Month = time.month val month0: Int = time.month0 val month1: Int = time.month1 val yearMonth: YearMonth = time.yearMonth val dayOfMonth: Int = time.dayOfMonth val dayOfWeek: DayOfWeek = time.dayOfWeek val dayOfWeek: Int = time.dayOfWeekInt val dayOfYear: Int = time.dayOfYear val hours: Int = time.hours val minutes: Int = time.minutes val seconds: Int = time.seconds val milliseconds: Int = time.milliseconds
From DateTime to Date & Time
val date: Date = dateTime.date val time: Time = dateTime.time
From DateTime to DateTimeTz
time.localUnadjusted time.toOffsetUnadjusted(offset: TimezoneOffset) time.local time.toOffset(offset: TimezoneOffset)
Formating and Parsing Dates
DateFormat interface allows to parse and format dates from/to Strings.
val dateFormat: DateFormat = DateFormat("EEE, dd MMM yyyy HH:mm:ss z") // Construct a new DateFormat from a String val date: DateTimeTz = dateFormat.parse("Sat, 08 Sep 2018 04:08:09 UTC") // Parse a Date from a String val dateStr: String = DateTime.now().format(dateFormat) // Format a Date using a specific DateFormat.
As for 1.0 Klock doesn’t have direct TimeZone support. But support offseted DateTime using
What Klock allows to do here is to get the UTC offset of the operating system TimeZone in a specific moment (having into account daylight changes when supported by the OS).
Klock allows to get Date information: from how many days has a month, to whether a year is leap, to which month will be in three years and a six months.
This UTC offset is represented by the class
TimezoneOffset that just wraps the
DayOfWeek is an enum with all seven days of the week:
- Constructing a DayOfWeek from an integer where Sunday=0:
- Getting index0 (sunday=0) and index1 (sunday=1) representations:
Month is an enum with all twelve months on it:
Months are a set of 28-31 days. The number of days of each month is always the same, except for
February that has 28 days in normal years, and 29 in leap years.
- Getting next and previous month (cyclic):
- Getting the number of days in a common and leap year:
- Getting the number of days for a specific year or a leap year:
month.days(leap = true)
- Getting a Month from a one-based representation (where January is 1 and December is 12), while wrapping outside numbers:
- Getting the month zero-based (January=0) and one-based (January=1) representation:
- Add or subtract months:
month + 11
Year class represents a normal Year and it is an inline class. It supports from year 1 to 9999 where the leap year formulas are valid.
- Construct a Year:
- Get the number of days of a specific year:
- Determine if a year is leap:
- How many days have passed since year 1 to the beginning of a specific year:
- Construct a year using the number of days since year 1:
YearMonth class is an inline class representing a pair of
Month. This pair has information about the number of days in a month
and can be useful to represent calendars.
- The current YearMonth:
- Number of days in a YearMonth:
- Number of days since the start of the year to reach the beginning of the month:
- Number of days since the start of the year to reach the end of the month:
- Get the components year and the month:
- Add or subtract months of years:
yearMonth - 1.years + 1.months