java根据时间戳 获取该时间戳所在周 月 年 最后时刻(的时间戳)

测试类

public class TestDate {
    public static void main(String[] args) {
        TimeData weekTime = DateUtil.getWeekTime(1604542728478l);   //2020-11-05 10:18:48
        System.out.println(weekTime+" weekTime");

        Date date = new Date(1604542728478l);
        System.out.println(date+"  date");  //Thu Nov 05 10:18:48 CST 2020 = 2020-11-05 10:18:48

        long monthTime = cn.hutool.core.date.DateUtil.endOfMonth(date).getTime();
        System.out.println(monthTime+"  monthTime");    //1606751999999 = 2020-11-30 23:59:59

        long yearTime = cn.hutool.core.date.DateUtil.endOfYear(date).getTime();
        System.out.println(yearTime+"  yearTime");  //1609430399999 = 2020-12-31 23:59:59
    }
}

实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class TimeData {
    /**
     * 数据查询的开始时间
     */
    private Long startTime;
    /**
     * 数据查询的结束时间
     */
    private Long endTime;

}

工具类

public class DateUtil {

    public static TimeData getWeekTime(Long day) {
        DateTime date = new DateTime(day);
        //获取周的星期一
        //判断周一是否在上个月
        //日所在的月
        int dayMonth = cn.hutool.core.date.DateUtil.month(date);
        //周开始所在的月
        DateTime beginOfWeek = cn.hutool.core.date.DateUtil.beginOfWeek(date);
        int beginMonth = cn.hutool.core.date.DateUtil.month(beginOfWeek);
        //周结束所在的月
        DateTime endOfWeek = cn.hutool.core.date.DateUtil.endOfWeek(date);
        int endMonth = cn.hutool.core.date.DateUtil.month(endOfWeek);

        DateTime startTime;
        DateTime endTime;
        if (beginMonth == endMonth) {
            //周开始和结束在一个月内
            startTime = beginOfWeek;
            endTime = endOfWeek;
        } else if (beginMonth == dayMonth) {
            //周开始和日在一个月内
            startTime = beginOfWeek;
            endTime = cn.hutool.core.date.DateUtil.endOfMonth(date);
        } else {
            //周结束和日在一个月内
            startTime = cn.hutool.core.date.DateUtil.beginOfMonth(date);
            endTime = endOfWeek;
        }
        endTime.setField(DateField.HOUR_OF_DAY, 23);
        endTime.setField(DateField.SECOND, 59);
        endTime.setField(DateField.MINUTE, 59);
        //判断本周结束时间是否是下个月,是的话 取月结束时间,不是,取周结束时间
        return new TimeData(startTime.getTime(), endTime.getTime());
    }
}

运行结果

TimeData(startTime=1604246400000, endTime=1604851199999) weekTime
Thu Nov 05 10:18:48 CST 2020  date
1606751999999  monthTime
1609430399999  yearTime

这辈子坚持与不坚持都不可怕,怕的是独自走在坚持的道路上!!
欢迎加入技术群聊。

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您好,根据时间戳获取有多少可以使用 Java 中的 Calendar 类和 SimpleDateFormat 类来实现。具体代码如下: ```java import java.text.SimpleDateFormat; import java.util.Calendar; public class Main { public static void main(String[] args) { long timestamp = System.currentTimeMillis(); // 获取当前时间戳 Calendar calendar = Calendar.getInstance(); // 获取 Calendar 实例 calendar.setTimeInMillis(timestamp); // 设置时间戳 int year = calendar.get(Calendar.YEAR); // 获取份 calendar.set(Calendar.MONTH, 11); // 设置份为 12 calendar.set(Calendar.DAY_OF_MONTH, 31); // 设置日期为 31 号 int week = calendar.get(Calendar.WEEK_OF_YEAR); // 获取数 System.out.println(year + "有" + week + ""); } } ``` 以上代码可以输出当前份有多少。如果需要获取指定时间戳所在份的数,只需要将 `timestamp` 替换为指定时间戳即可。 ### 回答2: 在Java中,要根据时间戳获取有多少,可以使用java.time包中的LocalDate类来实现。步骤如下: 首先,需要将时间戳转换为对应的LocalDate对象,可以使用Instant类的ofEpochMilli方法将时间戳转换为Instant对象,再使用atZone方法将Instant对象转换为对应的ZoneId时区对象,最后使用toLocalDate方法将ZoneId时区对象转换为LocalDate对象。 然后,可以使用LocalDate对象的get方法获取当前份,并通过Year对象的of方法创建对应的Year对象。 接下来,可以使用Year对象的length和isLeap方法来分别获取的天数和是否为闰最后,可以根据每7天的规则,将总天数除以7来得到当数。需要注意的是,如果当最后不足7天,则该不算作完整的一。 示例代码如下: ``` import java.time.*; import java.time.temporal.*; public class Main { public static void main(String[] args) { long timestamp = 1611729238000L; // 时间戳 // 将时间戳转换为LocalDate对象 LocalDate date = Instant.ofEpochMilli(timestamp) .atZone(ZoneId.systemDefault()) .toLocalDate(); // 获取对应的份 Year year = Year.of(date.getYear()); // 获取的天数和是否闰 int yearLength = year.length(); boolean isLeap = year.isLeap(); // 计算当数 int weeks = yearLength / 7; if (yearLength % 7 > 0) { weeks++; } // 输出结果 System.out.println("当数为:" + weeks); } } ``` 运行结果: 当数为:52 以上就是使用Java根据时间戳获取有多少的方法。 ### 回答3: Java中可以通过Calendar类和SimpleDateFormat类来获取有多少。 首先,我们可以使用Calendar类来获取当前的份,并创建一个Calendar对象来表示当前日期。然后,我们将日期设置为当的第一天(即11日),并将时间设置为0点。 接下来,我们使用SimpleDateFormat类来格式化日期,将日期转换为指定的格式。我们可以使用"ww"格式来获取该日期所在数。 然后,我们不断将日期增加7天,直到日期超过当最后一天。在每次增加7天后,我们获取当前日期的数,并将其与上次获取数比较,如果不相等,则表示有新的一开始。以此类推,我们可以计算出当有多少最后,我们将每数累加起来,并得到当的总数。 以下是一个示例代码: ```java import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Main { public static void main(String[] args) { // 获取当前份 Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); // 设置日期为当的第一天,时间为0点 calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, Calendar.JANUARY); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); // 计算当数 int totalWeeks = 0; SimpleDateFormat dateFormat = new SimpleDateFormat("ww"); Date currentDate = calendar.getTime(); String currentWeek = dateFormat.format(currentDate); while (calendar.get(Calendar.YEAR) == year) { calendar.add(Calendar.DAY_OF_MONTH, 7); currentDate = calendar.getTime(); String newWeek = dateFormat.format(currentDate); if (!newWeek.equals(currentWeek)) { totalWeeks++; currentWeek = newWeek; } } System.out.println("当前份为:" + year); System.out.println("当共有" + totalWeeks + ""); } } ``` 运行上述代码,将输出当前份和当的总数。注意,这里的数是基于ISO 8601标准的,即每的起始日期为一,每的第一个一所属的数为第1

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值