工具类持续更新

public class BaseCompareUtil {
/**
* 时间类
* getDayOfWeek---------获取一月的第一天是星期几
* getDayOfMonth---------获取一月最大天数
* stampToDateTime----------将时间戳转换为时间
* stringToDateTime---------将String转换为时间戳
* getTodayDate----------获取今日日期yyyy-MM-dd
* getAfterDayDate----------获取几日后日期yyyy-MM-dd
* getBeforeDayDate----------获取几日前日期yyyy-MM-dd
* belongCalendar--------------判断时间是否在时间段内
* caculateTotalTime-----------计算 两个日期之间天数
* getBetweenDates--------------获取两个日期之间的所有日期
* showDatePickers---------------日历点击
*/
//获取一月的第一天是星期几
public static int getDayOfWeek(int y, int m, int day) {
Calendar calendar = Calendar.getInstance();
calendar.set(y, m - 1, day);
return calendar.get(Calendar.DAY_OF_WEEK);
}

//获取一月最大天数
public static int getDayOfMonth(int y, int m) {
Calendar cal = Calendar.getInstance();
cal.set(y, m - 1, 1);
return cal.getActualMaximum(Calendar.DATE);
}

/**
* 将时间戳转换为时间
*/
public static String stampToDateTime(long s) {
if (s == 0) {
return "";
}
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = new Date(s);
res = simpleDateFormat.format(date);
return res;
}

/**
* 将String转换为时间
*/
public static String stringToDateTime(String s) {
if (TextUtils.isEmpty(s)) {
return null;
}
try {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = simpleDateFormat.parse(s);
SimpleDateFormat simpleDateFormatTo = new SimpleDateFormat("yyyy-MM-dd");
res = simpleDateFormatTo.format(date);
return res;
} catch (ParseException e) {
return "";
}
}

/**
* 获取今日日期yyyy-MM-dd
*/
public static String getTodayDate() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
Calendar calendar = Calendar.getInstance();
return simpleDateFormat.format(calendar.getTime());
}

/**
* 获取几日后日期yyyy-MM-dd
*/
public static String getAfterDayDate(int day) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + day);
return simpleDateFormat.format(calendar.getTime());
}

/**
* 获取几日前日期yyyy-MM-dd
*/
public static String getBeforeDayDate(int day) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - day);
return simpleDateFormat.format(calendar.getTime());
}


/**
*      * 判断时间是否在时间段内
*      * 
*      * @param nowTime
*      * @param beginTime
*      * @param endTime
*      * @return 1未进行 2 进行中 3已进行
*     
*/
public static String belongCalendar(Date nowTime, Date beginTime, Date endTime) {
String type;
if (nowTime.getTime() < beginTime.getTime()) {
type = "1";
} else if (nowTime.getTime() > endTime.getTime()) {
type = "3";
} else {
type = "2";
}
return type;
}

/**
* 计算 两个日期之间天数
*
* @param startTime
* @param endTime
* @return
* @throws ParseException
*/
public static int caculateTotalTime(String startTime, String endTime) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = null;
Date date = formatter.parse(startTime);
long ts = date.getTime();
date1 = formatter.parse(endTime);
long ts1 = date1.getTime();
long ts2 = ts1 - ts;
int totalTime = 0;
totalTime = (int) (ts2 / (24 * 3600 * 1000) + 1);
return totalTime;
}

/**
* 获取两个日期之间的所有日期
*/
public static List<Date> getBetweenDates(Date begin, Date end) {
List<Date> result = new ArrayList<Date>();
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(begin);
while (begin.getTime() <= end.getTime()) {
result.add(tempStart.getTime());
tempStart.add(Calendar.DAY_OF_YEAR, 1);
begin = tempStart.getTime();
}
return result;
}

/**
* 日历点击
*
* @param calendar
* @return
*/
public static void showDatePickers(Calendar calendar, Activity activity, EditText changeView) {
DatePickerDialog dialog = new DatePickerDialog(activity, (view, year, month, dayOfMonth) -> {
calendar.set(year, month, dayOfMonth);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
changeView.setText(format.format(calendar.getTime()));
}, calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
//设置日期最小值(只能选择当前时间以后的时间)
// dialog.getDatePicker().setMinDate(Calendar.getInstance().getTimeInMillis());
//设置起始日期和结束日期
DatePicker datePicker = dialog.getDatePicker();
datePicker.setMinDate(System.currentTimeMillis());
dialog.show();
}
/**__________________________________ _________________________________________*/
/***
*功能效果类
*
*isFastClick------------快速点击
*newInstance------------点击折行效果
*dialogAddWordListener---------------无法输入中文
*
*
*
*
*
*/
/**
* 两次点击按钮之间的点击间隔不能少于1000毫秒
*/
private static final int MIN_CLICK_DELAY_TIME = 1000;
private static long lastClickTime = -1;

/**
* 是否为快速点击
*
* @return 快速点击
*/
public static boolean isFastClick() {
boolean flag;
long curClickTime = System.currentTimeMillis();
if (curClickTime - lastClickTime > MIN_CLICK_DELAY_TIME) {
flag = false;
} else {
flag = true;
}
lastClickTime = curClickTime;
return flag;
}

/**
* 点击折行效果
*/
private View hideView, clickView, down;//需要展开隐藏的布局,开关控件

public static BaseCompareUtil newInstance(Context context, View hideView, View clickView, ImageView down) {
clickView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (View.VISIBLE == hideView.getVisibility()) {
hideView.setVisibility(View.GONE);
down.setImageResource(R.drawable.svg_bottom_c126);
} else {
hideView.setVisibility(View.VISIBLE);
down.setImageResource(R.drawable.svg_top_c126);
}
}
});
return new BaseCompareUtil(context, hideView, clickView, down);
}

private BaseCompareUtil(Context context, View hideView, View clickView, View down) {
this.hideView = hideView;
this.down = down;
this.clickView = clickView;
}

/**
* 无法输入中文
*
* @param editText EditText
*/
public static void dialogAddWordListener(EditText editText) {
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {
if (s.length() > 0) {
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= 0x4e00 && c <= 0X9fff) {
// 根据字节码判断
// 如果是中文,则清除输入的字符,否则保留
s.delete(i, i + 1);
}
}
}
}
});
}

}

posted on 2019-08-17 14:23  带镐伤的土豆  阅读(121)  评论(0编辑  收藏  举报

导航