LocalDateTime

actual constructor(year: Int, month: Int, day: Int, hour: Int, minute: Int, second: Int, nanosecond: Int)(source)

Constructs a LocalDateTime instance from the given date and time components.

The components month and day are 1-based.

The supported ranges of components:

See also

for a version that returns null instead of throwing an exception when the parameters are invalid.

Throws

if any parameter is out of range or if day is invalid for the given monthNumber and year.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Constructing a LocalDateTime value using its constructor
val dateTime = LocalDateTime(
    year = 2024,
    month = 2,
    day = 15,
    hour = 16,
    minute = 48,
    second = 59,
    nanosecond = 999_999_999
)
check(dateTime.date == LocalDate(2024, 2, 15))
check(dateTime.time == LocalTime(16, 48, 59, 999_999_999))
val dateTimeWithoutSeconds = LocalDateTime(
    year = 2024,
    month = 2,
    day = 15,
    hour = 16,
    minute = 48
)
check(dateTimeWithoutSeconds.date == LocalDate(2024, 2, 15))
check(dateTimeWithoutSeconds.time == LocalTime(16, 48)) 
   //sampleEnd
}

actual constructor(year: Int, month: Month, day: Int, hour: Int, minute: Int, second: Int, nanosecond: Int)(source)

Constructs a LocalDateTime instance from the given date and time components.

The supported ranges of components:

See also

for a version that returns null instead of throwing an exception when the parameters are invalid.

Throws

if any parameter is out of range, or if day is invalid for the given month and year.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Constructing a LocalDateTime value using its constructor
val dateTime = LocalDateTime(
    year = 2024,
    month = Month.FEBRUARY,
    day = 15,
    hour = 16,
    minute = 48,
    second = 59,
    nanosecond = 999_999_999
)
check(dateTime.date == LocalDate(2024, Month.FEBRUARY, 15))
check(dateTime.time == LocalTime(16, 48, 59, 999_999_999))
val dateTimeWithoutSeconds = LocalDateTime(
    year = 2024,
    month = Month.FEBRUARY,
    day = 15,
    hour = 16,
    minute = 48
)
check(dateTimeWithoutSeconds.date == LocalDate(2024, Month.FEBRUARY, 15))
check(dateTimeWithoutSeconds.time == LocalTime(16, 48)) 
   //sampleEnd
}

actual constructor(date: LocalDate, time: LocalTime)(source)
expect constructor(year: Int, month: Int, day: Int, hour: Int, minute: Int, second: Int = 0, nanosecond: Int = 0)(source)

Constructs a LocalDateTime instance from the given date and time components.

The components month and day are 1-based.

The supported ranges of components:

See also

for a version that returns null instead of throwing an exception when the parameters are invalid.

Throws

if any parameter is out of range or if day is invalid for the given monthNumber and year.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Constructing a LocalDateTime value using its constructor
val dateTime = LocalDateTime(
    year = 2024,
    month = 2,
    day = 15,
    hour = 16,
    minute = 48,
    second = 59,
    nanosecond = 999_999_999
)
check(dateTime.date == LocalDate(2024, 2, 15))
check(dateTime.time == LocalTime(16, 48, 59, 999_999_999))
val dateTimeWithoutSeconds = LocalDateTime(
    year = 2024,
    month = 2,
    day = 15,
    hour = 16,
    minute = 48
)
check(dateTimeWithoutSeconds.date == LocalDate(2024, 2, 15))
check(dateTimeWithoutSeconds.time == LocalTime(16, 48)) 
   //sampleEnd
}

expect constructor(year: Int, month: Month, day: Int, hour: Int, minute: Int, second: Int = 0, nanosecond: Int = 0)(source)

Constructs a LocalDateTime instance from the given date and time components.

The supported ranges of components:

See also

for a version that returns null instead of throwing an exception when the parameters are invalid.

Throws

