io.github.romankl.bitcoinvalue.util.DateUtility.java Source code

Java tutorial

Introduction

Here is the source code for io.github.romankl.bitcoinvalue.util.DateUtility.java

Source

/************************************************************************************************************
 * Copyright (C) 2014 - 2015  Roman Klauke                                                                  *
 *                                                                                                          *
 *     This program is free software: you can redistribute it and/or modify                                 *
 *     it under the terms of the GNU General Public License as published by                                 *
 *     the Free Software Foundation, either version 3 of the License, or                                    *
 *     (at your option) any later version.                                                                  *
 *                                                                                                          *
 *     This program is distributed in the hope that it will be useful,                                      *
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of                                       *
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                        *
 *     GNU General Public License for more details.                                                         *
 *                                                                                                          *
 *     You should have received a copy of the GNU General Public License                                    *
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.                                *
 ************************************************************************************************************/

package io.github.romankl.bitcoinvalue.util;

import android.content.Context;
import android.support.annotation.Nullable;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class DateUtility {

    public static String getDateRepresentation(final Context context) {
        final DateFormat shortDateFormat = android.text.format.DateFormat
                .getDateFormat(context.getApplicationContext());
        String pattern = null;
        if (shortDateFormat instanceof SimpleDateFormat) {
            pattern = ((SimpleDateFormat) shortDateFormat).toPattern();
        }

        return pattern;
    }

    public static String getTimeRepresentation(final Context context) {
        final DateFormat shortDateFormat = android.text.format.DateFormat
                .getDateFormat(context.getApplicationContext());
        String pattern = null;
        if (shortDateFormat instanceof SimpleDateFormat) {
            pattern = ((SimpleDateFormat) shortDateFormat).toPattern();
        }

        return pattern;
    }

    public static String convertDateRepresentationToIsoDate(final Context context, String entryDate) {
        if (isNullOrEmpty(entryDate))
            return "";

        DateTimeFormatter formatter = DateTimeFormat.forPattern(getDateRepresentation(context));
        return formatter.parseDateTime(entryDate).toString();
    }

    public static String convertIsoDateTimeToLocaleTime(final Context context, String isoDate) {
        return new DateTime(isoDate).toString(getTimeRepresentation(context));
    }

    public static String convertIsoDateToLocaleDate(final Context context, String isoDate) {
        if (isNullOrEmpty(isoDate))
            return "";

        return new DateTime(isoDate).toString(getDateRepresentation(context));
    }

    public static String convertIsoDateTimeToLocaleDateTime(final Context context, String isoDate) {
        if (isNullOrEmpty(isoDate))
            return "";

        return convertIsoDateTimeToLocaleDateTime(context, isoDate, " ");
    }

    public static String convertIsoDateTimeToLocaleDateTime(final Context context, String isoDate,
            String separator) {
        if (isNullOrEmpty(isoDate))
            return "";

        DateTime dateTime = new DateTime(isoDate);
        return new StringBuilder(dateTime.toString(getDateRepresentation(context))).append(separator)
                .append(dateTime.toString(getTimeRepresentation(context))).toString();
    }

    public static String convertDateToShort(String date) {
        if (isNullOrEmpty(date))
            return "";

        DateTime dateTime = new DateTime(date);
        String month;
        String day;

        month = String.valueOf(dateTime.getMonthOfYear());
        day = String.valueOf(dateTime.getDayOfMonth());

        return day + "." + month;
    }

    private static boolean isNullOrEmpty(@Nullable String string) {
        return (string == null) || (string.length() == 0);
    }

}