Android Open Source - adme Date Helper






From Project

Back to project page adme.

License

The source code is released under:

Apache License

If you think the Android project adme 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

/**
 */*  w ww  . j  a v  a  2  s. co m*/
 */
package com.danielesegato.adme.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Help with dates. Careful with {@link Date} methods, the date should be in UTC.
 */
public final class DateHelper {

    private static final ThreadLocal<DateFormat> ISO8601_UTC_FORMATTER = new ThreadLocal<DateFormat>();

    /**
     * The ISO8601 format specification (from w3c) without the timezone part.
     */
    private static final String ISO8601_CUSTOM_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";

    /**
     * Create a {@link SimpleDateFormat} per thread (not thread safe).
     *
     * @return create and store in the local thread an instance of {@link SimpleDateFormat}.
     */
    private static final DateFormat getIso8601UTCDateFormat() {
        DateFormat iso8601UTCDateFormat = ISO8601_UTC_FORMATTER.get();
        if (iso8601UTCDateFormat == null) {
            iso8601UTCDateFormat = new SimpleDateFormat(ISO8601_CUSTOM_FORMAT, Locale.UK);
            iso8601UTCDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
            ISO8601_UTC_FORMATTER.set(iso8601UTCDateFormat);
        }
        return iso8601UTCDateFormat;
    }

    /**
     * Parse the date time string and return a {@link Date} object. If the parsing fail an exception is raised, if you
     * don't want to handle it see {@link #parseUTCDateTime(String, Date)} instead. The string is expected in the
     * ISO8601 CUSTOM format, see {@link #ISO8601_CUSTOM_FORMAT} for details.
     *
     * @param iso8601_datetime the string in the ISO8601 CUSTOM format
     * @return the {@link Date} object parsed
     * @throws ParseException if the iso8601_datetime is not in a valid format
     * @see #ISO8601_CUSTOM_FORMAT
     */
    public static final Date parseUTCDateTime(String iso8601_datetime) throws ParseException {
        return getIso8601UTCDateFormat().parse(iso8601_datetime);
    }

    /**
     * Parse the date time string and return a {@link Date} object. If the parsing fail or the iso8601_datetime is null
     * the _default value is returned. The string is expected in the ISO8601 CUSTOM format, see
     * {@link #ISO8601_CUSTOM_FORMAT} for details.
     *
     * @param iso8601_datetime the string in the ISO8601 CUSTOM format
     * @return the {@link Date} object parsed or _default in case of failure
     * @see #ISO8601_CUSTOM_FORMAT
     */
    public static final Date parseUTCDateTime(String iso8601_datetime, Date _default) {
        try {
            return iso8601_datetime != null ? parseUTCDateTime(iso8601_datetime) : _default;
        } catch (ParseException e) {
            return _default;
        }
    }

    /**
     * Format the datetime {@link Date} object into a Custom ISO8601 format string.
     *
     * @param datetime the {@link Date} object
     * @return the formatted string
     * @see #ISO8601_CUSTOM_FORMAT
     */
    public static final String formatUTCDateTime(Date datetime) {
        return getIso8601UTCDateFormat().format(datetime);
    }

}




Java Source Code List

com.danielesegato.adme.ADME.java
com.danielesegato.adme.annotation.ADMEEntity.java
com.danielesegato.adme.annotation.ADMEField.java
com.danielesegato.adme.annotation.ADMEIndexConstraint.java
com.danielesegato.adme.config.ADMEConfigUtils.java
com.danielesegato.adme.config.ADMEEntityConfig.java
com.danielesegato.adme.config.ADMEFieldConfig.java
com.danielesegato.adme.config.ADMEIndexConstraintConfig.java
com.danielesegato.adme.config.OnForeignUpdateDelete.java
com.danielesegato.adme.config.SQLiteType.java
com.danielesegato.adme.db.ADMESerializerMapping.java
com.danielesegato.adme.db.ADMESerializer.java
com.danielesegato.adme.db.ContentProviderUris.java
com.danielesegato.adme.db.SQLiteContentProvider.java
com.danielesegato.adme.db.serializer.BaseADMESerializer.java
com.danielesegato.adme.db.serializer.BigDecimalADMESerializer.java
com.danielesegato.adme.db.serializer.BooleanADMESerializer.java
com.danielesegato.adme.db.serializer.BooleanObjectADMESerializer.java
com.danielesegato.adme.db.serializer.CurrencyADMESerializer.java
com.danielesegato.adme.db.serializer.DateAsStringADMESerializer.java
com.danielesegato.adme.db.serializer.DateAsTimestampADMESerializer.java
com.danielesegato.adme.db.serializer.DoubleADMESerializer.java
com.danielesegato.adme.db.serializer.DoubleObjectADMESerializer.java
com.danielesegato.adme.db.serializer.EnumIntADMESerializer.java
com.danielesegato.adme.db.serializer.EnumStringADMESerializer.java
com.danielesegato.adme.db.serializer.IntADMESerializer.java
com.danielesegato.adme.db.serializer.IntObjectADMESerializer.java
com.danielesegato.adme.db.serializer.LongADMESerializer.java
com.danielesegato.adme.db.serializer.LongObjectADMESerializer.java
com.danielesegato.adme.db.serializer.StringADMESerializer.java
com.danielesegato.adme.provider.ADMEContentProviderComponent.java
com.danielesegato.adme.provider.ADMEContentProvider.java
com.danielesegato.adme.utils.DateHelper.java
com.danielesegato.adme.utils.SQLStringHelper.java
com.danielesegato.adme.utils.SQLiteScriptParser.java
com.danielesegato.demo.adme.ADMEDemoMainActivity.java
com.danielesegato.demo.adme.NavigationDrawerFragment.java