Period to string [duplicate]
To pytanie ma już odpowiedź tutaj:
- Convert jodatime duration to string 2 odpowiedzi
Używam biblioteki Joda-Time z Javą. Mam pewne trudności z zamianą obiektu kropki na ciąg znaków w formacie "X dni, X godzin, X minut".
Te obiekty Period są najpierw tworzone przez dodanie ilości sekund do nich (są serializowane do XML jako sekundy, a następnie odtwarzane z nich). Jeśli po prostu użyję getHours () itp. metody w nich, wszystko co dostaję to zero i całkowita ilość sekund z getSeconds.
Jak mogę zmusić jodę do obliczenia sekund w odpowiednich polach, takich jak dni, godziny itp...?
4 answers
Musisz znormalizować okres, ponieważ jeśli skonstruujesz go z całkowitą liczbą sekund, to jest to jedyna wartość, jaką ma. Normalizacja go rozbije na całkowitą liczbę dni, Minut, Sekund itp.
Edit by ripper234 - dodanie TL; wersja DR: PeriodFormat.getDefault().print(period)
Na przykład:
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()));
}
Wydrukuje:
24 minutes and 12 seconds
3 days and 24 minutes and 12 seconds
Więc możesz zobaczyć wyjście dla nienormalizowanego okresu po prostu ignoruje liczbę godzin (nie przekonwertował 72 godziny do 3 dni).
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:02:45
Możesz również użyć domyślnego formatera, który jest dobry w większości przypadków:
Period period = new Period(startDate, endDate);
System.out.println(PeriodFormat.getDefault().print(period))
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-25 14:04:32
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()));
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-09-17 18:40:35
PeriodFormatter daysHoursMinutes = new PeriodFormatterBuilder()
.appendDays()
**.appendSuffix(" day", " days")
.appendSeparator(" and ")
.appendMinutes()
.appendSuffix(" minute", " minutes")**
.appendSeparator(" and ")
.appendSeconds()
.appendSuffix(" second", " seconds")
.toFormatter();
/ Align = "left" / Dołącz godziny po dniach i problem rozwiązany.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-02-13 17:32:37