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

摘自: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);
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容