PageRenderTime 267ms CodeModel.GetById 120ms app.highlight 60ms RepoModel.GetById 81ms app.codeStats 1ms

/src/mpv5/utils/date/DateConverter.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 623 lines | 338 code | 54 blank | 231 comment | 26 complexity | 72041148093d1da43a692ede504588ea MD5 | raw file
  1/*
  2 *  This file is part of YaBS.
  3 *  
  4 *      YaBS is free software: you can redistribute it and/or modify
  5 *      it under the terms of the GNU General Public License as published by
  6 *      the Free Software Foundation, either version 3 of the License, or
  7 *      (at your option) any later version.
  8 *  
  9 *      YaBS is distributed in the hope that it will be useful,
 10 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 *      GNU General Public License for more details.
 13 *  
 14 *      You should have received a copy of the GNU General Public License
 15 *      along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 16 */
 17package mpv5.utils.date;
 18
 19import java.text.DateFormat;
 20import java.text.DateFormatSymbols;
 21import java.text.ParseException;
 22import java.text.SimpleDateFormat;
 23import java.util.ArrayList;
 24import java.util.Arrays;
 25import java.util.Calendar;
 26import java.util.Date;
 27import java.util.List;
 28import java.util.Locale;
 29import java.util.Vector;
 30import mpv5.db.objects.User;
 31import mpv5.handler.FormatHandler;
 32import mpv5.logging.Log;
 33import mpv5.utils.text.TypeConversion;
 34
 35/**
 36 *
 37 *  This class provides date formatting methods and default date formats
 38 */
 39public class DateConverter {
 40
 41    /**
 42     * Gets month strings. For example: "January", "February", etc.
 43     */
 44    public static String[] months = DateFormatSymbols.getInstance().getMonths();
 45    private static Calendar cl = Calendar.getInstance();
 46    /**
 47     * The date formatter with the short formatting style for the default locale.
 48     */
 49    public static DateFormat DEF_DATE_FORMAT = DateFormat.getDateInstance(DateFormat.MEDIUM);
 50    /**
 51     * "EEE, dd MMM yyyy HH:mm:ss z"
 52     */
 53    public static final DateFormat NATIVE_DATE_FORMAT = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
 54    /**
 55     * "dd.MM.yyy - HH:mm:ss"
 56     */
 57    public static final DateFormat DE_FULL_DATE_FORMAT = new SimpleDateFormat("dd.MM.yyy - HH:mm:ss");
 58    /**
 59     * "yyyy-MM-dd HH:mm:ss"
 60     */
 61    public static final DateFormat DB_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 62    /**
 63     * DE format
 64     */
 65    public static final DateFormat DE_DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy", Locale.GERMAN);
 66    /**
 67     * "yyyy-MM-dd"
 68     */
 69    public static final DateFormat ENG_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
 70    /**
 71     * YYYY
 72     */
 73    public static final DateFormat YEAR_DATE_FORMAT = new SimpleDateFormat("yyyy");
 74    /**
 75     * Contains all available default date formats
 76     */
 77    public static final ArrayList<DateFormat> DATE_FORMATS = new ArrayList<DateFormat>(Arrays.asList(new DateFormat[]{
 78                DB_DATE_FORMAT,
 79                ENG_DATE_FORMAT,
 80                DE_DATE_FORMAT,
 81                DE_FULL_DATE_FORMAT,
 82                NATIVE_DATE_FORMAT,
 83                DEF_DATE_FORMAT,
 84                YEAR_DATE_FORMAT
 85            }));
 86
 87    /**
 88     *
 89     * @param date
 90     * @param amount
 91     * @return
 92     */
 93    public static synchronized Date addDays(Date date, Integer amount) {
 94        cl.setTime(date);
 95        cl.add(Calendar.DATE, amount);
 96
 97        return cl.getTime();
 98    }
 99
100    /**
101     *
102     * @param date
103     * @param amount
104     * @return
105     */
106    public static synchronized Date addDynamic(Date date, Integer amount, Integer Typ) {
107        cl.setTime(date);
108        cl.add(Typ, amount);
109
110        return cl.getTime();
111    }
112    /**
113     *
114     * @param date
115     * @param amount
116     * @return
117     */
118    public static synchronized Date addYears(Date date, int amount) {
119        cl.setTime(date);
120        cl.add(Calendar.YEAR, amount);
121
122        return cl.getTime();
123    }
124
125    /**
126     * Get DAYS difference
127     * @param date1
128     * @param date2
129     * @return
130     */
131    public static synchronized Integer getDifferenceBetween(Date date1, Date date2) {
132
133        if (date1.after(date2)) {
134            Date swap = date1;
135            date1 = date2;
136            date2 = swap;
137        }
138
139        Calendar d1 = Calendar.getInstance();
140        d1.setTime(date1);
141
142        Calendar d2 = Calendar.getInstance();
143        d2.setTime(date2);
144
145        int days = d2.get(java.util.Calendar.DAY_OF_YEAR)
146                - d1.get(java.util.Calendar.DAY_OF_YEAR);
147        int y2 = d2.get(java.util.Calendar.YEAR);
148        if (d1.get(java.util.Calendar.YEAR) != y2) {
149            d1 = (java.util.Calendar) d1.clone();
150            do {
151                days += d1.getActualMaximum(java.util.Calendar.DAY_OF_YEAR);
152                d1.add(java.util.Calendar.YEAR, 1);
153            } while (d1.get(java.util.Calendar.YEAR) != y2);
154        }
155        return days;
156
157    }
158
159    /**
160     * Returns the same date , one second before the next day
161     * @param date
162     * @return
163     */
164    public static synchronized Date getEndOfDay(Date date) {
165        Calendar calendar = cl;
166        synchronized (calendar) {
167            calendar.setTime(date);
168            calendar.set(Calendar.HOUR_OF_DAY, 23);
169            calendar.set(Calendar.MILLISECOND, 999);
170            calendar.set(Calendar.SECOND, 59);
171            calendar.set(Calendar.MINUTE, 59);
172            return calendar.getTime();
173        }
174    }
175
176    /**
177     * Returns the same date, first millisecond
178     * @param date
179     * @return
180     */
181    public static synchronized Date getStartOfDay(Date date) {
182        Calendar calendar = cl;
183        synchronized (calendar) {
184            calendar.setTime(date);
185            calendar.set(Calendar.HOUR_OF_DAY, 0);
186            calendar.set(Calendar.MILLISECOND, 1);
187            calendar.set(Calendar.SECOND, 0);
188            calendar.set(Calendar.MINUTE, 0);
189            return calendar.getTime();
190        }
191    }
192
193    /**
194     * Returns the same date, first millisecond of the year
195     * @param date
196     * @return
197     */
198    public static synchronized Date getStartOfYear(Date date) {
199        Calendar calendar = cl;
200        synchronized (calendar) {
201            calendar.setTime(date);
202            calendar.set(Calendar.HOUR_OF_DAY, 0);
203            calendar.set(Calendar.MILLISECOND, 1);
204            calendar.set(Calendar.SECOND, 0);
205            calendar.set(Calendar.MINUTE, 0);
206            calendar.set(Calendar.MONTH, 0);
207            calendar.set(Calendar.DAY_OF_MONTH, 1);
208            return calendar.getTime();
209        }
210    }
211
212    /**
213     * Returns the same date, last millisecond of the year
214     * @param date
215     * @return
216     */
217    public static synchronized Date getEndOfYear(Date date) {
218        Calendar calendar = cl;
219        synchronized (calendar) {
220            calendar.setTime(date);
221            calendar.set(Calendar.HOUR_OF_DAY, 23);
222            calendar.set(Calendar.MILLISECOND, 999);
223            calendar.set(Calendar.SECOND, 59);
224            calendar.set(Calendar.MINUTE, 59);
225            calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
226            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
227            return calendar.getTime();
228        }
229    }
230
231    /**
232     * Returns the same date, first millisecond
233     * @param date
234     * @return
235     */
236    public static synchronized Date getStartOfMonth(Date date) {
237        Calendar calendar = cl;
238        synchronized (calendar) {
239            calendar.setTime(date);
240            calendar.set(Calendar.HOUR_OF_DAY, 0);
241            calendar.set(Calendar.MILLISECOND, 1);
242            calendar.set(Calendar.SECOND, 0);
243            calendar.set(Calendar.MINUTE, 0);
244            calendar.set(Calendar.DAY_OF_MONTH, 1);
245            return calendar.getTime();
246        }
247    }
248
249    /**
250     * Returns the same date, last millisecond of the month
251     * @param date
252     * @return
253     */
254    public static synchronized Date getEndOfMonth(Date date) {
255        Calendar calendar = cl;
256        synchronized (calendar) {
257            calendar.setTime(date);
258            calendar.set(Calendar.HOUR_OF_DAY, 23);
259            calendar.set(Calendar.MILLISECOND, 999);
260            calendar.set(Calendar.SECOND, 59);
261            calendar.set(Calendar.MINUTE, 59);
262            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
263            return calendar.getTime();
264        }
265    }
266
267    /**
268     * Quarter as 1,2,3,4
269     * @return
270     */
271    public static synchronized int getQuarter() {
272        Calendar cal = Calendar.getInstance();
273        cal.setTime(new Date());
274        int month = cal.get(Calendar.MONTH); /* 0 through 11 */
275        int quarter = (month / 3) + 1;
276        return quarter;
277    }
278
279    /**
280     * Quarter of a given date as 1, 2, 3, 4
281     * @param date 
282     * @return Quarter as 1, 2, 3, 4
283     */
284    public static synchronized int getQuarter(Date date) {
285        Calendar cal = Calendar.getInstance();
286        cal.setTime(date);
287        int month = cal.get(Calendar.MONTH); /* 0 through 11 */
288        int quarter = (month / 3) + 1;
289        return quarter;
290    }
291
292    /**
293     * The current date in the default, localized format
294     * @return
295     */
296    public static synchronized String getTodayDefDate() {
297        return DE_DATE_FORMAT.format(new Date());
298    }
299
300    /**
301     * The current date in SQL format
302     * @return
303     */
304    public static synchronized String getTodayDBDate() {
305        return DB_DATE_FORMAT.format(new Date());
306    }
307
308    /**
309     *
310     * @param date
311     * @return
312     */
313    public static synchronized Date addYear(Date date) {
314        return addYears(date, 1);
315    }
316
317    /**
318     *
319     * @param date
320     * @return The next month
321     */
322    public static synchronized Date addMonth(Date date) {
323        return addMonths(date, 1);
324    }
325
326    /**
327     *
328     * @param date
329     * @param amount
330     * @return The next month
331     */
332    public static synchronized Date addMonths(Date date, int amount) {
333        Calendar cal = DateConverter.cl;
334
335        synchronized (cal) {
336            cal.setTime(date);
337            cal.add(Calendar.MONTH, amount);
338
339            return cal.getTime();
340        }
341    }
342
343    /**
344     *
345     * @param date
346     * @return End of the quarter
347     */
348    public static synchronized Date addQuarter(Date date) {
349        Calendar cal = DateConverter.cl;
350
351        synchronized (cal) {
352            cal.setTime(date);
353            cal.add(Calendar.MONTH, 3);
354
355            return cal.getTime();
356        }
357    }
358
359    /**
360     *
361     * @param date
362     * @return The next day
363     */
364    public static synchronized Date addDay(Date date) {
365        return addDays(date, 1);
366    }
367
368    /**
369     *
370     * @param date
371     * @return SQL conform date String
372     */
373    public static synchronized String getSQLDateString(Date date) {
374        return DB_DATE_FORMAT.format(date);
375    }
376
377    /**
378     *
379     * @param date
380     * @return Default date
381     */
382    public static synchronized String getDefDateString(Date date) {
383        return DEF_DATE_FORMAT.format(date);
384    }
385
386    /**
387     *
388     * @param datum
389     * @return
390     */
391    public static synchronized String getDay(Date datum) {
392        return DE_DATE_FORMAT.format(datum);
393    }
394
395    /**
396     *
397     * @param date
398     * @return
399     */
400    public static synchronized String getMonth(Date date) {
401        Calendar cal = Calendar.getInstance();
402        cal.setTime(date);
403        return String.valueOf(cal.get(Calendar.MONTH) + 1);
404    }
405
406    /**
407     *
408     * @param date
409     * @return
410     */
411    public static synchronized String getMonthName(Date date) {
412        Calendar cal = Calendar.getInstance();
413        cal.setTime(date);
414        return months[cal.get(Calendar.MONTH)];
415    }
416
417    /**
418     *
419     * @param date
420     * @return
421     */
422    public static synchronized String getYearName(Date date) {
423        Calendar cal = Calendar.getInstance();
424        cal.setTime(date);
425        return String.valueOf(cal.get(Calendar.YEAR));
426    }
427
428    /**
429     *
430     * @param date
431     * @return
432     */
433    public static synchronized String getDayOfMonth(Date date) {
434        Calendar cal = Calendar.getInstance();
435        cal.setTime(date);
436        return String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
437    }
438
439    /**
440     * Converts formated
441     * Default date (dd.mm.yyyy) or variants
442     * or
443     * yyyy-mm-dd hh.mm.ss[.nnnnnn] - SQL DATE Timestamp
444     *
445     * to a Date object.
446     *
447     * @param date
448     * @return Parsed date
449     */
450    public static synchronized Date getDate(String date) {
451        Date DATE = null;
452        for (DateFormat d : DATE_FORMATS) {
453            try {
454                DATE = d.parse(date);
455                return DATE;
456            } catch (ParseException ex) {
457            }
458        }
459        if (additionalFormats.isEmpty()) {
460            buildFormats();
461        }
462
463        if (DATE == null) {
464            for (DateFormat d : additionalFormats) {
465                try {
466                    DATE = d.parse(date);
467                    return DATE;
468                } catch (ParseException ex) {
469                }
470            }
471        }
472
473        if (DATE == null) {
474            Log.Debug(DateConverter.class, "String not parseable to a date: " + date);
475        }
476        return DATE;
477    }
478    static final List<DateFormat> additionalFormats = new Vector<DateFormat>();
479
480    /**
481     * Try to parse the given object to a date
482     * @param object
483     * @return
484     */
485    public static synchronized Date getDate(Object object) {
486        if (object instanceof Date) {
487            return (Date) object;
488        } else if (object instanceof java.sql.Date) {
489            return new Date(((java.sql.Date) object).getTime());
490        } else {
491            return getDate(object.toString());
492        }
493    }
494
495    /**
496     *
497     * @return
498     */
499    public static synchronized String getYear() {
500        return String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
501    }
502
503    /**
504     *
505     * @return
506     */
507    public static synchronized String getDayOfMonth() {
508        String m = String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
509        if (m.length() == 1) {
510            m = "0" + m;
511        }
512        return m;
513    }
514
515    /**
516     *
517     * @return
518     */
519    public static synchronized String getDayMonthAndYear() {
520        return DateConverter.getDefDateString(new Date());
521    }
522
523    /**
524     *
525     * @return
526     */
527    public static synchronized String getMonth() {
528        String m = String.valueOf(Calendar.getInstance().get(Calendar.MONTH) + 1);
529        if (m.length() == 1) {
530            m = "0" + m;
531        }
532        return m;
533    }
534
535    /**
536     *
537     * @return
538     */
539    public static synchronized String getMonthName() {
540        Calendar cal = Calendar.getInstance();
541        return months[cal.get(Calendar.MONTH)];
542    }
543
544    /**
545     *
546     * @param df
547     */
548    public static synchronized void setDefaultFormat(DateFormat df) {
549        DEF_DATE_FORMAT = df;
550    }
551
552    /**
553     * 
554     * @return
555     */
556    public static synchronized String getDefaultFormatString() {
557        if (DEF_DATE_FORMAT instanceof SimpleDateFormat) {
558            return ((SimpleDateFormat) DEF_DATE_FORMAT).toPattern();
559        } else {
560            return "dd.MM.yyyy";
561        }
562    }
563
564    private static synchronized void buildFormats() {
565        Locale[] locales = DateFormat.getAvailableLocales();
566        for (int i = 0; i < locales.length; i++) {
567            Locale locale = locales[i];
568            additionalFormats.addAll(Arrays.asList(new DateFormat[]{
569                        DateFormat.getDateInstance(DateFormat.SHORT, locale),
570                        DateFormat.getDateInstance(DateFormat.MEDIUM, locale),
571                        DateFormat.getDateInstance(DateFormat.LONG, locale),
572                        DateFormat.getDateInstance(DateFormat.FULL, locale),
573                        DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale),
574                        DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale),
575                        DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM, locale),
576                        DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM, locale),
577                        DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale),
578                        DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, locale),
579                        DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT, locale),
580                        DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, locale),
581                        DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL, locale),
582                        DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL, locale),
583                        DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL, locale),
584                        DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale),
585                        DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL, locale),
586                        DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL, locale),
587                        DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL, locale),
588                        DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale)
589                    }));
590        }
591    }
592
593    /**
594     * Generates a random date, which is not in the future
595     * @return
596     */
597    public static Date getRandomDate() {
598        return new RandomDate(new vTimeframe(new Date(0), new Date()));
599    }
600
601    /**
602     * 
603     * @return 01092010
604     */
605    public static String getDateNumeric() {
606        String year = getYear();
607        String month = getMonth();
608        String day = getDayOfMonth();
609        String dn = year + month + day;
610        if(mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("item.date.locale")==null)
611            mpv5.db.objects.User.getCurrentUser().getProperties().changeProperty("item.date.locale", Locale.getDefault().toString());
612        try {
613            Locale l = TypeConversion.stringToLocale(mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("item.date.locale"));
614            if (l.equals(Locale.GERMAN) || l.equals(Locale.GERMANY)) {
615                dn = day + month + year;
616            }
617        } catch (Exception e) {
618            Log.Debug(e);
619        } finally {
620            return dn;
621        }
622    }
623}