工具类持续更新

/**
* 开发常用工具类
*/
public class BaseCompareUtil {
/**
* 时间类
* getDayOfWeek---------获取一月的第一天是星期几
* getDayOfMonth---------获取一月最大天数
* stampToDateTime----------将时间戳转换为时间
* stringToDateTime---------将String转换为时间戳
* getTodayDate----------获取今日日期yyyy-MM-dd
* getAfterDayDate----------获取几日后日期yyyy-MM-dd
* getBeforeDayDate----------获取几日前日期yyyy-MM-dd
* belongCalendar--------------判断时间是否在时间段内
* caculateTotalTime-----------计算 两个日期之间天数
*/
//获取一月的第一天是星期几
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;
}
/***
*功能效果类
*
*isFastClick------------快速点击
*newInstance------------点击折行效果
*
*
*
*
*
*
*/
/**
* 两次点击按钮之间的点击间隔不能少于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;
}
}

猜你喜欢

转载自www.cnblogs.com/sunjian43792901/p/11368502.html
今日推荐