๐ฑ dreaming DiNO
[Kotlin] Date ํ๋ฃจ์ , ํ์ฃผ์ , ํ ๋ฌ ์ , 1์๊ฐ ์ , ๋ ์ง ๊ณ์ฐ ๋ณธ๋ฌธ
[Kotlin] Date ํ๋ฃจ์ , ํ์ฃผ์ , ํ ๋ฌ ์ , 1์๊ฐ ์ , ๋ ์ง ๊ณ์ฐ
MK_____ 2022. 10. 11. 10:20ํ์ฌ ์๊ฐ์ ๋ ์ง ๋ํ๊ณ ๋นผ๊ธฐ
ํ์ฌ ์๊ฐ์ ๊ฐ๊ณ ์๋ Date๊ฐ์ฒด์ ์, ์ผ์ ์ถ๊ฐํ๋ ์ฝ๋์ ๋๋ค. ํ์ฌ Date๋ฅผ Calendar์ ์ค์ ํ๊ณ add()๋ก ๋ ์ง ์ฐ์ฐ์ ํ์์ต๋๋ค. ๋ ์ง๋ฅผ ๋นผ๋ ค๋ฉด ์์๋ฅผ ๋ฃ์ผ๋ฉด ๋ฉ๋๋ค.
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*
fun main(args: Array<String>) {
val cal = Calendar.getInstance()
cal.time = Date()
val df: DateFormat = SimpleDateFormat("yyyy-MM-dd")
println("current: ${df.format(cal.time)}")
cal.add(Calendar.MONTH, 2)
cal.add(Calendar.DATE, -3)
println("after: ${df.format(cal.time)}")
}
๊ฒฐ๊ณผ
current: 2019-03-23
after: 2019-05-20
ํน์ ๋ ์ง์ ๋ํ๊ณ ๋นผ๊ธฐ
์์ ๋์ผํ์ง๋ง ํน์ ๋ ์ง์ Date๋ฅผ ์์ฑํ๋ ๊ฒ์ด ๋ค๋ฆ ๋๋ค. ์ด๋ฒ์๋ ๋ ์ง ๋ฐ ์๊ฐ ์ฐ์ฐ๋ ํ์์ต๋๋ค.
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*
fun main(args: Array<String>) {
val cal = Calendar.getInstance()
val df: DateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
val date:Date = df.parse("2019-07-04T12:30:30+0530")
cal.time = date
println("current: ${df.format(cal.time)}")
cal.add(Calendar.YEAR, 1)
cal.add(Calendar.MONTH, 2)
cal.add(Calendar.DATE, 3)
cal.add(Calendar.HOUR_OF_DAY, 1)
cal.add(Calendar.MINUTE, 20)
cal.add(Calendar.SECOND, 10)
println("after: ${df.format(cal.time)}")
}
๊ฒฐ๊ณผ
current: 2019-07-04T16:00:30+0900
after: 2020-09-07T17:20:40+0900
๋ ์ง ๋๊ฐ ๋ํ๊ธฐ
๋๊ฐ์ Date๋ฅผ ๋ํ๋ ค๋ฉด ๋๊ฐ์ Calendar๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค. Calendar.get์ผ๋ก ๋ ์ง๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค. ์์ ์ฝ๋๋ฅผ ์์ฉํ์ฌ ์๋์ ๊ฐ์ด ๊ตฌํํ๋ฉด ๋ฉ๋๋ค. MONTH๋ 0์ด 1์์ ์๋ฏธํ๊ธฐ ๋๋ฌธ์(zero based) ์ฐ์ฐ์ ํ ๋ 1์ ๋ํด์ค์ผ ํฉ๋๋ค.
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*
fun main(args: Array<String>) {
val cal = Calendar.getInstance()
val df: DateFormat = SimpleDateFormat("yyyy-MM-dd")
cal.time = df.parse("2019-07-04")
val cal2 = Calendar.getInstance()
cal2.time = Date()
println("cal: ${df.format(cal.time)}")
println("cal2: ${df.format(cal2.time)}")
cal.add(Calendar.YEAR, cal2.get(Calendar.YEAR))
cal.add(Calendar.MONTH, cal2.get(Calendar.MONTH) + 1) // Zero-based months
cal.add(Calendar.DATE, cal2.get(Calendar.DATE))
println("after cal: ${df.format(cal.time)}")
println("after cal2: ${df.format(cal2.time)}")
}
๊ฒฐ๊ณผ
cal: 2019-07-04
cal2: 2019-03-23
after cal: 4038-10-27
after cal2: 2019-03-23
@Test
public void catherine(){
//์ค๋
Date today = new Date();
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
String toDay = date.format(today);
System.out.println(toDay);
// 1์๊ฐ์
Calendar cal = Calendar.getInstance();
cal.setTime(today);
cal.add(Calendar.HOUR, -1);
// ํฌ๋งท๋ณ๊ฒฝ ( ๋
์์ผ ์๋ถ์ด)
SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
sdformat.setTimeZone(TimeZone.getTimeZone("UTC"));
String beforeHour = sdformat.format(cal.getTime());
System.out.println("1์๊ฐ ์ : " + beforeHour);
//ํ๋ฃจ ์
Calendar day = Calendar.getInstance();
day.add(Calendar.DATE , -1);
String beforeDate = new java.text.SimpleDateFormat("yyyy-MM-dd").format(day.getTime());
System.out.println(beforeDate);
//ํ์ฃผ ์
Calendar week = Calendar.getInstance();
week.add(Calendar.DATE , -7);
String beforeWeek = new java.text.SimpleDateFormat("yyyy-MM-dd").format(week.getTime());
System.out.println(beforeWeek);
//ํ๋ฌ ์
Calendar mon = Calendar.getInstance();
mon.add(Calendar.MONTH , -1);
String beforeMonth = new java.text.SimpleDateFormat("yyyy-MM-dd").format(mon.getTime());
System.out.println(beforeMonth);
}
์ถ์ฒ: https://codechacha.com/ko/kotlin-examples-add-two-dates/
https://rhr0916.tistory.com/202 [์๋ฆ๋ต๊ฒ ๋์ด๋ค๊ฒ ํ์์:ํฐ์คํ ๋ฆฌ]
'Android' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Kotlin] ์์ ArrayList.removeRange ์ฐ๊ธฐ (0) | 2022.12.06 |
---|---|
[Kotlin] [์๋ฌํด๊ฒฐ] ํจํค์ง๋ช ๋๋ฌธ์ > ์๋ฌธ์ Check your module classpath for missing or conflicting dependencies (0) | 2022.11.10 |
[Kotlin] Asset ํ์ผ ์ฝ์ด์ sdcard ์ ์ฅํ๊ธฐ (1) | 2022.10.05 |
Firebase๋? (0) | 2022.05.31 |
[Android] [์๋ฌํด๊ฒฐ] adb INSTALL_FAILED_TEST_ONLY (0) | 2022.03.31 |