
{filelink=6274}
import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
/**
* FormatHeure.
*
* Date: Sep 17, 2005
* Time: 1:01:13 PM
*
* @Auteur Hendrik Schreiber
*/
public class FormatHeure extends DateFormat {
private static final long UNE_SECONDE = 1000l;
private static final long UNE_MINUTE = UNE_SECONDE * 60l;
private static final long UNE_HEURE = UNE_MINUTE * 60l;
private static final long UN_JOUR = UNE_HEURE * 24l;
private SimpleDateFormat Formatseconds = new SimpleDateFormat("s's'");
private SimpleDateFormat formatMinute = new SimpleDateFormat("m'm'");
private SimpleDateFormat formatHeure = new SimpleDateFormat("H'h'");
private DateFormat fullFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM);
public TimeFormat() {
final TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
formatMinute.setTimeZone(utcTimeZone);
formatHeure.setTimeZone(utcTimeZone);
}
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
long time = date.getTime();
if (time >= UN_JOUR * 365) {
return fullFormat.format(date, toAppendTo, fieldPosition);
}
if (time >= UN_JOUR * 3) {
toAppendTo.append(time / UN_JOUR);
toAppendTo.append('d');
if (time % UN_JOUR != 0) {
formatHeure.format(date, toAppendTo, fieldPosition);
}
}
else if (time >= UNE_HEURE) {
toAppendTo.append(time / UNE_HEURE);
toAppendTo.append('h');
}
if (time >= UNE_MINUTE && time % UNE_HEURE !=0) {
formatMinute.format(date, toAppendTo, fieldPosition);
}
if (time >= UNE_SECONDE && time % UNE_MINUTE !=0) {
Formatseconds.format(date, toAppendTo, fieldPosition);
}
return toAppendTo;
}
public Date parse(String source, ParsePosition pos)
{
throw new RuntimeException("");
}
}