์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- ์๋ฐ์คํฌ๋ฆฝํธ ๊ณต๋ถ
- db
- Pandas
- ํ์ด์ฌ
- ํ๋ค์ค
- ์๋ฐ
- java
- ์๋ฐ์ด๋ณด
- ์๋ฐํ๋ก๊ทธ๋๋ฐ
- serverless
- streamlit
- ์๋๋ก์ด๋ ์ฑ ๊ฐ๋ฐ
- Matplotlib ๊ธฐ์ด
- ํ๋ค์ค๊ณต๋ถ
- MySQL
- ์๋ฐ์คํฌ๋ฆฝํธ
- ์๋ฐ๊ธฐ์ด
- JavaScript
- ์๋ฐ๊ณต๋ถ
- RESTful API
- ์น๋์๋ณด๋ ๊ธฐ์ด
- Android
- ํ์ด์ฌ๊ณต๋ถ
- ์คํธ๋ฆผ๋ฆฟ
- ์คํธ๋ฆผ๋ฆฟ ๊ธฐ๋ณธ
- Streamlit๊ธฐ๋ณธ
- ์น๋์๋ณด๋ ์ ์
- ์๋๋ก์ด๋ ์คํ๋์ค
- Streamlit๊ธฐ์ด
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- Today
- Total
ruriruriya
[Android] ์๋๋ก์ด๋ - Calendar๋ก ์ค์๊ฐ ์๊ฐ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ (feat. SimpleDateFormat) ๋ณธ๋ฌธ
[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);
}
}