a
554325746@qq.com
2019-12-25 603cb36a5123e46656b06a5deb8d7ac7ff81307f
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package com.basic.security.utils;
 
import android.text.TextUtils;
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 DateEditUtil1 {
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    static SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
 
    public static String longToDate(String longStr) {
        try {
            if (!TextUtils.isEmpty(longStr)) {
                long validTime = Long.parseLong(longStr);
                return DateEditUtil1.sdf.format(new Date(validTime));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
 
    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) {
            e.printStackTrace();
        }
        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) {
            e.printStackTrace();
        }
        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) {
            e.printStackTrace();
        }
        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) {
            e.printStackTrace();
        }
        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) {
                e.printStackTrace();
            }
            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) {
            e.printStackTrace();
        }
        TimePickerView pvTime1 = new TimePickerBuilder(BaseApplication.getApplication().activity, (date1, v) -> {
            editText.setText(sdf2.format(date1));
//                if (true) {
            if (callBack2 != null) {
                callBack2.notify("", sdf2.format(date1));
            }
            return;
//                }
//                Calendar cal2 = Calendar.getInstance();
//                try {
//                    Object objTime = editText2.getText();
//                    if (objTime != null && !"".equals(objTime + "")) {
//                        String strTime = objTime + "";
//                        cal2.setTime(sdf2.parse(strTime + ""));
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//                TimePickerView pvTime2 = new TimePickerBuilder(BaseApplication.getApplication().activity, new OnTimeSelectListener() {
//
//                    public void onTimeSelect(Date date2, View v) {
//                        // 新加时间限制  开始
//                        try {
//                            long startTime = date1.getTime();
//                            long endTime = date2.getTime();
//                            if (startTime > endTime){
//                                ToastUtil.show("结束时间不能大于开始时间");
//                                return;
//                            }
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                        // 新加时间限制   结束
//                        editText2.updateData(sdf2.format(date2));
//                        if (callBack2 != null) {
//                            callBack2.notify(sdf2.format(date1), sdf2.format(date2));
//                        }
//                    }
//
//                })
//                        .setTitleText("请选择结束时间")
//                        .setDate(cal2)
//                        .setType(new boolean[]{false, false, false, true, true, true})
//                        .build();
//                pvTime2.show();
        })
                .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);
    }
}