Java获取当前时间,以及当前时间友好显示及MD5加密
2016-12-23 14:41
453 查看
本人第一篇博客,就简单的随记一下时长用的东西
获取系统时间
友好时间显示
MD5加密
获取系统时间
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 ""; } }
相关文章推荐
- 判断手机号的输入是否正确、获取当前的时间以及显示距离上次发布有多久的时间
- 用java获取当前时间以及怎么设置时间的格式
- java中获取当前时间的字符串以及设置时间的格式
- 分享java程序设计中一个小程序:获取当前系统日期和时间在小程序中显示出来
- 友好时间显示 java以及php实现
- java中获取当前时间以及对当前时间操作详解
- java中获取当前系统时间以及过半小时之后的时间的实现
- JAVA,jsp,oracle获取系统当前时间以及日期格式转换!
- java获取本月开始时间和结束时间、上个月第一天和最后一天的时间以及当前日期往前推一周、一个月
- Java 以指定日期时间格式获取当前时间、以及每隔一秒刷新一次的方法------附带实例
- java获取获得Timestamp类型的当前系统时间。以及java.util.date 、java.sql.Date之间的转换
- java获取本月开始时间和结束时间、上个月第一天和最后一天的时间以及当前日期往前推一周、一个月
- java的Calendar类如何获取当前时间的时分秒以及毫秒。
- (转)Java获取当前系统时间
- java 时间 格式化(获取当前时间)
- 表单文本框中文本的获取,还有当前时间的显示
- 如何解决java.util.Date 获取的不是当前时间
- java 获取系统当前时间数字串
- JAVA获取系统当前时间
- java获取取得Timestamp类型的当前系统时间