문제

이 질문은 이미 여기에 답이 있습니다.

나는 사용하고있다 조다-시간 Java가있는 도서관. "X Days, X 시간, X 분"형식의주기 개체를 문자열로 바꾸는 데 어려움이 있습니다.

이 기간 객체는 먼저 몇 초를 추가하여 만들어집니다 (XML에 몇 초로 직렬화 된 다음 재현됩니다). 내가 단순히 gethours () 등을 사용하면 내가 얻는 모든 방법은 0과 getseconds의 초 금액.

Joda가 몇 초를 날, 시간 등과 같이 각 분야로 계산할 수 있도록하려면 어떻게해야합니까?

도움이 되었습니까?

해결책

총 2 초로 구성되면 유일한 가치이므로 기간을 정규화해야합니다. 정규화하면 총 일, 분, 초 등으로 분류됩니다.

Ripper234에 의해 편집 - 추가 a TL; DR 버전: PeriodFormat.getDefault().print(period)

예를 들어:

public static void main(String[] args) {
  PeriodFormatter daysHoursMinutes = new PeriodFormatterBuilder()
    .appendDays()
    .appendSuffix(" day", " days")
    .appendSeparator(" and ")
    .appendMinutes()
    .appendSuffix(" minute", " minutes")
    .appendSeparator(" and ")
    .appendSeconds()
    .appendSuffix(" second", " seconds")
    .toFormatter();

  Period period = new Period(72, 24, 12, 0);

  System.out.println(daysHoursMinutes.print(period));
  System.out.println(daysHoursMinutes.print(period.normalizedStandard()));
}

인쇄 :

24 minutes and 12 seconds
3 days and 24 minutes and 12 seconds

따라서 정상화되지 않은 기간의 출력이 단순히 시간 수를 무시하는 것을 볼 수 있습니다 (72 시간을 3 일로 변환하지 않았습니다).

다른 팁

대부분의 경우 기본 포맷터를 사용할 수도 있습니다.

Period period = new Period(startDate, endDate);
System.out.println(PeriodFormat.getDefault().print(period))
    Period period = new Period();
    // prints 00:00:00
    System.out.println(String.format("%02d:%02d:%02d", period.getHours(), period.getMinutes(), period.getSeconds()));
    period = period.plusSeconds(60 * 60 * 12);
    // prints 00:00:43200
    System.out.println(String.format("%02d:%02d:%02d", period.getHours(), period.getMinutes(), period.getSeconds()));
    period = period.normalizedStandard();
    // prints 12:00:00
    System.out.println(String.format("%02d:%02d:%02d", period.getHours(), period.getMinutes(), period.getSeconds()));
PeriodFormatter daysHoursMinutes = new PeriodFormatterBuilder()
    .appendDays()
    **.appendSuffix(" day", " days")
    .appendSeparator(" and ")
    .appendMinutes()
    .appendSuffix(" minute", " minutes")**
    .appendSeparator(" and ")
    .appendSeconds()
    .appendSuffix(" second", " seconds")
    .toFormatter();

당신은 시간을 놓치고 있습니다. 그래서 그 이유입니다. 며칠 후 몇 시간을 추가하고 문제가 해결되었습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top