1 24 25 package com.mckoi.util; 26 27 import java.math.BigDecimal ; 28 29 35 36 public class GeneralFormatter { 37 38 43 public static final int MAX_WEEKS = 0; 44 public static final int MAX_DAYS = 1; 45 public static final int MAX_HOURS = 2; 46 public static final int MAX_MINUTES = 3; 47 public static final int MAX_SECONDS = 4; 48 public static final int MAX_MILLISECONDS = 5; 49 50 54 private static final long MILLIS_IN_WEEK = 7 * 24 * 60 * 60 * 1000; 55 private static final long MILLIS_IN_DAY = 24 * 60 * 60 * 1000; 56 private static final long MILLIS_IN_HOUR = 60 * 60 * 1000; 57 private static final long MILLIS_IN_MINUTE = 60 * 1000; 58 private static final long MILLIS_IN_SECOND = 1000; 59 60 64 private static void appendFrame(StringBuffer str, double num, String frame, 65 boolean do_round, boolean append_plural_s) { 66 if (do_round) { 68 num = (long) num; 69 } 70 if (num != 0) { 72 str.append(new BigDecimal (num)); 73 str.append(' '); 74 str.append(frame); 75 if (num != 1 && append_plural_s) { 77 str.append('s'); 78 } 79 str.append(' '); 80 } 81 } 82 83 87 public static void appendWeekType(StringBuffer str, double total, 88 boolean shorthand) { 89 double num; 90 num = total / MILLIS_IN_WEEK; 92 appendFrame(str, num, "week", true, true); 93 num = (total % MILLIS_IN_WEEK) / MILLIS_IN_DAY; 95 appendFrame(str, num, "day", true, true); 96 num = (total % MILLIS_IN_DAY) / MILLIS_IN_HOUR; 98 appendFrame(str, num, "hr", true, true); 99 num = (total % MILLIS_IN_HOUR) / MILLIS_IN_MINUTE; 101 appendFrame(str, num, "min", true, true); 102 num = (total % MILLIS_IN_MINUTE) / MILLIS_IN_SECOND; 104 appendFrame(str, num, "sec", true, false); 105 num = total % MILLIS_IN_SECOND; 107 appendFrame(str, num, "ms", true, false); 108 } 109 110 public static void appendDayType(StringBuffer str, double total, 111 boolean shorthand) { 112 double num; 113 num = total / MILLIS_IN_DAY; 115 appendFrame(str, num, "day", true, true); 116 num = (total % MILLIS_IN_DAY) / MILLIS_IN_HOUR; 118 appendFrame(str, num, "hr", true, true); 119 num = (total % MILLIS_IN_HOUR) / MILLIS_IN_MINUTE; 121 appendFrame(str, num, "min", true, true); 122 num = (total % MILLIS_IN_MINUTE) / MILLIS_IN_SECOND; 124 appendFrame(str, num, "sec", true, false); 125 num = total % MILLIS_IN_SECOND; 127 appendFrame(str, num, "ms", true, false); 128 } 129 130 public static void appendHourType(StringBuffer str, double total, 131 boolean shorthand) { 132 double num; 133 num = total / MILLIS_IN_HOUR; 135 appendFrame(str, num, "hr", true, true); 136 num = (total % MILLIS_IN_HOUR) / MILLIS_IN_MINUTE; 138 appendFrame(str, num, "min", true, true); 139 num = (total % MILLIS_IN_MINUTE) / MILLIS_IN_SECOND; 141 appendFrame(str, num, "sec", true, false); 142 num = total % MILLIS_IN_SECOND; 144 appendFrame(str, num, "ms", true, false); 145 } 146 147 public static void appendMinuteType(StringBuffer str, double total, 148 boolean shorthand) { 149 double num; 150 num = total / MILLIS_IN_MINUTE; 152 appendFrame(str, num, "min", true, true); 153 num = (total % MILLIS_IN_MINUTE) / MILLIS_IN_SECOND; 155 appendFrame(str, num, "sec", true, false); 156 num = total % MILLIS_IN_SECOND; 158 appendFrame(str, num, "ms", true, false); 159 } 160 161 162 163 164 165 } 166 | Popular Tags |