java - Convert seconds to years/days/hours/minutes automatically, using JodaTime? -
is there way convert 'x' seconds y hours , z seconds when x exceeds 3600 seconds? similarly, convert 'a minutes , b seconds' when x exceeds 60 less 3600 seconds, using jodatime? understand have specify need in periodformatter, don't want specify - want formatted text based on value of seconds.
this similar how post on forum , post shown 'posted 10 seconds ago'.. after 1 minute see 'posted 1minute 20 seconds ago' , likewise weeks,days,years.
i'm not sure why don't want specify need in periodformatter. jodatime doesn't know how want display period string, need tell via periodformatter.
as 3600 seconds 1 hour, using formatter automatically you. here's code example using number of different inputs on same formatter should achieve desired result.
seconds s1 = seconds.seconds(3601); seconds s2 = seconds.seconds(2000); seconds s3 = seconds.seconds(898298); period p1 = new period(s1); period p2 = new period(s2); period p3 = new period(s3); periodformatter dhm = new periodformatterbuilder() .appenddays() .appendsuffix(" day", " days") .appendseparator(" , ") .appendhours() .appendsuffix(" hour", " hours") .appendseparator(" , ") .appendminutes() .appendsuffix(" minute", " minutes") .appendseparator(" , ") .appendseconds() .appendsuffix(" second", " seconds") .toformatter(); system.out.println(dhm.print(p1.normalizedstandard())); system.out.println(dhm.print(p2.normalizedstandard())); system.out.println(dhm.print(p3.normalizedstandard())); produces output::
1 hour , 1 second
33 minutes , 20 seconds
3 days , 9 hours , 31 minutes , 38 seconds
Comments
Post a Comment