-
[Kotlin] 코틀린 두 날짜간 차이 구하기, 디데이(D-Day) 구하기Android/Kotlin 2021. 2. 6. 16:54
코틀린에서 두 날짜간 차이, 시작일자로부터 경과일 수, D-Day를 구하는 방법 code snippets
fun main() { val dateFormat = SimpleDateFormat("yyyyMMdd") val startDate = dateFormat.parse("20200425").time val endDate = dateFormat.parse("20250425").time val today = Calendar.getInstance().apply { set(Calendar.HOUR_OF_DAY, 0) set(Calendar.MINUTE, 0) set(Calendar.SECOND, 0) set(Calendar.MILLISECOND, 0) }.time.time println("두 날짜간의 차이(일) : ${(endDate - startDate) / (24 * 60 * 60 * 1000)}") println("시작일 부터 경과 일 : ${(today - startDate) / (24 * 60 * 60 * 1000)}") println("목표일 까지 남은 일(D-DAY) : ${(endDate - today) / (24 * 60 * 60 * 1000)}") }
> 실행결과
두 날짜간의 차이(일) : 1826 시작일 부터 경과 일 : 287 목표일 까지 남은 일(D-DAY) : 1539 Process finished with exit code 0
- 초/분/시 단위를 제거하고 일자 기준으로 계산하였다
-> 시간 까지 고려하려면 SimpleDateFormat("yyyy-MM-dd HH:mm:ss") 으로 생성하고, today 를 가져올때 set 0하는부분을 지워준다'Android > Kotlin' 카테고리의 다른 글
[Kotlin] 코틀린 로또번호 생성 추출 (0) 2022.01.23 [Kotlin] 코틀린 숫자로 입력된 금액 한글로 변환하기 (1) 2021.01.01 [Kotlin] 코틀린 Null Safety, 안전한 null 처리 방법 (1) 2020.12.27 [kotlin] 코틀린 let, run, with, apply, also 함수 비교 및 상황별 함수 선택 (0) 2020.12.26