if any parameter is out of range, or if day is invalid for the given month and year.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Constructing a LocalDateTime value using its constructor
val dateTime = LocalDateTime(
    year = 2024,
    month = Month.FEBRUARY,
    day = 15,
    hour = 16,
    minute = 48,
    second = 59,
    nanosecond = 999_999_999
)
check(dateTime.date == LocalDate(2024, Month.FEBRUARY, 15))
check(dateTime.time == LocalTime(16, 48, 59, 999_999_999))
val dateTimeWithoutSeconds = LocalDateTime(
    year = 2024,
    month = Month.FEBRUARY,
    day = 15,
    hour = 16,
    minute = 48
)
check(dateTimeWithoutSeconds.date == LocalDate(2024, Month.FEBRUARY, 15))
check(dateTimeWithoutSeconds.time == LocalTime(16, 48)) 
   //sampleEnd
}

expect constructor(date: LocalDate, time: LocalTime)(source)

Constructs a LocalDateTime instance by combining the given date and time parts.

See also

for a version that returns null instead of throwing an exception when the parameters are invalid.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Converting a LocalDate and a LocalTime to a LocalDateTime value and getting them back
val date = LocalDate(2024, 2, 15)
val time = LocalTime(16, 48)
val dateTime = LocalDateTime(date, time)
check(dateTime.date == date)
check(dateTime.time == time)
check(dateTime == date.atTime(time))
check(dateTime == time.atDate(date)) 
   //sampleEnd
}
actual constructor(year: Int, month: Int, day: Int, hour: Int, minute: Int, second: Int, nanosecond: Int)(source)

Constructs a LocalDateTime instance from the given date and time components.

The components month and day are 1-based.

The supported ranges of components:

See also

for a version that returns null instead of throwing an exception when the parameters are invalid.

Throws

if any parameter is out of range or if day is invalid for the given monthNumber and year.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Constructing a LocalDateTime value using its constructor
val dateTime = LocalDateTime(
    year = 2024,
    month = 2,
    day = 15,
    hour = 16,
    minute = 48,
    second = 59,
    nanosecond = 999_999_999
)
check(dateTime.date == LocalDate(2024, 2, 15))
check(dateTime.time == LocalTime(16, 48, 59, 999_999_999))
val dateTimeWithoutSeconds = LocalDateTime(
    year = 2024,
    month = 2,
    day = 15,
    hour = 16,
    minute = 48
)
check(dateTimeWithoutSeconds.date == LocalDate(2024, 2, 15))
check(dateTimeWithoutSeconds.time == LocalTime(16, 48)) 
   //sampleEnd
}

actual constructor(year: Int, month: Month, day: Int, hour: Int, minute: Int, second: Int, nanosecond: Int)(source)

Constructs a LocalDateTime instance from the given date and time components.

The supported ranges of components:

See also

for a version that returns null instead of throwing an exception when the parameters are invalid.

Throws

if any parameter is out of range, or if day is invalid for the given month and year.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Constructing a LocalDateTime value using its constructor
val dateTime = LocalDateTime(
    year = 2024,
    month = Month.FEBRUARY,
    day = 15,
    hour = 16,
    minute = 48,
    second = 59,
    nanosecond = 999_999_999
)
check(dateTime.date == LocalDate(2024, Month.FEBRUARY, 15))
check(dateTime.time == LocalTime(16, 48, 59, 999_999_999))
val dateTimeWithoutSeconds = LocalDateTime(
    year = 2024,
    month = Month.FEBRUARY,
    day = 15,
    hour = 16,
    minute = 48
)
check(dateTimeWithoutSeconds.date == LocalDate(2024, Month.FEBRUARY, 15))
check(dateTimeWithoutSeconds.time == LocalTime(16, 48)) 
   //sampleEnd
}

actual constructor(date: LocalDate, time: LocalTime)(source)

Constructs a LocalDateTime instance by combining the given date and time parts.

See also

for a version that returns null instead of throwing an exception when the parameters are invalid.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Converting a LocalDate and a LocalTime to a LocalDateTime value and getting them back
val date = LocalDate(2024, 2, 15)
val time = LocalTime(16, 48)
val dateTime = LocalDateTime(date, time)
check(dateTime.date == date)
check(dateTime.time == time)
check(dateTime == date.atTime(time))
check(dateTime == time.atDate(date)) 
   //sampleEnd
}