java - JodaTime 相当于 DateUtils.truncate()

标签 java jodatime

我从未使用过JodaTime之前,但回答这个问题,How to get ordinal Weekdays in a Month .

我试过了,想出了这个丑陋的代码来取消设置下面的所有字段:

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

Commons / Lang 在哪里等效使用 DateUtils会是

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

在 JodaTime 中做这件事的首选习惯用法是什么?

最佳答案

您可以使用 roundFloorCopy() 模仿 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()

关于java - JodaTime 相当于 DateUtils.truncate(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4133682/

相关文章:

spring - JodaTime 和 BeanPropertySqlParameterSource

JAVA和JODA Date解析即使格式错误也不会抛出异常

jpa - LocalDateTime 和 JodaDate 冲突

java - 将 JodaTime 持续时间转换为字符串

java - 为什么这个 Joda-Time 相关测试有时会失败?

java - 如何从 HttpServerExchange 获取请求正文?

java - IIS 重写 BadRequest 响应数据

Java:查找第一个正则表达式的索引

Java 无限循环

java - Spring获取数据库路径时出错