时间转换工具类
来源:互联网 发布:linux怎么编译内核 编辑:程序博客网 时间:2024/04/29 02:37
public class TimeUtil { /** * 把一个时间戳转为描述性的文字 * 如果时间戳代表的时间是今天14:35 返回“14:35” * 如果时间戳代表的时间是昨天14:35 返回“昨天 14:35” * * 如果时间戳代表的时间是更往前的14:35 返回“xxxx-xx-xx 14:35” * * @param time 秒值 * @return */ public static String getTime(long time) { String result = ""; Calendar calendar = Calendar.getInstance(); int week = calendar.get(Calendar.DAY_OF_WEEK) - 1; //根据time与当前时间的差距计算差了多少天 long now = System.currentTimeMillis();//毫秒值 //计算差了多少天 int day = (int) (now / 1000 / 3600 / 24 - time / 1000 / 3600 / 24); if (week!=0){ int dayCha = week - day; if (day == 0) { result = new SimpleDateFormat("HH:mm").format(time); return result; } if (day == 1) { result = "昨天 " + new SimpleDateFormat("HH:mm").format(time); return result; } if (day >=2) { if (dayCha >= 1 && dayCha <= 6) { String weekTime = getWeekTime(time); result = weekTime; return result; }else { result = new SimpleDateFormat("yyyy-MM-dd").format(time); return result; } } }else { //周日 week=7; int dayCha = week - day; if (day == 0) { result = new SimpleDateFormat("HH:mm").format(time); return result; } if (day == 1) { result = "昨天 " + new SimpleDateFormat("HH:mm").format(time); return result; } if (day >=2) { if (dayCha >= 1 && dayCha <= 6) { String weekTime = getWeekTime(time); result = weekTime; return result; }else { result = new SimpleDateFormat("yyyy-MM-dd").format(time); return result; } } } return result; } private static String getWeekTime(long date) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); String time = df.format(date); String Week = ""; Calendar c = Calendar.getInstance(); try { c.setTime(df.parse(time)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } int i = c.get(Calendar.DAY_OF_WEEK); if (i== 1) { Week += "星期天"; } if (i ==2) { Week += "星期一"; } if (i ==3) { Week += "星期二"; } if (i == 4) { Week += "星期三"; } if (i == 5) { Week += "星期四"; } if (i == 6) { Week += "星期五"; } if (i == 7) { Week += "星期六"; } return Week; } public static String getMyTime(long time){ Calendar calendar=Calendar.getInstance(); String result = ""; int week =calendar.get(Calendar.DAY_OF_WEEK); //根据time与当前时间的差距计算差了多少天 long now = System.currentTimeMillis();//毫秒值 //计算差了多少天 int day = (int)(now/1000/3600/24-time/1000/3600/24); switch (day) { case 0: result = new SimpleDateFormat("HH:mm").format(time); break; case 1: result = "昨天 "+new SimpleDateFormat("HH:mm").format(time); break; case 2: case 3: case 4: case 5: case 6: //result = "前天 "+new SimpleDateFormat("HH:mm").format(time*1000); String weekTime = getWeekTime(time); result=weekTime; //显示周几 break; default: result = new SimpleDateFormat("yyyy-MM-dd").format(time); break; } return result; } public static String getTimestr(Date date){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); String time = format.format(date); return time; } public static String getTimeStrWithSec(Date date){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = format.format(date); return time; } public static Date stringToDate(String dateString) { ParsePosition position = new ParsePosition(0); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date dateValue = simpleDateFormat.parse(dateString, position); return dateValue; } public static long stringToLong(String dateString) { ParsePosition position = new ParsePosition(0); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); Date dateValue = simpleDateFormat.parse(dateString, position); return dateValue.getTime(); } public static long getDateTime(String sTime){ SimpleDateFormat format= new SimpleDateFormat("yyyy.MM"); String time=sTime; Date date= null; try { date = format.parse(time); } catch (ParseException e) { e.printStackTrace(); } if (date!=null){ return date.getTime(); } System.out.print("Format To times:"+date.getTime()); return date.getTime(); } public static long getDateTimeForXx(String sTime){ SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time=sTime; Date date= null; try { date = format.parse(time); } catch (ParseException e) { e.printStackTrace(); } if (date!=null){ return date.getTime(); } System.out.print("Format To times:"+date.getTime()); return date.getTime(); }}
对于时间的处理,在社交类的app需求最多。
阅读全文
0 0
- 时间转换工具类
- 时间转换工具类
- 时间转换工具类
- Android 时间转换工具类
- Android工具类(时间转换)
- 时间转换处理工具类
- android 时间转换工具类
- DateUtil时间转换工具类
- 3.时间转换工具类
- Java获取时间 时间计算 转换时间工具类
- 通用工具-Time时间转换类
- DateUtil工具类时间戳类型转换
- java日期时间转换工具类
- Android 日期时间等转换工具类
- 新浪微博时间转换工具类
- Android开发 时间戳转换工具类
- 工具类--TimeUtil 时间格式化与转换
- Date&String时间转换工具类
- 【算法题】求有序数列中相邻数字之间的最大差值
- 卷积的可视化学习
- 异常类处理1
- 新建和删除视图
- jq获取iframe中的内容
- 时间转换工具类
- 使用springboot+dubbo+zookeeper构建搭建生产者平台
- 《深入理解Linux内核》-3.1. 进程,轻量级进程,和线程
- linux学习第二十五篇:cut,sort,wc,uniq,tee,tr,split命令,shell特殊符号
- Unity 开发常见问题整理 (一)yield return 不执行的原因
- C++内存池讲解
- 分页工具类JdbcUtil
- 数组调用mutableCopy并不能把数组内的对象元素再生成一份新的,所以并不是真正的深拷贝
- 【转自知乎】:localhost、127.0.0.1 和 本机IP 三者的区别?