2011-09-01 91 views
0

假设给定一个Timestamp timestamp如何建立月份的第一个和最后一个作为时间戳,给定的时间戳?

我使用约达时间来构建该月的第一天:

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这需要这么多的代码。有更清洁的方法吗?

+0

这是相当不错的覆盖(尽管不正是这个问题)[这里] [1]。 [1]:http://stackoverflow.com/questions/7130948/joda-time-most-recent-week-and-month –

回答

1

我发现这个在documentation

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

这看起来很有希望 - 它实际上它设置为最后毫秒该月份,还是只更新日期字段? –

+0

据我所知,只有相应的属性(在这种情况下'dateOfMonth')被设置为取决于DateTime的最小/最大值。我目前无法测试它。 – Jiri

相关问题