Android Open Source - baracus-framework Date Util






From Project

Back to project page baracus-framework.

License

The source code is released under:

Apache License

If you think the Android project baracus-framework listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.mantucon.baracus.util;
// w  w w.j  a v a 2s.  co m
import android.text.format.DateFormat;
import net.mantucon.baracus.context.BaracusApplicationContext;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created with IntelliJ IDEA.
 * User: marcus
 * Date: 17.10.12
 * Time: 06:02
 * To change this template use File | Settings | File Templates.
 */
public class DateUtil {

    private static volatile GregorianCalendar gregorianCalendar = new GregorianCalendar();

    protected DateUtil() {
        // Protection Constructor
    }

    public static class DateFormatException extends RuntimeException {
        public DateFormatException(Throwable throwable) {
            super(throwable);
        }
    }

    /**
     * reverts a date string back into a date usign the system's date format
     *
     * @param date - the date string
     * @return the parsed date
     */
    public static Date toDate(String date) {
        try {
            return DateFormat.getDateFormat(BaracusApplicationContext.getContext()).parse(date);
        } catch (ParseException e) {
            throw new DateFormatException(e);
        }
    }

    public static class DateComparator implements Comparator<Date> {
        @Override
        public int compare(Date date, Date date1) {
            if (date1 == null && date == null) {
                return 0;
            }

            if (date1 == null) {
                return 1;
            }

            if (date == null) {
                return -1;
            }

            return java.lang.Long.valueOf(date.getTime()).compareTo(Long.valueOf(date1.getTime()));
        }
    }

    public static String toEuropeanDate(Date date) {
        return date != null ? new SimpleDateFormat("dd.MM.yyyy").format(date) : "";
    }

    public static Date fromEuropeanDate(String date) {
        try {
            return date != null ? new SimpleDateFormat("dd.MM.yyyy").parse(date) : null;
        } catch (ParseException e) {
            throw new DateFormatException(e);
        }
    }

    public static String toReverseDate(Date date) {
        return date != null ? new SimpleDateFormat("yyyyMMdd_hhmmss").format(date) : "";
    }


    /**
     * @return today's date
     */
    public static Date today() {
        return fromEuropeanDate(toEuropeanDate(new Date()));
    }

    public static DayDate addDay(Date d) {
        gregorianCalendar.setTime(d);
        gregorianCalendar.add(Calendar.DAY_OF_MONTH, 1);
        return new DayDate(gregorianCalendar.getTime());

    }

    public static DayDate subtractDay(Date d) {
        gregorianCalendar.setTime(d);
        gregorianCalendar.add(Calendar.DAY_OF_MONTH, -1);
        return new DayDate(gregorianCalendar.getTime());

    }

    /*
    public static DayDate subtractSecond(Date d) {
        gregorianCalendar.setTime(d);
        gregorianCalendar.add(Calendar.SECOND,-1);
        return gregorianCalendar.getTime();

    } */

    public static DayDate addWeek(Date d) {
        gregorianCalendar.setTime(d);
        gregorianCalendar.add(Calendar.DAY_OF_MONTH, 7);
        return new DayDate(gregorianCalendar.getTime());
    }

    public static DayDate addMonth(Date d) {
        gregorianCalendar.setTime(d);
        gregorianCalendar.add(Calendar.MONTH, 1);
        return new DayDate(gregorianCalendar.getTime());
    }

    public static DayDate addMonths(Date d, int months) {
        gregorianCalendar.setTime(d);
        gregorianCalendar.add(Calendar.MONTH, months);
        return new DayDate(gregorianCalendar.getTime());
    }

    public static DayDate addTwoMonths(Date d) {
        gregorianCalendar.setTime(d);
        gregorianCalendar.add(Calendar.MONTH, 2);
        return new DayDate(gregorianCalendar.getTime());
    }

    public static DayDate addQuarter(Date d) {
        gregorianCalendar.setTime(d);
        gregorianCalendar.add(Calendar.MONTH, 3);
        return new DayDate(gregorianCalendar.getTime());
    }

    public static DayDate addHalfYear(Date d) {
        gregorianCalendar.setTime(d);
        gregorianCalendar.add(Calendar.MONTH, 6);
        return new DayDate(gregorianCalendar.getTime());
    }

    public static DayDate addYear(Date d) {
        gregorianCalendar.setTime(d);
        gregorianCalendar.add(Calendar.MONTH, 12);
        return new DayDate(gregorianCalendar.getTime());
    }

