๊ด€๋ฆฌ ๋ฉ”๋‰ด

ruriruriya

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - Calendar๋กœ ์‹ค์‹œ๊ฐ„ ์‹œ๊ฐ„ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ (feat. SimpleDateFormat) ๋ณธ๋ฌธ

๐Ÿค–Android/Java

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - Calendar๋กœ ์‹ค์‹œ๊ฐ„ ์‹œ๊ฐ„ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ (feat. SimpleDateFormat)

๋ฃจ๋ฆฌ์•ผใ…‘ 2024. 1. 16. 09:06
๋ฐ˜์‘ํ˜•

์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ ๊ฐœ๋ฐœ ์‹œ ์‹ค์‹œ๊ฐ„ ์‹œ๊ฐ„ ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐฉ๋ฒ•๊ณผ
๋‚ด๊ฐ€ ๋‚˜ํƒ€๋‚ด๊ณ  ์‹ถ์€ ํ˜•์‹์œผ๋กœ ํ™”๋ฉด์— ๋ณด์—ฌ์ฃผ๋„๋ก ํ•ด๋ณด์ž.

 

1. ํ™”๋ฉด์„ค๊ณ„

ํ™”๋ฉด ์„ค๊ณ„๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ TextView ํ•œ๊ฐœ๋งŒ ๋ฐฐ์น˜ํ•˜๊ณ  id ์ ์šฉ์„ ํ–ˆ๋‹ค.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/txtTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="์‹ค์‹œ๊ฐ„ ์‹œ๊ฐ„"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

2. ์‹ค์‹œ๊ฐ„ ์‹œ๊ฐ„ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ

Calendar ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ๋’ค
getInstance() ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•ด์„œ ํ˜„์žฌ ์‹œ๊ฐ„ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

Calendar calendar= Calendar.getInstance();

 

3. ์‹œ๊ฐ„ ์ •๋ณด ๋ฐ์ดํ„ฐ ํฌ๋งท ์„ค์ •

SimpleDateFormat์€ Java์—์„œ ๋‚ ์งœ๋ฅผ ํ˜•์‹ํ™”ํ•˜๊ณ  ํŒŒ์‹ฑํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค๋กœ
๋‹ค์–‘ํ•œ ๋‚ ์งœ ํฌ๋งท์„ ์ง€์›ํ•œ๋‹ค.

- ๋…„-์›”-์ผ(yyy-MM-dd)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

- ์›”/์ผ/๋…„ (MM/dd/yyyy)

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

- ์ „์ฒด ๋‚ ์งœ ๋ฐ ์‹œ๊ฐ„ (yyyy-MM-dd HH:mm:ss)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

- ์‹œ:๋ถ„ AM/PM (hh:mm a)

SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");

- ์š”์ผ (EEEE)

SimpleDateFormat sdf = new SimpleDateFormat("EEEE");

- ์‹œ๊ฐ„๋Œ€ (z)

SimpleDateFormat sdf = new SimpleDateFormat("z");

ํŠน์ • ํŒจํ„ด (์‚ฌ์šฉ์ž ์ •์˜)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");

 

4. ์ตœ์ข… ์ฝ”๋“œ ๋ฐ ํ™”๋ฉด

๋ณธ์ธ์€ ๋‘์ž๋ฆฌ ์ˆ˜ ์‹œ๊ฐ„๊ณผ ๋ถ„์„ ๋‚˜ํƒ€๋‚ด๋Š” ํฌ๋งท์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

SimpleDateFormat format =new SimpleDateFormat("HH:mm");
String time = format.format(calendar.getTime());

 

- ์•ˆ๋“œ๋กœ์ด๋“œ ํ™”๋ฉด

 

- ์ตœ์ข… ์ฝ”๋“œ

[activity_main.xml]

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/txtTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="์‹ค์‹œ๊ฐ„ ์‹œ๊ฐ„"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

[MainActivity.java]

package com.example.calendar;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    TextView txtTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtTime = findViewById(R.id.txtTime);

        Calendar calendar= Calendar.getInstance(); //ํ˜„์žฌ ์‹œ๊ฐ„์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ์ฒด

        SimpleDateFormat format =new SimpleDateFormat("HH:mm");
        String time = format.format(calendar.getTime());

        txtTime.setText(time);

    }
}
๋ฐ˜์‘ํ˜•