【问题标题】:How to get date with year change in java如何在java中获取年份变化的日期
【发布时间】:2018-03-26 08:48:30
【问题描述】:

我的方法有以下字段 - id、年份和月份。

Collection<User> userCollection = getUserForMonth(int id, int year, int month);

用户给我日期范围。在 fromTimestamp 和 toTimestamp 之间。我必须使用 getUserForMonth 方法,但我不知道如何动态更改这些参数(年份和月份)。

我从时间戳开始和结束日期是这样的

LocalDate startDate = new Timestamp(csvRaportTransport.getFromTimestamp()).toLocalDateTime().toLocalDate();
LocalDate endDate = new Timestamp(csvRaportTransport.getToTimestamp()).toLocalDateTime().toLocalDate();

我可以像 getStartDate 一样设置年份和月份,但我不知道如何切换它。

我虽然差不多,但如何改变月份和年份?不知道。 你能帮帮我吗?

【问题讨论】:

  • 开始there- LocalDate javadocs。我猜plusYearsplusMonths 是你要找的。​​span>
  • getUserForMonth(int id, int year, int month) 不适用于日期范围,除非您尝试获取日期范围之间的所有月份并为每个月调用该方法
  • @Shadi 是的,我试着把所有的月份和年份都放在一起。我不能使用 plusMonth/plusYear,因为我可能会错过一些日期。
  • 您的问题不清楚。您是否尝试将开始和停止日期作为一个范围来查询数据库中包含该范围内日期的行?
  • @Tom 你的评论毫无意义。为了清楚起见,请修改您的问题。以unclear what you're asking 投票结束。

标签: java localdate


【解决方案1】:

如果您想获取 startDate 和 StartYear 之间所有月份的所有用户,您可以这样做:

    LocalDate startDate = LocalDate.of(2018, 01, 1);
    LocalDate endDate = LocalDate.of(2018, 03, 1);
    while(startDate.isBefore(endDate))
    {
        getUserForMonth(123,startDate.getYear(), startDate.getMonthValue());
        startDate = startDate.plusMonths(1);//.plusYears if you want years
    }

如果您想获取与 startDate 和 endDate 相关的特定月份的用户,您可以使用:

    LocalDate startDate = LocalDate.of(2018, 01, 1);
    LocalDate endDate = LocalDate.of(2018, 03, 1);

    getUserForMonth(123,startDate.getYear(), startDate.getMonthValue());
    getUserForMonth(123,endDate.getYear(), endDate.getMonthValue());

我希望它有所帮助。 正如用户 xxxvodnikxxx 所提到的,您可以在他的评论中提供的 LocalDate javadoc 中阅读更多相关信息

【讨论】:

  • 我强烈建议使用isBefore 而不是equals
  • 我在你的 while 循环中使用了 isBefore。问题是如果我使用日期 2017-12-29 和 2018-01-05 我不能得到 2018 年。
  • 根据 Max 的评论改为 isBefore。为什么您无法获得正确的年份?我运行时得到以下响应: while(startDate.isBefore(endDate)) { getUserForMonth(123,startDate.getYear(), startDate.getMonthValue()); startDate = startDate.plusDays(1); System.out.println(startDate);控制台:2017-12-30 2017-12-31 2018-01-01 2018-01-02 2018-01-03 2018-01-04 2018-01-05
  • 我犯了一些错误。现在它起作用了。太感谢了! :-)
【解决方案2】:

要获取日期范围之间的所有月份,请使用

    public List<LocalDate> getMonthsBetween(LocalDate startDate, LocalDate endDate) {
        long numOfMonthsBetween = ChronoUnit.MONTHS.between(startDate, endDate);
        if(numOfMonthsBetween == 0){
        // means that start and end dates are in the same month
            return Collections.singletonList(startDate);
        }
        return IntStream.iterate(0, i -> i+1)
            .limit(numOfMonthsBetween)
            .mapToObj(i -> startDate.plusMonths(i))
            .collect(Collectors.toList());
    }

然后你可以遍历日期

getMonthsBetween(LocalDate.now(),LocalDate.now().plusMonths(11)).stream()
        .forEach(System.out::println);

这会大喊大叫

2018-03-26
2018-04-26
2018-05-26
2018-06-26
2018-07-26
2018-08-26
2018-09-26
2018-10-26
2018-11-26
2018-12-26
2019-01-26

只需在 forEach 中调用您的 getUserForMonth(int id, int year, int month) 获取每个月的结果并将子结果添加到您计划返回的最终集合中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    相关资源
    最近更新 更多