- 시간 기본 포맷 예제 String 표기
"yyyy-MM-dd HH:mm:ss.SSS"
1. 현재 시간을 unix timestamp로 확보하기
import java.time.Instant
fun getCurrentUnixTimestamp(): Long {
return Instant.now().epochSecond
}
// ##################################################
fun main() {
val currentUnixTimestamp = getCurrentUnixTimestamp()
println("Current Unix Timestamp: $currentUnixTimestamp")
}
2. 현재 시간을 LocalDateTime으로 확보하기
LocalDateTime.now()
import java.time.LocalDateTime
import java.time.ZoneOffset
fun getCurrentLocalDateTime(): LocalDateTime {
return LocalDateTime.now(ZoneOffset.UTC)
}
// ##################################################
fun main() {
val currentLocalDateTime = getCurrentLocalDateTime()
println("Current LocalDateTime: $currentLocalDateTime")
}
- 한국 서울 현재 시간 가져오기
import java.time.LocalDateTime
import java.time.ZoneId
data = LocalDateTime.now(ZoneId.of("Asia/Seoul"))
3. 현재 시간을 String으로 확보하기
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
fun getCurrentDateTimeAsString(): String {
val currentLocalDateTime = LocalDateTime.now(ZoneOffset.UTC)
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
return currentLocalDateTime.format(formatter)
}
// Recommendation
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
fun getCurrentKstTimeAsString(): String {
val currentLocalDateTime = LocalDateTime.now(ZoneId.of("Asia/Seoul"))
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
return currentLocalDateTime.format(formatter)
}
import java.time.Instant
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
fun currentUnixTimestampToString(): String {
val currentUnixTimestamp = Instant.now().epochSecond
val formatter = DateTimeFormatter.ISO_DATE_TIME
return formatter.format(Instant.ofEpochSecond(currentUnixTimestamp).atOffset(ZoneOffset.UTC))
}
4. LocalDateTime → unix timestamp & string
- LocalDateTime → Unix Timestamp
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneOffset
fun localDateTimeToUnixTimestamp(localDateTime: LocalDateTime): Long {
return localDateTime.toInstant(ZoneOffset.UTC).epochSecond
}
import java.time.LocalDateTime
import java.time.ZoneOffset
fun customDateTimeToUnixTimestamp(year: Int, month: Int, day: Int, hour: Int, minute: Int, second: Int): Long {
val customDateTime = LocalDateTime.of(year, month, day, hour, minute, second)
return customDateTime.toInstant(ZoneOffset.UTC).epochSecond
}
- LocalDateTime → String
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
fun localDateTimeToString(localDateTime: LocalDateTime): String {
val formatter = DateTimeFormatter.ISO_DATE_TIME // ISO 8601 형식 - ex. 2020-07-10 15:00:00.000
return localDateTime.format(formatter)
}
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
fun localDateTimeToStringWithFormat(localDateTime: LocalDateTime, format: String): String {
val formatter = DateTimeFormatter.ofPattern(format) // format 사용자 지정
return localDateTime.format(formatter)
}
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.Locale
fun localDateTimeToStringWithLocale(localDateTime: LocalDateTime, format: String, locale: Locale): String {
val formatter = DateTimeFormatter.ofPattern(format, locale) // 포맷 사용자 지정 및 로케일 설정
return localDateTime.format(formatter)
}
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
fun localDateTimeToStringWithZone(localDateTime: LocalDateTime, format: String, zoneId: String): String {
val formatter = DateTimeFormatter.ofPattern(format)
val zonedDateTime = localDateTime.atZone(ZoneId.of(zoneId)) // 커스텀 포맷 및 시간대 설정
return zonedDateTime.format(formatter)
}
5. unix timestamp → LocalDateTime & string
- unix timestamp → LocalDateTime
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneOffset
// 일반적인 예시
fun unixTimestampToLocalDateTime(unixTimestamp: Long): LocalDateTime {
return LocalDateTime.ofInstant(Instant.ofEpochSecond(unixTimestamp), ZoneOffset.UTC)
}
// 한국 시간으로 바꾸는 일반적인 예시
fun unixTimestampToLocalDateTime(unixTimestamp: Long): LocalDateTime {
return LocalDateTime.ofInstant(Instant.ofEpochSecond(unixTimestamp), ZoneId.of("Asia/Seoul"))
}
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
// 다른 타임존 적용
fun unixTimestampToLocalDateTimeWithZone(unixTimestamp: Long, zoneId: String): LocalDateTime {
return LocalDateTime.ofInstant(Instant.ofEpochSecond(unixTimestamp), ZoneId.of(zoneId))
}
import java.time.LocalDateTime
// 직접 계산하기
fun extractUnixTimestampParts(unixTimestamp: Long): LocalDateTime {
val year = unixTimestamp / 31536000L // 1년의 초
val remainingSeconds = unixTimestamp % 31536000L
val month = remainingSeconds / 2592000L // 1달의 초
val day = (remainingSeconds % 2592000L) / 86400L // 1일의 초
val hour = (remainingSeconds % 86400L) / 3600L // 1시간의 초
val minute = (remainingSeconds % 3600L) / 60L // 1분의 초
val second = remainingSeconds % 60L // 초
return LocalDateTime.of(year.toInt(), month.toInt(), day.toInt(), hour.toInt(), minute.toInt(), second.toInt())
}
- unix timestamp → String
import java.time.Instant
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
// 기본 포맷 사용
fun unixTimestampToString(unixTimestamp: Long): String {
val instant = Instant.ofEpochSecond(unixTimestamp)
val formatter = DateTimeFormatter.ISO_DATE_TIME // 또는 원하는 포맷 선택
return formatter.format(instant.atOffset(ZoneOffset.UTC))
}
import java.time.Instant
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
// 사용자 정의 포맷 - UTC
fun unixTimestampToStringWithFormat(unixTimestamp: Long, format: String): String {
val instant = Instant.ofEpochSecond(unixTimestamp)
val formatter = DateTimeFormatter.ofPattern(format)
return formatter.format(instant.atOffset(ZoneOffset.UTC))
}
// 사용자 정의 포맷 "yyyy-MM-dd HH:mm:ss"
fun unixTimestampToKSTStringWithFormat(unixTimestamp: Long, format: String): String {
val dateTime = Instant.ofEpochSecond(unixTimestamp).atZone(ZoneId.of("Asia/Seoul")).toLocalDateTime()
val formatter = DateTimeFormatter.ofPattern(format)
return dateTime.format(formatter)
}
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter
// 시간대 설정
fun unixTimestampToStringWithZone(unixTimestamp: Long, zoneId: String): String {
val instant = Instant.ofEpochSecond(unixTimestamp)
val formatter = DateTimeFormatter.ISO_DATE_TIME
return formatter.format(instant.atZone(ZoneId.of(zoneId)).toOffsetDateTime())
}
6. string → LocalDateTime & unix timestamp
- to LocalDateTime
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
fun stringToLocalDateTime(dateTimeStr: String): LocalDateTime {
val formatter = DateTimeFormatter.ISO_DATE_TIME // 또는 원하는 포맷 선택
return LocalDateTime.parse(dateTimeStr, formatter)
}
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
fun stringToLocalDateTimeWithFormat(dateTimeStr: String, format: String): LocalDateTime {
val formatter = DateTimeFormatter.ofPattern(format) // 사용자 정의 포맷
return LocalDateTime.parse(dateTimeStr, formatter)
}
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.Locale
fun stringToLocalDateTimeWithLocale(dateTimeStr: String, format: String, locale: Locale): LocalDateTime {
val formatter = DateTimeFormatter.ofPattern(format, locale)
return LocalDateTime.parse(dateTimeStr, formatter) // 사용자 정의 포맷 및 로케일 설정
}
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
fun stringToLocalDateTimeWithZone(dateTimeStr: String, zoneId: String): LocalDateTime {
val formatter = DateTimeFormatter.ISO_DATE_TIME
val zonedDateTime = ZonedDateTime.parse(dateTimeStr, formatter.withZone(ZoneId.of(zoneId))) // 시간대 설정
return zonedDateTime.toLocalDateTime()
}
- to Unix Timestamp : UTC 기준
import java.time.LocalDateTime
import java.time.ZoneOffset
fun stringToUnixTimestamp(dateTimeStr: String): Long {
val localDateTime = LocalDateTime.parse(dateTimeStr)
return localDateTime.toEpochSecond(ZoneOffset.UTC)
}
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
fun stringToUnixTimestampWithFormat(dateTimeStr: String, format: String): Long {
val formatter = DateTimeFormatter.ofPattern(format) // 사용자 정의 포맷 사용
val localDateTime = LocalDateTime.parse(dateTimeStr, formatter)
return localDateTime.toEpochSecond(ZoneOffset.UTC)
}
import java.time.ZonedDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
fun stringToUnixTimestampWithZone(dateTimeStr: String, zoneId: String): Long {
val formatter = DateTimeFormatter.ISO_DATE_TIME
val zonedDateTime = ZonedDateTime.parse(dateTimeStr, formatter.withZone(ZoneId.of(zoneId))) // 시간대 설정
return zonedDateTime.toEpochSecond()
}
- to Unix Timestamp : KST 기준
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
fun kstStringToUnixTimestampUsingJavaTime(kstString: String): Long {
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
val dateTime = LocalDateTime.parse(kstString, formatter)
val instant = dateTime.atZone(ZoneId.of("Asia/Seoul")).toInstant()
return instant.epochSecond
}
fun main() {
val kstString = "2023-09-18 15:30:00" // 원하는 KST 문자열 시간
val unixTimestamp = kstStringToUnixTimestampUsingJavaTime(kstString)
println("KST String: $kstString")
println("Unix Timestamp: $unixTimestamp")
}
import java.text.SimpleDateFormat
import java.util.*
fun kstStringToUnixTimestamp(kstString: String): Long {
val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
dateFormat.timeZone = TimeZone.getTimeZone("Asia/Seoul")
try {
val date = dateFormat.parse(kstString)
return date.time / 1000 // 밀리초를 초로 변환
} catch (e: Exception) {
throw IllegalArgumentException("Invalid KST string format: $kstString")
}
}
fun main() {
val kstString = "2023-09-18 15:30:00" // 원하는 KST 문자열 시간
val unixTimestamp = kstStringToUnixTimestamp(kstString)
println("KST String: $kstString")
println("Unix Timestamp: $unixTimestamp")
}