android - Java如何判断一个时间戳是否是昨天?
问题描述
如题,根据时间戳怎么判断是不是昨天呢?
问题解答
回答1:JDK 内置方式
public static boolean isYesterday(long timestamp) { Calendar c = Calendar.getInstance(); clearCalendar(c, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND); c.add(Calendar.DAY_OF_MONTH, -1); long firstOfDay = c.getTimeInMillis(); // 昨天最早时间 c.setTimeInMillis(timestamp); clearCalendar(c, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND); // 指定时间戳当天最早时间 return firstOfDay == c.getTimeInMillis();}private static void clearCalendar(Calendar c, int... fields) { for (int f : fields) {c.set(f, 0); }}回答2:
( ( timestamp - System.currentTimeMillis() ) / (3600 * 24) ) == -1回答3:
public boolean isYesterday(long time) { boolean isYesterday = false; Date date; try {SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd');date = sdf.parse(sdf.format(new Date()));if (time < date.getTime() && time > (date.getTime() - 24*60*60*1000)) { isYesterday = true;} } catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace(); } return isYesterday;}
相关文章:
1. docker gitlab 如何git clone?2. android - 目前有哪些用Vue.js开发移动App的方案?3. docker镜像push报错4. font-family - 我引入CSS3自定义字体没有效果?5. 在windows下安装docker Toolbox 启动Docker Quickstart Terminal 失败!6. docker images显示的镜像过多,狗眼被亮瞎了,怎么办?7. PHP中的$this代表当前的类还是方法?8. nignx - docker内nginx 80端口被占用9. angular.js - 求一款angular插件10. PHP类中的$this