开发者

Joda - remove seconds, milliseconds from PeriodFormat.getDefault().print()?

开发者 https://www.devze.com 2023-04-06 15:01 出处:网络
I have a method like this: public static String getFormattedDateDifference(DateTime startDate, DateTime endDate) {

I have a method like this:

public static String getFormattedDateDifference(DateTime startDate, DateTime endDate) {
        Per开发者_StackOverflow中文版iod p = new Period(startDate, endDate);
        return PeriodFormat.getDefault().print(p);
    }

I'd like to chop off the seconds and milliseconds from printing. How can I do that?


If you only want it down to minutes, why not just specify the right period type to start with?

private static final PeriodType PERIOD_TO_MINUTES = 
     PeriodType.standard().withSecondsRemoved().withMillisRemoved();

public static String getFormattedDateDifference(DateTime startDate,
                                                DateTime endDate) {

    Period p = new Period(startDate, endDate, PERIOD_TO_MINUTES);
    return PeriodFormat.getDefault().print(p);
}

I expect that to format in the way you want.

Note that if these are really meant to be dates, you should probably use PeriodType.yearMonthDay() and specify the values as LocalDate. DateTime should be used for date and time values, not just dates.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号