600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 仿微信朋友圈日期显示 时间戳格式化

仿微信朋友圈日期显示 时间戳格式化

时间:2019-11-10 02:12:16

相关推荐

仿微信朋友圈日期显示 时间戳格式化

...调用

long time=convert2long(“-03-0116:32:34”);textview.setText(getFormatTime(time));

/*** 时间戳格式转换*/public static String getFormatTime(long timesamp) {String result = "";Calendar todayCalendar = Calendar.getInstance();Calendar otherCalendar = Calendar.getInstance();otherCalendar.setTimeInMillis(timesamp);SimpleDateFormat timeFormat = new SimpleDateFormat("M月d日 HH:mm");SimpleDateFormat yearTimeFormat = new SimpleDateFormat("yyyy年M月d日 HH:mm");SimpleDateFormat hourAndMinFormat = new SimpleDateFormat("HH:mm");boolean yearTemp = todayCalendar.get(Calendar.YEAR)==otherCalendar.get(Calendar.YEAR);if(yearTemp){int todayMonth=todayCalendar.get(Calendar.MONTH);int otherMonth=otherCalendar.get(Calendar.MONTH);if(todayMonth==otherMonth){//表示是同一个月int temp=todayCalendar.get(Calendar.DATE)-otherCalendar.get(Calendar.DATE);switch (temp) {case 0:result = hourAndMinFormat.format(new Date(timesamp));break;case 1:result = "昨天 " + hourAndMinFormat.format(new Date(timesamp));break;default:result = timeFormat.format(new Date(timesamp));break;}}else{result = timeFormat.format(new Date(timesamp));}}else{result=yearTimeFormat.format(new Date(timesamp));}return result;}/*** 将日期格式的字符串转换为长整型** @param date* @return*/public static long convert2long(String date) {try {SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");return sf.parse(date).getTime();} catch (ParseException e) {e.printStackTrace();}return 0l;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。