加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java获取当天、当前月、当前年(今年)的开始和结束时间戳

发布时间:2020-12-15 05:28:32 所属栏目:Java 来源:网络整理
导读:最近在做统计相关的功能的时候涉及到了获取当天的开始和结束的时间戳、当月和当年的开始结束时间戳,特此记录,以作备忘。 相关代码 package com.lingyejun.authenticator;import java.time.Instant;import java.time.LocalDateTime;import java.time.ZoneId

最近在做统计相关的功能的时候涉及到了获取当天的开始和结束的时间戳、当月和当年的开始结束时间戳,特此记录,以作备忘。

相关代码

package com.lingyejun.authenticator;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.TimeZone;

public class CalendarAdjust {

    /**
     * 获取指定某一天的开始时间戳
     *
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  如 GMT+8:00
     * @return
     */
    public static Long getDailyStartTime(Long timeStamp,String timeZone) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(timeStamp);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.MILLISECOND,0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取指定某一天的结束时间戳
     *
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  如 GMT+8:00
     * @return
     */
    public static Long getDailyEndTime(Long timeStamp,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);
        calendar.set(Calendar.MILLISECOND,999);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取当月开始时间戳
     *
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  如 GMT+8:00
     * @return
     */
    public static Long getMonthStartTime(Long timeStamp,String timeZone) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(timeStamp);
        calendar.add(Calendar.YEAR,0);
        calendar.add(Calendar.MONTH,0);
        calendar.set(Calendar.DAY_OF_MONTH,1);// 设置为1号,当前日期既为本月第一天
        calendar.set(Calendar.HOUR_OF_DAY,0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取当月的结束时间戳
     *
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  如 GMT+8:00
     * @return
     */
    public static Long getMonthEndTime(Long timeStamp,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));// 获取当前月最后一天
        calendar.set(Calendar.HOUR_OF_DAY,999);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取当年的开始时间戳
     *
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  如 GMT+8:00
     * @return
     */
    public static Long getYearStartTime(Long timeStamp,0);
        calendar.add(Calendar.DATE,0);
        calendar.set(Calendar.DAY_OF_YEAR,1);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取当年的最后时间戳
     *
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  如 GMT+8:00
     * @return
     */
    public static Long getYearEndTime(Long timeStamp,String timeZone) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(timeStamp);
        int year = calendar.get(Calendar.YEAR);
        calendar.clear();
        calendar.set(Calendar.YEAR,year);
        calendar.set(Calendar.HOUR_OF_DAY,999);
        calendar.roll(Calendar.DAY_OF_YEAR,-1);
        return calendar.getTimeInMillis();
    }

    /**
     * 时间戳转字符串
     *
     * @param timestamp 毫秒级时间戳
     * @param zoneId    如 GMT+8或UTC+08:00
     * @return
     */
    public static String timestampToStr(long timestamp,String zoneId) {
        ZoneId timezone = ZoneId.of(zoneId);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp),timezone);
        return localDateTime.toString();
    }

    public static void main(String[] args) {
        Long currentTime = System.currentTimeMillis();
        System.out.println("Current Time : " + currentTime + " = " + timestampToStr(currentTime,"GMT+8"));
        Long dailyStart = getDailyStartTime(currentTime,"GMT+8:00");
        Long dailyEnd = getDailyEndTime(currentTime,"GMT+8:00");
        Long monthStart = getMonthStartTime(currentTime,"GMT+8:00");
        Long monthEnd = getMonthEndTime(currentTime,"GMT+8:00");
        Long yearStart = getYearStartTime(currentTime,"GMT+8:00");
        Long yearEnd = getYearEndTime(currentTime,"GMT+8:00");

        System.out.println("Daily Start : " + dailyStart + " = " + timestampToStr(dailyStart,"GMT+8") + " Daily End : " + dailyEnd + " = " + timestampToStr(dailyEnd,"GMT+8"));
        System.out.println("Month Start : " + monthStart + " = " + timestampToStr(monthStart,"GMT+8") + " Month End : " + monthEnd + " = " + timestampToStr(monthEnd,"GMT+8"));
        System.out.println("Year Start : " + yearStart + " = " + timestampToStr(yearStart,"GMT+8") + " Year End : " + yearEnd + " = " + timestampToStr(yearEnd,"GMT+8"));
    }
}

效果

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读