Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- 안드로이드
- 다이얼로그 프래그먼트
- 데이터바인딩
- 레트로핏 MVVM
- Retrofit with MVVM
- dialog fragment
- 레트로핏2
- NestedScrollView
- dialogfragment singleton
- recyclerview
- 레트로핏 코틀린
- java
- DataBinding
- 위치정보확인
- ScrollView with ConstraintLayout
- dialog resize
- ScrollView Child View Height Programmatically
- 인텐트란?
- 리사이클러뷰
- 뷰바인딩
- viewBinding
- 스크롤뷰 자식 뷰 높이 동적조절
- Retrofit Kotlin
- lifecycleScope
- Android
- 다이얼로그 크기조절
- 쉐어드
- 프로그래머스
- programmers
- location System
Archives
- Today
- Total
안드로이드 세계
[Android] SimpleDateFormat에 대하여 본문
SimpleDateFormat은 Date -> String 형태로 변환할 때 사용되어진다.
예시로 현재시간을 구할 때 다음과 같이 사용되어진다.
fun getCurrentTime(): String{
val formatter = SimpleDateFormat("yyyyMMdd HH:mm:ss", Locale.getDefault())
return formatter.format(Calendar.getInstance().time)
}
여기에서 주목할점은 format형태로 주어지는 "yyyyMMdd HH:mm:ss" 인데, 해당내용은 다음과같다.
y | year |
M | Month in year |
d | Day in month |
H | Hour in day (0-23) |
m | Minute in hour |
s | Second in minute |
이 외에도 다른 형태가 있는데, 해당내용은 아래의 링크를 참조해보면 된다.
docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
SimpleDateFormat (Java Platform SE 7 )
Parses text from a string to produce a Date. The method attempts to parse text starting at the index given by pos. If parsing succeeds, then the index of pos is updated to the index after the last character used (parsing does not necessarily use all charac
docs.oracle.com
좀 더 편하게 사용하기위해서 다음과 같이 사용한다.
Util.kt
fun Date.dateToString(format: String, local : Locale = Locale.getDefault()): String{
val formatter = SimpleDateFormat(format, local)
return formatter.format(this)
}
fun currentDate(): Date{
return Calendar.getInstance().time
}
Activity나 Fragment에서 사용할 부분에서
val currentDate = currentDate()
currentDate.dateToString("yyyyMMdd")
또는
currentDate().dateToString("yyyyMMdd")
'안드로이드(Android) > 코틀린(Kotlin)' 카테고리의 다른 글
[Android] ScrollView의 Child View Height 문제 (0) | 2021.05.01 |
---|---|
[Android] Retrofit2 사용법 (0) | 2021.05.01 |
[Android] E/libc: Access denied finding property "ro.serialno" 에러시 대처법 (0) | 2021.04.20 |
[Android] 버튼 백그라운드 변경문제 (0) | 2021.02.19 |
[Android] RecyclerView 스크롤시 설정 해제되는 현상(체크박스, 백그라운드색상 등)해결법 (0) | 2021.02.18 |
Comments