相关文章推荐
强悍的红薯  ·  Java 中分布式 ID 的设计方案 | ...·  2 月前    · 
会开车的仙人掌  ·  CAST - Amazon Kinesis ...·  2 月前    · 
冲动的野马  ·  数据类型强制转换 | Vertica 12.0.x·  2 月前    · 
乖乖的围巾  ·  Twitter-Snowflake,64位自 ...·  1 月前    · 
爱旅游的汽水  ·  Mysql · 戈壁堂·  2 周前    · 
火星上的海豚  ·  食鹽- 维基词典,自由的多语言词典·  5 月前    · 
打篮球的卤蛋  ·  火锅必备食材有哪些_蔬菜类·  5 月前    · 
没读研的火柴  ·  关于举报联系方式的公告-深圳市市场监督管理局·  5 月前    · 
腹黑的西装  ·  星际战甲官网:成为天诺!·  6 月前    · 
朝气蓬勃的豌豆  ·  杨绛今日凌晨去世,享年105岁·  6 月前    · 
Code  ›  Java获取当天、当前月、当前年(今年)的开始和结束时间戳开发者社区
gmt 时间戳
https://cloud.tencent.com/developer/article/2285686
傲视众生的熊猫
7 月前
翎野君

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

前往小程序,Get 更优 阅读体验!
立即前往
腾讯云
开发者社区
文档 建议反馈 控制台
首页
学习
活动
专区
圈层
工具
MCP广场
文章/答案/技术大牛
发布
首页
学习
活动
专区
圈层
工具
MCP广场
返回腾讯云官网
翎野君
首页
学习
活动
专区
圈层
工具
MCP广场
返回腾讯云官网
社区首页 > 专栏 > Java获取当天、当前月、当前年(今年)的开始和结束时间戳

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

作者头像
翎野君
发布 于 2023-05-12 20:11:10
发布 于 2023-05-12 20:11:10
3.4K 0 0
代码可运行
举报
文章被收录于专栏: 翎野君 翎野君
运行总次数: 0
代码可运行

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

相关代码

代码语言: javascript
代码 运行次数: 0
运行
复制
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, String timeZone) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(timeStamp);
        calendar.set(Calendar.HOUR_OF_DAY, 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);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
     * 获取当月的结束时间戳
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  如 GMT+8:00
     * @return
    public static Long getMonthEndTime(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, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));// 获取当前月最后一天
        calendar.set(Calendar.HOUR_OF_DAY, 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 getYearStartTime(Long timeStamp, String timeZone) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(timeStamp);
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.DATE, 0);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        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 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, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 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");
 
推荐文章
强悍的红薯  ·  Java 中分布式 ID 的设计方案 | 小豆丁技术栈
2 月前
会开车的仙人掌  ·  CAST - Amazon Kinesis Data Analytics SQL 参考
2 月前
冲动的野马  ·  数据类型强制转换 | Vertica 12.0.x
2 月前
乖乖的围巾  ·  Twitter-Snowflake,64位自增ID算法详解 - 漫漫路
1 月前
爱旅游的汽水  ·  Mysql · 戈壁堂
2 周前
火星上的海豚  ·  食鹽- 维基词典,自由的多语言词典
5 月前
打篮球的卤蛋  ·  火锅必备食材有哪些_蔬菜类
5 月前
没读研的火柴  ·  关于举报联系方式的公告-深圳市市场监督管理局
5 月前
腹黑的西装  ·  星际战甲官网:成为天诺!
6 月前
朝气蓬勃的豌豆  ·  杨绛今日凌晨去世,享年105岁
6 月前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号