本文介绍了JodaTime相当于DateUtils.truncate()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前我从未使用过,但回答了这个问题,。

I have never used JodaTime before, but answering this question, How to get ordinal Weekdays in a Month.

我试了一下,想出了这个丑陋的代码,以便在一天之内取消设置所有字段:

I tried it and came up with this ugly code to unset all fields below day:

DateTime startOfMonth =
    input.withDayOfMonth(1)
        .withHourOfDay(0)       // there
        .withMinuteOfHour(0)    // has got to
        .withSecondOfMinute(0)  // be a shorter way
        .withMillisOfSecond(0); // to do this

相当于使用将

Date startOfMonth = DateUtils.truncate(input, Calendar.MONTH);

在JodaTime中,首选的成语是什么?

What's the preferred idiom to do that in JodaTime?

推荐答案

您可以使用 模仿DateUtils.truncate

You can use roundFloorCopy() to mimic DateUtils.truncate

Date truncateMonth = DateUtils.truncate(input, Calendar.MONTH);
-> DateTime truncateMonth = input.dayOfMonth().roundFloorCopy();

Date truncateMinute = DateUtils.truncate(input, Calendar.MINUTE);
-> DateTime truncateMinute = input.minuteOfDay().roundFloorCopy();

Date truncateHourOfDay = DateUtils.truncate(input, Calendar.HOUR_OF_DAY);
-> DateTime truncateHourOfDay = input.hourOfDay().roundFloorCopy()

这篇关于JodaTime相当于DateUtils.truncate()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 19:43