001: package snow.utils;
002:
003: import java.text.SimpleDateFormat;
004: import java.util.GregorianCalendar;
005: import java.util.Calendar;
006: import java.text.DecimalFormat;
007:
008: public final class DateUtils {
009: static final DecimalFormat format0 = new DecimalFormat("0.0");
010:
011: private DateUtils() {
012: }
013:
014: /** For differences between months until millis.
015: * @param diff in millis
016: */
017: public static final String formatTimeDifference(long diff) {
018: return formatTimeDifference(diff, false);
019: }
020:
021: private static long cachedMidnight = 0;
022:
023: public synchronized static long getMidnightTodayMorning() {
024: // SPEEDUP 40 times
025: long dt = System.currentTimeMillis() - cachedMidnight;
026: if (dt >= 0 && dt < 24L * 3600 * 1000) {
027: // still valid
028: return cachedMidnight;
029: }
030:
031: // no more valid => recompute
032: // speed: 170000 per second.
033: cachedMidnight = getStartOfDayForTime(System
034: .currentTimeMillis());
035: return cachedMidnight;
036: }
037:
038: public synchronized static long getStartOfDayForTime(long time) {
039:
040: Calendar cal = GregorianCalendar.getInstance();
041: cal.setTimeInMillis(time);
042: cal.set(Calendar.HOUR_OF_DAY, 0);
043: cal.set(Calendar.MINUTE, 0);
044: cal.set(Calendar.SECOND, 0);
045: cal.set(Calendar.MILLISECOND, 0);
046:
047: return cal.getTimeInMillis();
048: }
049:
050: public static boolean isToday(long t) {
051: long md = getMidnightTodayMorning();
052: if (t - md >= 0 && t - md < 24L * 3600 * 1000)
053: return true;
054: return false;
055: }
056:
057: /** For differences between months until millis.
058: * @param diff in millis
059: */
060: public static final String formatTimeDifference(long diff,
061: boolean notGreaterMultiplierThanHours) {
062: long adiff = Math.abs(diff);
063: if (adiff < 1000L)
064: return "" + diff + " ms";
065: if (adiff < 1000L * 60) {
066: double ds = diff / 1000.0;
067: return roundInt(ds) + " s";
068: }
069: if (adiff < 1000L * 3600) {
070: double dm = diff / 1000.0 / 60.0;
071: if (dm < 2) {
072: return format0.format(dm) + " m";
073: } else {
074: return roundInt(dm) + " m";
075: }
076: }
077: if (notGreaterMultiplierThanHours || adiff < 1000L * 3600 * 24) {
078: return format0.format(diff / 1000.0 / 3600.0) + " h";
079: }
080: if (adiff < 1000L * 3600 * 24 * 30) // THE L IS MANDATORY !!! (overpass int range !!!!!!!!!!!)
081: {
082: double dd = diff / 1000.0 / 3600.0 / 24.0;
083: if (dd < 2) {
084: return format0.format(dd) + " day";
085: } else {
086: int rid = roundInt(dd);
087: return rid + " day" + (rid != 1 ? "s" : "");
088: }
089: }
090: if (adiff < 1000L * 3600 * 24 * 30 * 13) {
091: double md = diff / 1000.0 / 3600.0 / 24.0 / 30;
092: if (md < 1.5) {
093: return format0.format(md) + " month";
094: } else {
095: int rim = roundInt(md);
096: return rim + " month" + (rim != 1 ? "s" : "");
097: }
098: }
099: double yd = diff / 1000.0 / 3600 / 24 / 30 / 12;
100: if (yd < 1.5) {
101: return format0.format(yd) + " year";
102: } else {
103: int riy = roundInt(yd);
104: return "" + riy + " year" + (riy != 1 ? "s" : "");
105: }
106: }
107:
108: public static SimpleDateFormat dateFormatMDY = new SimpleDateFormat(
109: "MMM dd yyyy");
110: public static SimpleDateFormat dateFormatMDYhm = new SimpleDateFormat(
111: "MMM dd yyyy HH:mm");
112: public static SimpleDateFormat dateFormatMDYhmFileName = new SimpleDateFormat(
113: "MMM dd yyyy HH.mm");// without :
114: public static SimpleDateFormat timeFormatHHMM = new SimpleDateFormat(
115: "HH:mm");
116: public static final long oneDayMs = 1000L * 3600 * 24;
117:
118: public static String formatDate(long time) {
119: return dateFormatMDY.format(time);
120: }
121:
122: /** Fixed format. No special "Today" case.
123: */
124: public static String formatDateAndTimeFix(long time) {
125: return dateFormatMDYhm.format(time);
126: }
127:
128: /** Writes today for today, ...
129: */
130: public static String formatDateAndTimeHuman(long time) {
131: if (isToday(time))
132: return "Today " + timeFormatHHMM.format(time);
133: if (isToday(time + oneDayMs))
134: return "Yesterday " + timeFormatHHMM.format(time);
135: if (isToday(time - oneDayMs))
136: return "Tomorrow " + timeFormatHHMM.format(time);
137: return dateFormatMDYhm.format(time);
138: }
139:
140: public static void main(String[] arguments) {
141: formatDate(System.currentTimeMillis());
142: }
143:
144: private static int roundInt(double d) {
145: return (int) Math.round(d);
146: }
147: }
|