suchen

Heim  >  Fragen und Antworten  >  Hauptteil

android - Java如何判断一个时间戳是否是昨天?

如题,根据时间戳怎么判断是不是昨天呢?

迷茫迷茫2812 Tage vor873

Antworte allen(3)Ich werde antworten

  • PHP中文网

    PHP中文网2017-04-18 10:54:36

    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);
        }
    }

    Antwort
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:54:36

        ( ( timestamp - System.currentTimeMillis() ) / (3600 * 24) ) == -1

    Antwort
    0
  • 高洛峰

    高洛峰2017-04-18 10:54:36

    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 block
            e.printStackTrace();
        }
        return isYesterday;
    }

    Antwort
    0
  • StornierenAntwort