本人第一篇博客,就简单的随记一下时长用的东西
获取系统时间
友好时间显示SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); Date curDate = new Date(System.currentTimeMillis());//获取当前时间 String str = formatter.format(curDate); LogUtil.w("当前系统时间:",str);
//获取单独年月日数字
Calendar now = Calendar.getInstance(); now_years = now.get(Calendar.YEAR); now_months = now.get(Calendar.MONTH); now_days = now.get(Calendar.DAY_OF_MONTH);
//系统拽出的判断时间方法,正确
public static String friendlyTime(String sdate) {
// Date time = toDate(sdate);
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date time = null;
try {
time = sf.parse(sdate);
} catch (ParseException e) {
e.printStackTrace();
}
LogUtil.w("aaaaaaaa+++++++++++++++++++++++++",time.getTime()+"");
//获取time距离当前的秒数
int ct = (int)((System.currentTimeMillis() - time.getTime())/1000);
if(ct == 0) {
return "刚刚";
}
if(ct > 0 && ct < 60) {
return ct + "秒前";
}
if(ct >= 60 && ct < 3600) {
return Math.max(ct / 60,1) + "分钟前";
}
if(ct >= 3600 && ct < 86400)
return ct / 3600 + "小时前";
if(ct >= 86400 && ct < 2592000){ //86400 * 30
int day = ct / 86400 ;
return day + "天前";
}
if(ct >= 2592000 && ct < 31104000) { //86400 * 30
return ct / 2592000 + "月前";
}
return ct / 31104000 + "年前";
}
MD5加密
/**
* MD5加码。32位
*
* @param inStr
* @return
*/
public String MD5(String inStr) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
char[] charArray = inStr.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++)
byteArray[i] = (byte) charArray[i];
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
return "";
}
}