Kotlin fun DateLib > Kotlin

본문 바로가기

사이트 내 전체검색

Kotlin

Kotlin fun DateLib

작성일 20-07-19 09:26

페이지 정보

작성자sbLAB 조회 3,144회 댓글 0건

본문


import android.util.Log
import java.text.DateFormat
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/** 현재시각 문자열 얻기
 * getMyCurrentDateTime()
 * result -> 20200617232323
 * */
fun getMyCurrentDateTime(format : String="yyyyMMddHHmmss"):String{
    return SimpleDateFormat(format, Locale.getDefault()).format(Date())
}

/**
 * 다른포멧의 날짜만들기
 * getNewFormatDate("20201225","yyyyMMdd","yyyy-MM-dd")
 */

fun getNewFormatDate(srcDateStrString,inputFormatString?,outputFormatString?): String? {
    try {
        Log.e("DATE""Input Date Date is $srcDateStr")
        val convertedDate = SimpleDateFormat(outputFormat)
            .format(
                SimpleDateFormat(inputFormat)
                    .parse(srcDateStr)
            )
        Log.e("DATE""Output Date is $convertedDate")

        //Update Date
        return convertedDate
    } catch (eParseException) {
        e.printStackTrace()
    }
    return null
}

/**
 * 2020/12/25 날짜형태 유효성 확인
 * validDate("2020/12/25")
 * true / false
 */
fun validDate(dateStr:String):Boolean{
       val df = SimpleDateFormat("yyyy/MM/dd")
        df.isLenient = false
        try {
            val date:Date = df.parse(dateStr)
            Log.d("DEBUG","Legal Date $date")
            return true
        } catch (eParseException){
            return false
        }
        return false

}

/**
 * 몇개월전후 날짜 얻기
 *  getAddMinusMonth(-6)
 */
fun getAddMinusMonth(diffInt,srcPattern:String="yyyy/MM/dd"):String {
    val cal  = Calendar.getInstance()
    cal.time = Date()
    val df: DateFormat = SimpleDateFormat(srcPattern)
    cal.add(Calendar.MONTH, diff)
    return df.format(cal.time)
}

/**
 * 며칠전후 날짜 얻기
 * getAddMinusDays(-7)
 */
fun getAddMinusDays(diffIntsrcPattern:String="yyyy/MM/dd"):String {
    val cal  = Calendar.getInstance()
    cal.time = Date()
    val df: DateFormat = SimpleDateFormat(srcPattern)
    cal.add(Calendar.DATE, diff)
    return df.format(cal.time)
}



댓글목록

등록된 댓글이 없습니다.

Copyright © 소유하신 도메인. All rights reserved.
PC 버전으로 보기