一.项目需求里,效果如图
先取到时间戳,例:2486401
然后代码如下
long day = millisUntilFinished / 86400; //86400 = 24 * 60 * 60
long hour = (millisUntilFinished - day * 86400) / 60 / 60;
long min = (millisUntilFinished - day * 86400 - hour * 3600) / 60;
long sec = millisUntilFinished - day * 86400 - hour * 3600 - min * 60;
holder.timeTextView.setText(Html.fromHtml("距离活动开始还有: " + "<big>" + day + "</big>" + "日 " + "<big>" + hour
+ "</big>" + "时 " + "<big>" + min + "</big>" + "分 " + "<big>" + sec + "</big>" + "秒"));
另外说一句,这里的android 的Html.fromHtml的font标签只实现了color和face两个属性,所以<font size=3>文字</font>并不能改变文字的大小,可以考虑用big和small
二.获取几天之后的间隔天数 此代码拷贝自CNnumen 的博客
/**
* 获取两个日期之间的间隔天数
* @return
*/
public static int getGapCount(Date startDate, Date endDate) {
Calendar fromCalendar = Calendar.getInstance();
fromCalendar.setTime(startDate);
fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
fromCalendar.set(Calendar.MINUTE, 0);
fromCalendar.set(Calendar.SECOND, 0);
fromCalendar.set(Calendar.MILLISECOND, 0);
Calendar toCalendar = Calendar.getInstance();
toCalendar.setTime(endDate);
toCalendar.set(Calendar.HOUR_OF_DAY, 0);
toCalendar.set(Calendar.MINUTE, 0);
toCalendar.set(Calendar.SECOND, 0);
toCalendar.set(Calendar.MILLISECOND, 0);
return (int) ((toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24));
}