Pregunta

I currently work on a double value that represent the total consumed time

for example, I have a 260 that means 260 second is consumed

To display to user, I would like to format it

for example , it should be something like 0year,0month,0day,1hr,2min,30sec

But I found the SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); is not quite suit my case (seems the "h" in "hr" is conflicted with the hour symbol?)

So , how to change the HH:mm:ss to the case mentioned above?

Thanks for helping

¿Fue útil?

Solución

DateFormat is useful to format dates, not an absolute value of time.

To achieve your goal, you can take a look to Formatter

Hope this sample helps you:

String total_consumed_time = String.format("%01d year, %01d month, %01d day, %01d hr, %01d min, %01d sec", time_year, time_month, time_day, time_hour, time_min, time_seg);

I didn't try that code, but I use similar workaround with an absolute time in milliseconds:

long time = 260000; // time in mseg
long time_hour = TimeUnit.MILLISECONDS.toHours(time);
time -= TimeUnit.HOURS.toMillis(time_hour);
long time_min = TimeUnit.MILLISECONDS.toMinutes(time);
time -= TimeUnit.MINUTES.toMillis(time_min);
long time_seg = TimeUnit.MILLISECONDS.toSeconds(time);
String total_time = String.format("%02d:%02d:%02d", time_hour, time_min, time_seg);

With a result of "00:04:20" (4 minutes and 20 seconds).

Otros consejos

Accepted answer is in most cases okay for solving your problem, but gives wrong reason why not to use the class SimpleDateFormat. This format class is well suited for objects of type java.util.Date (which are kind of unix timestamps in milliseconds hence absolute value of time, NOT dates). In order to treat letters like "hr" as literals you need to escape them. Example code:

// create timestamp
java.util.Date jud = new java.util.Date(260 * 1000); // milliseconds

// create format for timestamp
SimpleDateFormat sdf = 
  new SimpleDateFormat("yyyy'year',M'month',d'day',H'hr',m'min',s'sec'");

// otherwise you will get extra offset time (example: in England +1 hour DST)
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

// output: 1970year,1month,1day,0hr,4min,20sec
String formatted = sdf.format(jud);
System.out.println(formatted);

Even with the applied and tricky time zone correction in code you face the problem that you have an output for the year 1970, a point in time. Hereby you can see that SimpleDateFormat does format timestamps well (absolute values in time) but NOT durations (amount resp. length of time). This semantic problem can also not be solved by the approach to use java.util.Formatter as soon as the input increases the day limit of 86400 seconds.

Old JDK and Android don't offer a built-in solution for evaluating time differences expressed in years, months and days. Java 8 does offer (limited) support with new API (class 'Period' only for date part, not time part). External libraries like JodaTime or my own one (actually only as alpha-version) give more support. JodaTime even offers a special PeriodFormatter which is ideal for solving your problem.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top