    public static Date roundToDay(Date d) {
        return fromEuropeanDate(toEuropeanDate(d));
    }


    public static DayDate addOneYear(Date today) {
        return new DayDate(net.mantucon.baracus.util.DateUtil.addOneYear(today));
    }

    public static DayDate endOfTime() {
        return new DayDate(fromEuropeanDate("31.12.9999"));
    }

    public static DayDate beginOfTime() {
        return new DayDate(fromEuropeanDate("01.01.01"));
    }
}




Java Source Code List

net.mantucon.baracus.annotations.Bean.java
net.mantucon.baracus.context.AnnotationScanner.java
net.mantucon.baracus.context.BaracusApplicationContext.java
net.mantucon.baracus.context.BeanContainer.java
net.mantucon.baracus.context.Exceptions.java
net.mantucon.baracus.context.ManagedActivity.java
net.mantucon.baracus.context.ManagedFragment.java
net.mantucon.baracus.dao.BaracusOpenHelper.java
net.mantucon.baracus.dao.BaseDao.java
net.mantucon.baracus.dao.ConfigurationDao.java
net.mantucon.baracus.errorhandling.CustomErrorHandler.java
net.mantucon.baracus.errorhandling.ErrorHandler.java
net.mantucon.baracus.errorhandling.ErrorHandlingFactory.java
net.mantucon.baracus.errorhandling.ErrorSeverity.java
net.mantucon.baracus.errorhandling.StandardErrorHandler.java
net.mantucon.baracus.errorhandling.TextEditErrorHandler.java
net.mantucon.baracus.lifecycle.ApplicationContextInitializer.java
net.mantucon.baracus.lifecycle.Destroyable.java
net.mantucon.baracus.lifecycle.Initializeable.java
net.mantucon.baracus.migr8.MigrationStep.java
net.mantucon.baracus.migr8.ModelVersion000.java
net.mantucon.baracus.model.ConfigurationParameter.java
net.mantucon.baracus.orm.AbstractModelBase.java
net.mantucon.baracus.orm.FieldList.java
net.mantucon.baracus.orm.Field.java
net.mantucon.baracus.orm.Identifiable.java
net.mantucon.baracus.orm.LazyCollection.java
net.mantucon.baracus.orm.LazyMorphicCollection.java
net.mantucon.baracus.orm.LazyReference.java
net.mantucon.baracus.orm.LegacyModelBase.java
net.mantucon.baracus.orm.ModelBase.java
net.mantucon.baracus.orm.NullReference.java
net.mantucon.baracus.orm.ObjectReference.java
net.mantucon.baracus.orm.OptimisticLockingModelBase.java
net.mantucon.baracus.orm.OptmisticLocking.java
net.mantucon.baracus.orm.ReferenceLoader.java
net.mantucon.baracus.orm.Reference.java
net.mantucon.baracus.orm.Timestamped.java
net.mantucon.baracus.signalling.ConfigurationChangeListener.java
net.mantucon.baracus.signalling.DataChangeAwareComponent.java
net.mantucon.baracus.signalling.DataSetChangeAwareComponent.java
net.mantucon.baracus.signalling.DeleteAwareComponent.java
net.mantucon.baracus.signalling.GenericEventAwareComponent.java
net.mantucon.baracus.signalling.GenericEvent.java
net.mantucon.baracus.ui.ConstrainedEditText.java
net.mantucon.baracus.ui.ConstrainedSpinner.java
net.mantucon.baracus.ui.ErrorView.java
net.mantucon.baracus.ui.Popup.java
net.mantucon.baracus.util.DBBackup.java
net.mantucon.baracus.util.DataUtil.java
net.mantucon.baracus.util.DateUtil.java
net.mantucon.baracus.util.DayDate.java
net.mantucon.baracus.util.Logger.java
net.mantucon.baracus.util.StringUtil.java
net.mantucon.baracus.validation.AbstractValidator.java
net.mantucon.baracus.validation.ConstrainedView.java
net.mantucon.baracus.validation.ValidatableView.java
net.mantucon.baracus.validation.ValidationFactory.java
net.mantucon.baracus.validation.Validator.java
net.mantucon.baracus.validation.builtins.DateFromNow.java
net.mantucon.baracus.validation.builtins.NumberMustBeGreaterThanZero.java
net.mantucon.baracus.validation.builtins.StringIsNumericDouble.java
net.mantucon.baracus.validation.builtins.StringIsNumericInteger.java
net.mantucon.baracus.validation.builtins.StringNotEmpty.java