在Joda-Time中,将DateTime设置为月初 [英] In Joda-Time, set DateTime to start of month

查看:356
本文介绍了在Joda-Time中,将DateTime设置为月初的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的API允许库客户端传递日期:

My API allows library client to pass Date:

method(java.util.Date date)

使用 Joda-Time ,从这一天起,我想提取一个月,并在本月的所有日子里迭代。

Working with Joda-Time, from this date I would like to extract the month and iterate over all days this month contains.

现在,传递的日期是通常 new Date() - 表示当前即时。我的问题实际上是将新的 DateMidnight(jdkDate)实例设置为在月初。

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.

有人可以演示这个用例 Joda-Time

Could someone please demonstrates this use case with Joda-Time?

推荐答案

当月的第一天开始的午夜是由以下方式给出的:

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);

如果从java.util.Date开始,则使用不同的DateMidnight构造函数:

If starting from a java.util.Date, a different DateMidnight constructor is used:

// first midnight in java.util.Date's month
DateMidnight first = new DateMidnight( date ).withDayOfMonth(1);

Joda时间java doc - http://joda-time.sourceforge.net/api-release/index.html

Joda Time java doc - http://joda-time.sourceforge.net/api-release/index.html

这篇关于在Joda-Time中,将DateTime设置为月初的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