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);
|
}
|
}
|