摘要来自:http://blog.csdn.net/syif88/article/details/53517613

显示样例如下:

Android(java)时间转换星期 昨天 今天 几分钟前工具_第1张图片

Android(java)时间转换星期 昨天 今天 几分钟前工具_第2张图片

代码如下:

package utils;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class TimeUtil {static SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");/** * 24小时制转化成12小时制 *  * @param strDay */public static String timeFormatStr(Calendar calendar, String strDay) {String tempStr = "";int hour = calendar.get(Calendar.HOUR_OF_DAY);if (hour > 11) {tempStr = "下午" + " " + strDay;} else {tempStr = "上午" + " " + strDay;}return tempStr;}/** * 时间转化为星期 *  * @param indexOfWeek *            星期的第几天 */public static String getWeekDayStr(int indexOfWeek) {String weekDayStr = "";switch (indexOfWeek) {case 1:weekDayStr = "星期日";break;case 2:weekDayStr = "星期一";break;case 3:weekDayStr = "星期二";break;case 4:weekDayStr = "星期三";break;case 5:weekDayStr = "星期四";break;case 6:weekDayStr = "星期五";break;case 7:weekDayStr = "星期六";break;}return weekDayStr;}/** * 将时间戳格式化,13位的转为10位 *  * @param timestamp * @return */public static long timestampFormate(long timestamp) {String timestampStr = timestamp + "";if (timestampStr.length() == 13) {timestamp = timestamp / 1000;}return timestamp;}/** * 获取日起时间秒差 * * @param time *            需要格式化的时间 如"2014-07-14 19:01:45" * @param pattern *            输入参数time的时间格式 如:"yyyy-MM-dd HH:mm:ss" *            

* 如果为空则默认使用"yyyy-MM-dd HH:mm:ss"格式 * @return time为null,或者时间格式不匹配,输出空字符"" * @throws ParseException */public static long formatLongTime(String time, String pattern) {long dTime = 0;if (time != null) {if (pattern == null)pattern = "yyyy-MM-dd HH:mm:ss";Date tDate;try {tDate = new SimpleDateFormat(pattern).parse(time);Date today = new Date();if (tDate != null) {dTime = (today.getTime() - tDate.getTime()) / 1000;}} catch (ParseException e) {e.printStackTrace();}}return dTime;}/** * 获取日期时间戳 * * @param time * 需要格式化的时间 如"2014-07-14 19:01:45" * @param pattern * 输入参数time的时间格式 如:"yyyy-MM-dd HH:mm:ss" *

* 如果为空则默认使用"yyyy-MM-dd HH:mm:ss"格式 * @return time为null,或者时间格式不匹配,输出空字符"" * @throws ParseException */public static long timeStamp(String time, String pattern) {long dTime = 0;if (time != null) {if (pattern == null)pattern = "yyyy-MM-dd HH:mm:ss";Date tDate;try {tDate = new SimpleDateFormat(pattern).parse(time);if (tDate != null) {dTime = tDate.getTime() / 1000;}} catch (ParseException e) {e.printStackTrace();}}return dTime;}/** * 时间转化为显示字符串 * * @param timeStamp * 单位为秒 */public static String getTimeStr(long timeStamp) {if (timeStamp == 0)return "";Calendar inputTime = Calendar.getInstance();inputTime.setTimeInMillis(timeStamp * 1000);Date currenTimeZone = inputTime.getTime();Calendar calendar = Calendar.getInstance();calendar.set(Calendar.HOUR_OF_DAY, 23);calendar.set(Calendar.MINUTE, 59);calendar.set(Calendar.HOUR_OF_DAY, 0);calendar.set(Calendar.MINUTE, 0);calendar.set(Calendar.SECOND, 0);calendar.set(Calendar.MILLISECOND, 0);if (calendar.before(inputTime)) {SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");return sdf.format(currenTimeZone);}calendar.add(Calendar.DAY_OF_MONTH, -1);if (calendar.before(inputTime)) {return "昨天";} else {calendar.add(Calendar.DAY_OF_MONTH, -5);if (calendar.before(inputTime)) {return getWeekDayStr(inputTime.get(Calendar.DAY_OF_WEEK));} else {calendar.set(Calendar.DAY_OF_MONTH, 1);calendar.set(Calendar.MONTH, Calendar.JANUARY);int year = inputTime.get(Calendar.YEAR);int month = inputTime.get(Calendar.MONTH);int day = inputTime.get(Calendar.DAY_OF_MONTH);return year + "/" + month + "/" + day;}}}/** * 时间转化为聊天界面显示字符串 * * @param timeStamp * 单位为秒 */public static String getChatTimeStr(long timeStamp) {if (timeStamp == 0)return "";Calendar inputTime = Calendar.getInstance();String timeStr = timeStamp + "";if (timeStr.length() == 10) {timeStamp = timeStamp * 1000;}inputTime.setTimeInMillis(timeStamp);Date currenTimeZone = inputTime.getTime();Calendar calendar = Calendar.getInstance();// if (calendar.before(inputTime)){// //当前时间在输入时间之前// SimpleDateFormat sdf = new SimpleDateFormat("yyyy" +// "年"+"MM"+"月"+"dd"+"日");// return sdf.format(currenTimeZone);// }calendar.set(Calendar.HOUR_OF_DAY, 0);calendar.set(Calendar.MINUTE, 0);calendar.set(Calendar.SECOND, 0);calendar.set(Calendar.MILLISECOND, 0);if (calendar.before(inputTime)) {SimpleDateFormat sdf = new SimpleDateFormat("h:mm");return timeFormatStr(inputTime, sdf.format(currenTimeZone));}calendar.add(Calendar.DAY_OF_MONTH, -1);if (calendar.before(inputTime)) {SimpleDateFormat sdf = new SimpleDateFormat("h:mm");return "昨天" + " " + timeFormatStr(inputTime, sdf.format(currenTimeZone));} else {calendar.set(Calendar.DAY_OF_MONTH, 1);calendar.set(Calendar.MONTH, Calendar.JANUARY);if (calendar.before(inputTime)) {SimpleDateFormat sdf = new SimpleDateFormat("M" + "月" + "d" + "日");String temp1 = sdf.format(currenTimeZone);SimpleDateFormat sdf1 = new SimpleDateFormat("h:mm");String temp2 = timeFormatStr(inputTime, sdf1.format(currenTimeZone));return temp1 + temp2;} else {SimpleDateFormat sdf = new SimpleDateFormat("yyyy" + "/" + "M" + "/" + "d" + " ");String temp1 = sdf.format(currenTimeZone);SimpleDateFormat sdf1 = new SimpleDateFormat("h:mm");String temp2 = timeFormatStr(inputTime, sdf1.format(currenTimeZone));return temp1 + temp2;}}}/** * 群发使用的时间转换 */public static String multiSendTimeToStr(long timeStamp) {if (timeStamp == 0)return "";Calendar inputTime = Calendar.getInstance();String timeStr = timeStamp + "";if (timeStr.length() == 10) {timeStamp = timeStamp * 1000;}inputTime.setTimeInMillis(timeStamp);Date currenTimeZone = inputTime.getTime();Calendar calendar = Calendar.getInstance();calendar.set(Calendar.HOUR_OF_DAY, 0);calendar.set(Calendar.MINUTE, 0);calendar.set(Calendar.SECOND, 0);calendar.set(Calendar.MILLISECOND, 0);if (calendar.before(inputTime)) {SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");return sdf.format(currenTimeZone);}calendar.add(Calendar.DAY_OF_MONTH, -1);if (calendar.before(inputTime)) {@SuppressWarnings("unused")SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");return "昨天";} else {calendar.add(Calendar.DAY_OF_MONTH, -5);if (calendar.before(inputTime)) {return getWeekDayStr(inputTime.get(Calendar.DAY_OF_WEEK));} else {calendar.set(Calendar.DAY_OF_MONTH, 1);calendar.set(Calendar.MONTH, Calendar.JANUARY);if (calendar.before(inputTime)) {SimpleDateFormat sdf = new SimpleDateFormat("M" + "/" + "d" + " ");String temp1 = sdf.format(currenTimeZone);SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm");String temp2 = sdf1.format(currenTimeZone);return temp1 + temp2;} else {SimpleDateFormat sdf = new SimpleDateFormat("yyyy" + "/" + "M" + "/" + "d" + " ");String temp1 = sdf.format(currenTimeZone);SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm");String temp2 = sdf1.format(currenTimeZone);return temp1 + temp2;}}}}/** * 格式化时间(输出类似于 刚刚, 4分钟前, 一小时前, 昨天这样的时间) * * @param time * 需要格式化的时间 如"2014-07-14 19:01:45" * @param pattern * 输入参数time的时间格式 如:"yyyy-MM-dd HH:mm:ss" *

* 如果为空则默认使用"yyyy-MM-dd HH:mm:ss"格式 * @return time为null,或者时间格式不匹配,输出空字符"" */public static String formatDisplayTime(String time, String pattern) {String display = "";int tMin = 60 * 1000;int tHour = 60 * tMin;int tDay = 24 * tHour;if (time != null) {if (pattern == null)pattern = "yyyy-MM-dd HH:mm:ss";try {Date tDate = new SimpleDateFormat(pattern).parse(time);Date today = new Date();SimpleDateFormat thisYearDf = new SimpleDateFormat("yyyy");SimpleDateFormat todayDf = new SimpleDateFormat("yyyy-MM-dd");Date thisYear = new Date(thisYearDf.parse(thisYearDf.format(today)).getTime());Date yesterday = new Date(todayDf.parse(todayDf.format(today)).getTime());Date beforeYes = new Date(yesterday.getTime() - tDay);if (tDate != null) {@SuppressWarnings("unused")SimpleDateFormat halfDf = new SimpleDateFormat("MM月dd日");long dTime = today.getTime() - tDate.getTime();if (tDate.before(thisYear)) {display = new SimpleDateFormat("yyyy年MM月dd日").format(tDate);} else {if (dTime < tMin) {display = "刚刚";} else if (dTime < tHour) {display = (int) Math.ceil(dTime / tMin) + "分钟前";} else if (dTime < tDay && tDate.after(yesterday)) {display = (int) Math.ceil(dTime / tHour) + "小时前";} else if (tDate.after(beforeYes) && tDate.before(yesterday)) {display = "昨天 " + new SimpleDateFormat("HH:mm").format(tDate);} else {display = multiSendTimeToStr(tDate.getTime() / 1000);}}}} catch (Exception e) {e.printStackTrace();}}return display;}public static void main(String[] args) {//TODO 当前时间System.out.println(sf.format(new Date()));// TODO 字传入时间日期字符串转换群发使用时间输出System.out.println(formatDisplayTime("2017-06-30 10:34:00", null));// TODO 先 转换成时间戳 转换成对应的时间输出格式long timeStamp = timeStamp("2017-06-29 10:34:00", null);System.out.println(multiSendTimeToStr(timeStamp));//群发使用时间System.out.println(getChatTimeStr(timeStamp));//时间转化为聊天界面显示字符串System.out.println(getTimeStr(timeStamp));//时间转化为显示字符串}}


更多相关文章

  1. Android 隔一段时间重复执行某代码
  2. Android 指定(后缀名)格式文件扫描
  3. android毫秒转化为时间
  4. 为什么我写的Android闹钟到时间时却不会响
  5. android判断指定时间是否是当前时间
  6. Android 各种时间格式转化和获取时间戳
  7. Android studio 解析JSON格式数据
  8. Root权限下修改 Android 系统时间

随机推荐

  1. Android画圆角矩形图片,并在图片上写字
  2. okhttp的应用详解与源码解析--http的发展
  3. Android(安卓)Studio 1.0 (稳定版) 完全
  4. Android颜色选择器
  5. 安卓按比例布局,layout_weight用法
  6. android的camera
  7. Android(安卓)Eclipse JNI 调用 .so文件
  8. android全屏去掉title栏的多种实现方法
  9. 关于android的中文文档
  10. Android中读写文件