Android Open Source - journal Date Time Helper






From Project

Back to project page journal.

License

The source code is released under:

MIT License

If you think the Android project journal 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 cochrane343.journal;
//from w  ww. j  a  v  a2 s  . c  o m
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/**
 * The helper class for handling dates and times.
 * It operates on Joda-Time's {@link DateTime}. 
 * 
 * @author cochrane34
 * @since 1.0
 */
public class DateTimeHelper {
    /**
     * The constant {@link DateTime} instance representing the Java epoch of 1970-01-01T00:00:00Z UTC
     */
    private static final DateTime EPOCH = new DateTime(0, DateTimeZone.UTC);   
    
    private static final int MONTHS_IN_ONE_YEAR = 12;

    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormat.forPattern("MMM yy");
    
    /**
     * @return a {@link DateTime} instance representing the current time in UTC
     */
    public static DateTime getNow() {
        return new DateTime(DateTimeZone.UTC);
    }    
    
    /**
     * @return the number of months elapsed since the Java epoch
     */
    public static int getMonthsSinceEpoch() {
        final DateTime now = getNow();
        return (now.year().get() - EPOCH.year().get()) * MONTHS_IN_ONE_YEAR
            + now.monthOfYear().get() - EPOCH.monthOfYear().get();
    }
    
    /**
     * @param monthsSinceEpoch the number of months elapsed since the Java epoch
     * @return a {@link DateTime} instance representing the first timestamp of the month specified by <code>monthsSinceEpoch</code>
     */
    public static DateTime getStartOfMonth(final int monthsSinceEpoch) {
        return getMonth(monthsSinceEpoch).dayOfMonth().withMinimumValue().withTimeAtStartOfDay();
    }    
    
    /**
     * @param monthsSinceEpoch the number of months elapsed since the Java epoch
     * @return a {@link DateTime} instance representing the last timestamp of the month specified by <code>monthsSinceEpoch</code>
     */
    public static DateTime getEndOfMonth(final int monthsSinceEpoch) {
        return getStartOfMonth(monthsSinceEpoch).plusMonths(1).minusMillis(1);
    }
        
    /**
     * @param monthsSinceEpoch the number of months elapsed since the Java epoch
     * @return a print string for the the month specified by <code>monthsSinceEpoch</code>
     */
    public static String printMonth(final int monthsSinceEpoch) {
        final DateTime month = getMonth(monthsSinceEpoch);
        return DATE_FORMATTER.print(month);
    }
    
    /**
     * @param monthsSinceEpoch the number of months elapsed since the Java epoch
     * @return a {@link DateTime} instance representing the month specified by <code>monthsSinceEpoch</code>
     */
    public static DateTime getMonth(final int monthsSinceEpoch) {
        return EPOCH.plusMonths(monthsSinceEpoch);
    }    
}




Java Source Code List

cochrane343.journal.Constants.java
cochrane343.journal.CurrencyHelper.java
cochrane343.journal.DateTimeHelper.java
cochrane343.journal.ExpenseEditListener.java
cochrane343.journal.ExpensesListAdapter.java
cochrane343.journal.MainActivity.java
cochrane343.journal.MonthlyExpensesFragment.java
cochrane343.journal.MonthlyExpensesPagerAdapter.java
cochrane343.journal.SettingsActivity.java
cochrane343.journal.TranslationHelper.java
cochrane343.journal.contentprovider.JournalContentProvider.java
cochrane343.journal.contentprovider.JournalContract.java
cochrane343.journal.contentprovider.JournalDatabaseHelper.java
cochrane343.journal.dialogs.CategorySpinnerAdapter.java
cochrane343.journal.dialogs.ExpenseDialogFragment.java
cochrane343.journal.dialogs.ExpenseDialogListener.java
cochrane343.journal.exceptions.IllegalDisplayModeException.java
cochrane343.journal.exceptions.IllegalLoaderIdException.java
cochrane343.journal.exceptions.IllegalUriException.java
cochrane343.journal.exceptions.MissingFragmentArgumentException.java