tags:

views:

158

answers:

3

Is there a way to format a date to have mmm:ss instead of hh:mm:ss? Basically, 7:15:28 should show up instead as 435:28 instead.

The SimpleDateFormat class does not support this format is there one that does or will I have to implement a variation myself?

+4  A: 

You'll have to implement it yourself, but you should bear in mind that implementing a Formatter is not a simple task, as it has many l10n considerations.

David Grant
is this as far as you know or you are sure there's no package out there that can handle this format?
yx
@yx: there may well be packages to help you. My answer is limited to extending the Java SE class.
David Grant
+1  A: 

You could use Joda Time to build a custom formatter. With the DateTimeFormatterBuilder class you could at least show the minutes of a day:

import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;

DateTimeFormatter customFormat = new DateTimeFormatterBuilder()
      .appendMinuteOfDay(1)
      .appendLiteral(':')
      .appendSecondOfMinute(2)
      .toFormatter();
System.out.println(customFormat.print(System.currentTimeMillis()));
Christoph Metzendorf
+2  A: 

If all you want to do is format a Date into the format you specified (with no other parts of the Date included) you can just do it very simply:

   public String format(Date date) {      
      Calendar cal = new GregorianCalendar();
      cal.setTime(date);
      return cal.get(Calendar.HOUR) * 60 + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND);      
   }
staffan