Java 在 Joda-Time 中,将 DateTime 设置为月初
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2261480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Joda-Time, set DateTime to start of month
提问by Maxim Veksler
My API allows library client to pass Date:
我的 API 允许库客户端传递日期:
method(java.util.Date date)
Working with Joda-Time, from this date I would like to extract the month and iterate over all days this month contains.
使用Joda-Time,从这个日期开始,我想提取月份并迭代这个月包含的所有天数。
Now, the passed date is usually new Date()- meaning current instant. My problem actually is setting the new DateMidnight(jdkDate)instance to be at the start of the month.
现在,传递的日期通常是new Date()- 表示当前时刻。我的问题实际上是将新的DateMidnight(jdkDate)实例设置为月初。
Could someone please demonstrates this use case with Joda-Time?
有人可以用Joda-Time演示这个用例吗?
采纳答案by Lachlan Roche
Midnight at the start of the first day of the current month is given by:
当月第一天开始的午夜由下式给出:
// first midnight in this month
DateMidnight first = new DateMidnight().withDayOfMonth(1);
// last midnight in this month
DateMidnight last = first.plusMonths(1).minusDays(1);
If starting from a java.util.Date, a different DateMidnight constructor is used:
如果从 java.util.Date 开始,则使用不同的 DateMidnight 构造函数:
// first midnight in java.util.Date's month
DateMidnight first = new DateMidnight( date ).withDayOfMonth(1);
Joda Time java doc - https://www.joda.org/joda-time/apidocs/overview-summary.html
Joda 时间 java 文档 - https://www.joda.org/joda-time/apidocs/overview-summary.html
回答by andi5
Oh, I did not see that this was about jodatime. Anyway:
哦,我没有看到这是关于 jodatime 的。反正:
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
int min = c.getActualMinimum(Calendar.DAY_OF_MONTH);
int max = c.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int i = min; i <= max; i++) {
c.set(Calendar.DAY_OF_MONTH, i);
System.out.println(c.getTime());
}
Or using commons-lang:
或者使用 commons-lang:
Date min = DateUtils.truncate(date, Calendar.MONTH);
Date max = DateUtils.addMonths(min, 1);
for (Date cur = min; cur.before(max); cur = DateUtils.addDays(cur, 1)) {
System.out.println(cur);
}
回答by ngeek
An alternative way (without taking DateMidnight into account) to get the first day of the month would be to use:
另一种方法(不考虑 DateMidnight)获取该月的第一天是使用:
DateTime firstDayOfMonth = new DateTime().dayOfMonth().withMinimumValue();
回答by Basil Bourque
First Moment Of The Day
一天中的第一刻
The answer by ngeekis correct, but fails to put the time to the first moment of the day. To adjust the time, append a call to withTimeAtStartOfDay
.
ngeek的答案是正确的,但没有把时间放在一天的第一刻。要调整时间,请将调用附加到withTimeAtStartOfDay
。
// ? 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
org.joda.time.DateTime startOfThisMonth = new org.joda.time.DateTime().dayOfMonth().withMinimumValue().withTimeAtStartOfDay();
org.joda.time.DateTime startofNextMonth = startOfThisMonth.plusMonths( 1 ).dayOfMonth().withMinimumValue().withTimeAtStartOfDay();
System.out.println( "startOfThisMonth: " + startOfThisMonth );
System.out.println( "startofNextMonth: " + startofNextMonth );
When run in Seattle US…
在美国西雅图跑步时……
startOfThisMonth: 2013-11-01T00:00:00.000-07:00
startofNextMonth: 2013-12-01T00:00:00.000-08:00
Note the difference in those two lines of console output: -7 vs -8 because of Daylight Saving Time.
请注意这两行控制台输出的区别: -7 与 -8 因为夏令时。
Generally one should always specify the time zone rather than rely on default. Omitted here for simplicity. One should add a line like this, and pass the time zone object to the constructors used in example above.
通常应该始终指定时区而不是依赖默认值。为简单起见,此处省略。应该添加这样的一行,并将时区对象传递给上面示例中使用的构造函数。
// Time Zone list: http://joda-time.sourceforge.net/timezones.html (Possibly out-dated, read note on that page)
// UTC time zone (no offset) has a constant, so no need to construct: org.joda.time.DateTimeZone.UTC
org.joda.time.DateTimeZone kolkataTimeZone = org.joda.time.DateTimeZone.forID( "Asia/Kolkata" );
java.time
时间
The above is correct but outdated. The Joda-Time library is now supplanted by the java.time framework built into Java 8 and later.
以上是正确的,但已经过时了。Joda-Time 库现在被 Java 8 及更高版本中内置的 java.time 框架所取代。
The LocalDate
represents a date-only value without time-of-day and without time zone. A time zone is crucial in determine a date. For any given moment the date varies by zone around the globe.
该LocalDate
代表没有时间一天和不同时区的日期,唯一的价值。时区对于确定日期至关重要。对于任何特定时刻,日期因全球区域而异。
ZoneId zoneId = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( zoneId );
Use one of the TemporalAdjusters
to get first of month.
使用其中一个TemporalAdjusters
来获得第一个月。
LocalDate firstOfMonth = today.with( TemporalAdjusters.firstDayOfMonth() );
The LocalDate
can generate a ZonedDateTime
that represents the first moment of the day.
该LocalDate
可生成ZonedDateTime
代表这一天的第一刻。
ZonedDateTime firstMomentOfCurrentMonth = firstOfMonth.atStartOfDay( zoneId );
回答by JustinKSU
DateMidnight
is now deprecated. Instead you can do:
DateMidnight
现在已弃用。相反,您可以这样做:
LocalDate firstOfMonth = new LocalDate(date).withDayOfMonth(1);
LocalDate lastOfMonth = firstOfMonth.plusMonths(1).minusDays(1);
If you know the time zone use new LocalDate(date, timeZone)
instead for greater accuracy.
如果您知道时区,请new LocalDate(date, timeZone)
改用以获得更高的准确性。
You can also do .dayOfMonth().withMinimumValue()
instead of .withDayOfMonth(1)
你也可以.dayOfMonth().withMinimumValue()
代替.withDayOfMonth(1)
EDIT:
编辑:
This will give you 12/1/YYYY 00:00
and 12/31/YYYY 00:00
. If you rather the last of the month be actually the first of the next month (because you are doing a between clause), then remove the minusDays(1)
from the lastOfMonth calculation
这会给你12/1/YYYY 00:00
和12/31/YYYY 00:00
。如果您希望本月的最后一个月实际上是下个月的第一天(因为您正在执行 between 子句),minusDays(1)
则从 lastOfMonth 计算中删除