Notice
Recent Posts
Recent Comments
Link
ยซ   2024/07   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
more
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐ŸŒฑ dreaming DiNO

[Kotlin] Date ํ•˜๋ฃจ์ „, ํ•œ์ฃผ์ „, ํ•œ ๋‹ฌ ์ „, 1์‹œ๊ฐ„ ์ „, ๋‚ ์งœ ๊ณ„์‚ฐ ๋ณธ๋ฌธ

Android

[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 [์•„๋ฆ„๋‹ต๊ฒŒ ๋‚˜์ด๋“ค๊ฒŒ ํ•˜์†Œ์„œ:ํ‹ฐ์Šคํ† ๋ฆฌ]