Kotlin fun DateLib
페이지 정보
작성자 sbLAB 댓글 0건 조회 2,671회 작성일 20-07-19 09:26본문
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(srcDateStr: String,inputFormat: String?,outputFormat: String?): 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 (e: ParseException) {
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 (e: ParseException){
return false
}
return false
}
/**
* 몇개월전후 날짜 얻기
* getAddMinusMonth(-6)
*/
fun getAddMinusMonth(diff: Int,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(diff: Int, srcPattern: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)
}
댓글목록
등록된 댓글이 없습니다.