package com.doumee.lib_coremodel.util; import android.app.Activity; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; /** * Created by MSI on 2018/4/18. */ public class TimeUtils { public static String stringToString(String oldTime){ long old=0; try { old= Long.parseLong(oldTime); }catch (NumberFormatException e){ return ""; } long time= (System.currentTimeMillis() - old)/1000; long sec = time/60; if (sec<60) { if(sec<=0){ return "刚刚"; }else{ return String.format("%d分钟前",sec); } } // 秒转小时 long hours = time/3600; if (hours<24) { return String.format("%d小时前",hours); } //秒转天数 long days = time/3600/24; if (days < 30) { return String.format("%d天前",days); } //秒转月 long months = time/3600/24/30; if (months < 12) { return String.format("%d月前",months); } //秒转年 long years = time/3600/24/30/12; return String.format("%d年前",years); } public static String longToString(long oldTime){ long time= (System.currentTimeMillis() - oldTime)/1000; long sec = time/60; if (sec<60) { if(sec<=0){ return "刚刚"; }else{ return String.format("%d分钟前",sec); } } // 秒转小时 long hours = time/3600; if (hours<24) { return String.format("%d小时前",hours); } //秒转天数 long days = time/3600/24; if (days < 30) { return String.format("%d天前",days); } //秒转月 long months = time/3600/24/30; if (months < 12) { return String.format("%d月前",months); } //秒转年 long years = time/3600/24/30/12; return String.format("%d年前",years); } public static String getDayAndWeek(Calendar calendar){ String[] weekList = new String[]{"", "周日", "周一", "周二", "周三", "周四", "周五", "周六"}; String weekStr = weekList[calendar.get(Calendar.DAY_OF_WEEK)]; DateFormat df = new SimpleDateFormat("yyyy年"); return df.format(calendar.getTime())+calendar.get(Calendar.MONTH)+"月"+calendar.get(Calendar.DAY_OF_MONTH)+"日 "+weekStr; } public static String getTime(Calendar calendar, Activity activity){ boolean is24HourFormat = android.text.format.DateFormat.is24HourFormat(activity); //判断时间制 String ampm = ""; int hour = calendar.get(Calendar.HOUR_OF_DAY); if(is24HourFormat) { //24小时制 if (hour < 12) { ampm = "上午"; } else { ampm = "下午"; } }else { //12小时制 if(calendar.get(Calendar.AM_PM)==0){ ampm = "上午"; }else { ampm = "下午"; } } if(hour>12){ hour-=12; } return ampm+hour+":"+new SimpleDateFormat("mm").format(calendar.getTime()); } }