그놈의 안드로이드

kotlin_Extension_코드 본문

개인코드

kotlin_Extension_코드

Sandai.Developer 2020. 5. 23. 14:31

1. convertCurrency

>> 숫자를 1000단위로 ,를 찍어 String으로 반환

val Number.convertCurrency: String
    get() {
        val format = DecimalFormat("###,###.##")
        return format.format(this)
    }
val String.convertCurrency: String
    get() {
        val format = DecimalFormat("###,###.##")
        if (this.contains(".")) {
            return format.format(this.toFloat())
        } else {
            return format.format(this.toLong())
        }
    }


2. convertDate

>> Date형 String을 파싱하여 String으로 반환

fun String.convertDate(): Date? {
    val strFormat = when (this.trim().count()) {
        6 -> {
            "yyMMdd"
        }
        8 -> {
            "yyyyMMdd"
        }
        10 -> {
            "yyyy-MM-dd"
        }
        12 -> {
            "yyMMddHHmmss"
        }
        14 -> {
            if (this.contains("  ")) {
                "yyMMdd  HHmmss"
            } else {
                "yyyyMMddHHmmss"
            }
        }
        else -> "yyMMdd"
    }
    return convertDate(strFormat)
}

3. convertDate

>> String형 Date를 받아 Date로 반환

fun String.convertDate(strFormat: String): Date? {
    return try {
        val dateFormat = SimpleDateFormat(strFormat)
        dateFormat.parse(this)
    } catch (e: Exception) {
        null
    }
}

4. convertTel

>> -가 없는 전화번호의 길이를 체크하여 -을 삽입

fun String.convertTel(): String? {
    val strFormat = when (this.trim().count()) {

        //000-0000
        7 -> {
            val strbuf = StringBuffer(this)
            strbuf.insert(3, "-")
            strbuf.toString()
        }

        //0000-0000
        8 -> {
            val strbuf = StringBuffer(this)
            strbuf.insert(4, "-")
            strbuf.toString()
        }

        //00-000-0000
        9 -> {
            val strbuf = StringBuffer(this)
            strbuf.insert(5, "-")
            strbuf.insert(2, "-")
            strbuf.toString()
        }

        //00-0000-0000
        //000-000-0000
        10 -> {
            if (this.substring(0, 2) == "02"){
                val strbuf = StringBuffer(this)
                strbuf.insert(6, "-")
                strbuf.insert(2, "-")
                strbuf.toString()
            }else {
                val strbuf = StringBuffer(this)
                strbuf.insert(6, "-")
                strbuf.insert(3, "-")
                strbuf.toString()
            }
        }

        //000-0000-0000
        11 -> {
            val strbuf = StringBuffer(this)
            strbuf.insert(7, "-")
            strbuf.insert(3, "-")
            strbuf.toString()
        }

        else -> {
            this
        }
    }


    return strFormat
}

5. getDateBasedOnToday

>> 오늘기준 날짜를 계산하여 반환

fun getDateBasedOnToday(year: Int = 0, month: Int = 0, day: Int = 0): Date {
    var cal = GregorianCalendar(Locale.KOREA)
    cal.setTime(Date())
    cal.add(Calendar.YEAR, year) // 1년을 더한다.
    cal.add(Calendar.MONTH, month) // 한달을 더한다.
    cal.add(Calendar.DAY_OF_YEAR, day) // 하루를 더한다.

    var fm = SimpleDateFormat("yyyyMMdd")
    var strDate = fm.format(cal.getTime())
    var date = SimpleDateFormat("yyyyMMdd").parse(strDate)
    return date
}

'개인코드' 카테고리의 다른 글

hexStringToByteArray  (0) 2020.05.23
toHexString  (0) 2020.05.23
Comments