DeteMin
2020-01-15 7584a724d8ff312cae1ae57c6918df10ea72b686
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package com.basic.security.utils;
 
import android.widget.EditText;
 
import com.basic.security.base.BaseApplication;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
public class DateEditUtil {
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    static SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
 
    public static void setClick(final EditText editText, final String dateSepartor) {
        try {
            String defaultValue = sdf.format(new Date()) + dateSepartor + sdf.format(new Date());
            editText.setText(defaultValue);
        } catch (Exception e) {
            ExceptionUtil.printException(e);
        }
        editText.setOnClickListener(view -> showStartDate(editText, dateSepartor));
    }
 
    public static void showStartDate(final EditText editText, final String dateSepartor) {
        Calendar cal = Calendar.getInstance();
        try {
            Object objTime = editText.getText();
            if (objTime != null && !"".equals(objTime + "")) {
                String strTime = objTime + "";
                if (strTime.contains(dateSepartor)) {
                    strTime = strTime.split(dateSepartor, -1)[0];
                }
                cal.setTime(sdf.parse(strTime + ""));
            }
        } catch (Exception e) {
            ExceptionUtil.printException(e);
        }
        TimePickerView pvTime = new TimePickerBuilder(BaseApplication.getApplication().activity, (date, v) -> {
            String selectStartDate = sdf.format(date);
            String oldEditText = editText.getText().toString();
            String result = dateSepartor;
            if (oldEditText.contains(dateSepartor)) {
                result = selectStartDate + dateSepartor + oldEditText.split(dateSepartor, -1)[1];
            }
            editText.setText(result);
            showEndDate(editText, dateSepartor);
        })
                .setTitleText("请选择开始时间")
                .setDate(cal)
                .setType(new boolean[]{true, true, true, true, true, false})
                .build();
        pvTime.show();
    }
 
    public static void showEndDate(final EditText editText, final String dateSepartor) {
        Calendar cal = Calendar.getInstance();
        try {
            Object objTime = editText.getText();
            if (objTime != null && !"".equals(objTime + "")) {
                String strTime = objTime + "";
                if (strTime.contains(dateSepartor)) {
                    strTime = strTime.split(dateSepartor, -1)[1];
                }
                cal.setTime(sdf.parse(strTime + ""));
            }
        } catch (Exception e) {
            ExceptionUtil.printException(e);
        }
        TimePickerView pvTime = new TimePickerBuilder(BaseApplication.getApplication().activity, (date, v) -> {
            String selectEndDate = sdf.format(date);
            String oldEditText = editText.getText().toString();
            String result;
            result = oldEditText.split(dateSepartor, -1)[0] + dateSepartor + selectEndDate;
            editText.setText(result);
        })
                .setTitleText("请选择结束时间")
                .setDate(cal)
                .setType(new boolean[]{true, true, true, true, true, false})
                .build();
        pvTime.show();
    }
 
    public static void showStartAndEndDate(final EditText editText, final EditText editText2) {
        selectTime(editText, editText2, true);
    }
 
    public static void showDate(final EditText editText) {
        selectTime(editText, null, false);
    }
 
    private static void selectTime(final EditText editText, final EditText editText2, boolean twice) {
        Calendar cal1 = Calendar.getInstance();
        try {
            Object objTime = editText.getText();
            if (objTime != null && !"".equals(objTime + "")) {
                String strTime = objTime + "";
                cal1.setTime(sdf.parse(strTime + ""));
            }
        } catch (Exception e) {
            ExceptionUtil.printException(e);
        }
        TimePickerView pvTime1 = new TimePickerBuilder(BaseApplication.getApplication().activity, (date, v) -> {
            editText.setText(sdf.format(date));
            if (!twice) {
                return;
            }
            Calendar cal2 = Calendar.getInstance();
            try {
                Object objTime = editText2.getText();
                if (objTime != null && !"".equals(objTime + "")) {
                    String strTime = objTime + "";
                    cal2.setTime(sdf.parse(strTime + ""));
                }
            } catch (Exception e) {
                ExceptionUtil.printException(e);
            }
            TimePickerView pvTime2 = new TimePickerBuilder(BaseApplication.getApplication().activity, (date1, v1) -> editText2.setText(sdf.format(date1)))
                    .setTitleText("请选择结束时间")
                    .setDate(cal2)
                    .setType(new boolean[]{true, true, true, true, true, false})
                    .build();
            pvTime2.show();
        })
                .setTitleText(editText2 == null ? "请选择结束时间" : "请选择开始时间")
                .setDate(cal1)
                .setType(new boolean[]{true, true, true, true, true, false})
                .build();
        pvTime1.show();
    }
 
    public static void selectTime(long startTime, long endTime, boolean twice, CallBack callBack) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(new Date(startTime));
        TimePickerView pvTime1 = new TimePickerBuilder(BaseApplication.getApplication().activity, (date1, v) -> {
            if (!twice) {
                if (callBack != null) {
                    callBack.notify(date1.getTime(), 0);
                }
                return;
            }
            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(new Date(endTime));
            TimePickerView pvTime2 = new TimePickerBuilder(BaseApplication.getApplication().activity, (date2, v1) -> {
                if (callBack != null) {
                    callBack.notify(date1.getTime(), date2.getTime());
                }
            })
                    .setTitleText("请选择结束时间")
                    .setDate(cal2)
                    .setType(new boolean[]{true, true, true, true, true, false})
                    .build();
            pvTime2.show();
        })
                .setTitleText("请选择开始时间")
                .setDate(cal1)
                .setType(new boolean[]{true, true, true, true, true, false})
                .build();
        pvTime1.show();
    }
 
    public static void showEndOneDate(final EditText editText, final String title, final CallBack2 callBack2) {
        selectOneDayTime(editText, title, callBack2);
    }
 
    private static void selectOneDayTime(final EditText editText, final String title, CallBack2 callBack2) {
        Calendar cal1 = Calendar.getInstance();
        try {
            Object objTime = editText.getText();
            if (objTime != null && !"".equals(objTime + "")) {
                String strTime = objTime + "";
                cal1.setTime(sdf2.parse(strTime + ""));
            }
        } catch (Exception e) {
            ExceptionUtil.printException(e);
        }
        TimePickerView pvTime1 = new TimePickerBuilder(BaseApplication.getApplication().activity, (date1, v) -> {
            editText.setText(sdf2.format(date1));
            if (callBack2 != null) {
                callBack2.notify("", sdf2.format(date1));
            }
            return;
        })
                .setTitleText("请选择" + title + "时间")
                .setDate(cal1)
                .setType(new boolean[]{false, false, false, true, true, true})
                .build();
        pvTime1.show();
    }
 
    public interface CallBack {
        void notify(long start_time, long end_time);
    }
 
    public interface CallBack2 {
        void notify(String start_time, String end_time);
    }
}