美文网首页
根据今天日期,查询今天开始和结束的时间戳

根据今天日期,查询今天开始和结束的时间戳

作者: 不na讷 | 来源:发表于2022-07-18 16:31 被阅读0次

摘自:https://qa.1r1g.com/sf/ask/1975579651/

/**
 * @param date the date in the format "yyyy-MM-dd"
 */
public long getStartOfDayInMillis(String date) throws ParseException {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Calendar calendar = Calendar.getInstance();
        calendar.setTime(format.parse(date));
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    return calendar.getTimeInMillis();
}

/**
 * @param date the date in the format "yyyy-MM-dd"
 */
public long getEndOfDayInMillis(String date) throws ParseException {
    // Add one day's time to the beginning of the day.
    // 24 hours * 60 minutes * 60 seconds * 1000 milliseconds = 1 day
    return getStartOfDayInMillis(date) + (24 * 60 * 60 * 1000);
}

相关文章

网友评论

      本文标题:根据今天日期,查询今天开始和结束的时间戳

      本文链接:https://www.haomeiwen.com/subject/udkvbrtx.html