KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > utils > DateUtils


1 package snow.utils;
2
3 import java.text.*;
4 import java.util.Date JavaDoc;
5 import java.util.GregorianCalendar JavaDoc;
6 import java.util.Calendar JavaDoc;
7 import java.text.DecimalFormat JavaDoc;
8
9 /** DateUtils.
10 */

11 public final class DateUtils
12 {
13    static final DecimalFormat JavaDoc format0 = new DecimalFormat JavaDoc("0.0");
14    public static SimpleDateFormat dateFormatMDY = new SimpleDateFormat("MMM dd yyyy");
15    public static SimpleDateFormat dateFormatMDYhm = new SimpleDateFormat("MMM dd yyyy HH:mm");
16    public static SimpleDateFormat timeFormatHHMM = new SimpleDateFormat("HH:mm");
17    public static final long oneDayMs = 1000L*3600*24;
18
19
20
21    private DateUtils()
22    {
23    }
24
25
26   /** Fixed format. No special "Today" case.
27   */

28   public static String JavaDoc formatDateAndTimeFix(long time)
29   {
30      return dateFormatMDYhm.format(time);
31   }
32
33   /** For differences between months until millis.
34   * @param diff in millis
35   */

36   public static final String JavaDoc formatTimeDifference(long diff)
37   {
38      return formatTimeDifference(diff,false);
39   }
40
41
42    private static long cachedMidnight = 0;
43
44    public synchronized static long getMidnightTodayMorning()
45    {
46       // SPEEDUP 40 times
47
long dt = System.currentTimeMillis() - cachedMidnight;
48       if(dt>=0 && dt < 24L*3600*1000)
49       {
50          // still valid
51
return cachedMidnight;
52       }
53
54       // no more valid => recompute
55
// speed: 170000 per second.
56
cachedMidnight = getStartOfDayForTime(System.currentTimeMillis());
57       return cachedMidnight;
58    }
59
60    public synchronized static long getStartOfDayForTime(long time)
61    {
62
63       Calendar JavaDoc cal = GregorianCalendar.getInstance();
64       cal.setTimeInMillis( time );
65       cal.set( Calendar.HOUR_OF_DAY, 0);
66       cal.set( Calendar.MINUTE, 0);
67       cal.set( Calendar.SECOND, 0);
68       cal.set( Calendar.MILLISECOND, 0);
69
70       return cal.getTimeInMillis();
71    }
72
73
74    public static boolean isToday(long t)
75    {
76       long md = getMidnightTodayMorning();
77       if(t - md >=0 && t - md < 24L*3600*1000) return true;
78       return false;
79    }
80
81
82   /** For differences between months until millis.
83   * @param diff in millis
84   */

85   public static final String JavaDoc formatTimeDifference(long diff, boolean notGreaterMultiplierThanHours)
86   {
87     long adiff = Math.abs(diff);
88     if(adiff < 1000L) return "" + diff + " ms";
89     if(adiff < 1000L*60)
90     {
91       double ds = diff/1000.0;
92       return roundInt(ds)+" s";
93     }
94     if(adiff < 1000L*3600)
95     {
96        double dm = diff/1000.0/60.0;
97        if(dm<2)
98        {
99           return format0.format(dm)+" m";
100        }
101        else
102        {
103           return roundInt(dm)+" m";
104        }
105     }
106     if(notGreaterMultiplierThanHours ||adiff < 1000L*3600*24)
107     {
108       return format0.format(diff/1000.0/3600.0)+" h";
109     }
110     if(adiff < 1000L*3600*24*30) // THE L IS MANDATORY !!! (overpass int range !!!!!!!!!!!)
111
{
112        double dd = diff/1000.0/3600.0/24.0;
113        if(dd<2)
114        {
115          return format0.format(dd)+" day";
116        }
117        else
118        {
119          int rid = roundInt(dd);
120          return rid+" day"+(rid!=1?"s":"");
121        }
122     }
123     if(adiff < 1000L*3600*24*30*13)
124     {
125        double md = diff/1000.0/3600.0/24.0/30;
126        if(md<1.5)
127        {
128          return format0.format(md)+" month";
129        }
130        else
131        {
132           int rim = roundInt(md);
133           return rim+" month"+(rim!=1?"s":"");
134        }
135     }
136     double yd = diff/1000.0/3600/24/30/12;
137     if(yd<1.5)
138     {
139        return format0.format(yd)+" year";
140     }
141     else
142     {
143        int riy = roundInt(yd);
144        return ""+riy+" year"+(riy!=1?"s":"");
145     }
146   }
147
148   private static int roundInt(double d)
149   {
150      return (int) Math.round(d);
151   }
152
153    public static int getDaysUntilBirthday(long birth)
154    {
155       return getDaysUntilBirthday(birth, System.currentTimeMillis());
156    }
157
158    /** @return the number of days until birthday if less than 6 months (180 days), positive,
159    * or the negative number of days since last birthday.
160    */

161    public static int getDaysUntilBirthday(long birth, long now)
162    {
163       // now
164
Calendar JavaDoc cn = GregorianCalendar.getInstance();
165       cn.setTime(new Date JavaDoc(now));
166       int dyNow = cn.get(Calendar.DAY_OF_YEAR);
167
168       // the birthday
169
Calendar JavaDoc cb = GregorianCalendar.getInstance();
170       cb.setTime(new Date JavaDoc(birth));
171       // this current year
172
cb.set(Calendar.YEAR, cn.get(Calendar.YEAR));
173       int dyb = cb.get(Calendar.DAY_OF_YEAR);
174
175
176       // first case:
177
int diff = dyb - dyNow;
178       if(diff<-180)
179       {
180          //System.out.println("diff="+diff);
181
// already occured for more than 6 months => look for next
182
int daysThisYear = cn.getActualMaximum(Calendar.DAY_OF_YEAR);
183          //System.out.println("daysThisYear="+daysThisYear);
184
int remainingThisYear = daysThisYear-dyNow;
185          //System.out.println("remainingThisYear="+remainingThisYear);
186
// next year
187
cb.set(Calendar.YEAR, cn.get(Calendar.YEAR)+1);
188          dyb = cb.get(Calendar.DAY_OF_YEAR);
189
190          return remainingThisYear+dyb;
191       }
192       else if(diff>180)
193       {
194          // far away ! => look last year
195
cb.set(Calendar.YEAR, cn.get(Calendar.YEAR)-1);
196          int daysOfLastYear = cb.getActualMaximum(Calendar.DAY_OF_YEAR);
197          int lastYearDaysFromBirth = daysOfLastYear-cb.get(Calendar.DAY_OF_YEAR);
198
199          return dyNow+ lastYearDaysFromBirth;
200
201       }
202       else if(diff<0)
203       {
204          // already occured this year.
205
return diff; // <0
206

207       }
208       else
209       {
210          // not occured this year
211
return diff;
212       }
213    }
214
215    /** @param month 0 for january !
216    * @param day 1 for the first day.
217    */

218    public static Date JavaDoc getDate(int day, int month, int year)
219    {
220       Calendar JavaDoc cn = GregorianCalendar.getInstance();
221       cn.set(year,month,day);
222       return cn.getTime();
223    }
224
225    public static void main(String JavaDoc[] args)
226    {
227       System.out.println("" +
228         getDaysUntilBirthday(
229            getDate(1,8,2007).getTime(), // birth
230
getDate(12,7,2007).getTime() // now
231
));
232
233       System.out.println("" +
234         getDaysUntilBirthday(
235            getDate(12,9,2007).getTime(), // birth
236
getDate(12,0,2007).getTime() // now
237
));
238    }
239
240 } // DateUtils
Popular Tags