공부이야기/[AndroidStudio]
[AndroidStudio] MaterialCalendar Range 두 기간의 날짜 사이 값 받기
Nameless
2022. 4. 19. 00:25
MaterialCalendar Range로 설정
//activitiy_main.xml
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="20dp">
<Button
android:id="@+id/eventDatePickerBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="날짜 선택"
android:onClick="datePickerAction"
android:textAlignment="center"
android:textColor="@color/black"
android:background="@null"
android:textSize="20sp" />
<TextView
android:id="@+id/startDateTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="시 작"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp"/>
<TextView
android:id="@+id/endDateTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="종 료"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="@null"
android:onClick="saveEventAction"
android:text="저장"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
|
cs |
각가의 TextView에 startdate, enddate 표시됨
//MainActivity.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.util.Pair;
import com.google.android.material.datepicker.MaterialDatePicker;
import com.google.android.material.datepicker.MaterialPickerOnPositiveButtonClickListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity
{
private TextView startDateTV,endDateTV;
private Button deleteEventBtn,eventDatePickerBtn;
String startdateString, enddateString;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initWidgets();
}
private void initWidgets()
{
eventDatePickerBtn = findViewById(R.id.eventDatePickerBtn);
startDateTV = findViewById(R.id.startDateTV);
endDateTV= findViewById(R.id.endDateTV);
}
public void saveEventAction(View View) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy년 MM월 dd일");
Date d_from = null;
try {
d_from = simpleDateFormat.parse(startdateString);
} catch (ParseException e) {
e.printStackTrace();
}
Date d_to = null;
try {
d_to = simpleDateFormat.parse(enddateString);
} catch (ParseException e) {
e.printStackTrace();
}
long t1 = d_from.getTime();
long t2 = d_to.getTime();
if(t1<t2){
for(long i=t1; i<=t2; i+=86400000){
Log.i("Date", simpleDateFormat.format(i));
}
}
}
public void datePickerAction(View view)
{
MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();
builder.setTitleText("날짜를 선택해주세요");
final MaterialDatePicker materialDatePicker = builder.build();
materialDatePicker.show(getSupportFragmentManager(), "Date_PICKER");
materialDatePicker.addOnPositiveButtonClickListener(
new MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>()
{
@Override
public void onPositiveButtonClick(Pair<Long, Long> selection)
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy년 MM월 dd일");
Date startdate = new Date();
Date enddate = new Date();
startdate.setTime(selection.first);
enddate.setTime(selection.second);
startdateString = simpleDateFormat.format(startdate);
enddateString = simpleDateFormat.format(enddate);
startDateTV.setText(startdateString);
endDateTV.setText(enddateString);
}
});
}
}
|
cs |
저장 버튼 누르면 19일부터 22일까지 기록된다.