gpt4 book ai didi

java - 如何使用给定的时间戳将第一个月和最后一个月构建为时间戳?

转载 作者:行者123 更新时间:2023-12-01 15:44:51 26 4
gpt4 key购买 nike

假设给定时间戳时间戳:

我正在使用 Joda Time 来构建该月的第一天:

DateTime dateTime = new DateTime(timestamp);    
MutableDateTime firstOfMonth = dateTime.toMutableDateTime();
firstOfMonth.setDayOfMonth(1);
firstOfMonth.setTime(0, 0, 0, 0);

以及该月的最后一天:

MutableDateTime lastOfMonth = firstOfMonth.toMutableDateTime();
lastOfMonth.addMonths(1);
lastOfMonth.addMillis(-1);

但我想知道计算 firstOfMonthlastOfMonth 这需要这么多代码。有没有更干净的方法?

最佳答案

我在 documentation 中找到了这个

DateTime dt = ...
DateTime firstDayOfMonth = dt.dayOfMonth().withMinimumValue();
DateTime lastDayOfMonth = dt.dayOfMonth().withMaximumValue();

关于java - 如何使用给定的时间戳将第一个月和最后一个月构建为时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7272573/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com