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

I have a way like this:

public static String getFormattedDateDifference(DateTime startDate, DateTime endDate) { Period p = new Period(startDate, endDate); return PeriodFormat.getDefault().print(p); } 

I would like to chop off seconds and milliseconds from printing. How can i do this?

+6
source share
1 answer

If you want it to be just a few minutes, why not just specify the right type of period 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 the format to be what you want.

Note: if they are really for 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.

+9
source

Source: https://habr.com/ru/post/897852/


All Articles