Android Open Source - android-NSUWeather Formatter






From Project

Back to project page android-NSUWeather.

License

The source code is released under:

MIT License

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

/*
 * The MIT License (MIT)/*ww  w.  ja  v a  2 s. co  m*/
 *
 * Copyright (c) 2014 Maxim Belsky
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.mbelsky.nsuweather.utils;

import android.content.Context;
import com.mbelsky.nsuweather.R;
import com.mbelsky.nsuweather.preferences.Storage;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @author mbelsky 26.01.14
 */
public class Formatter {

    private static final String MINUS_SIGN = "−";
    private static final String DEGREE_SIGN = "°";

    /**
     * @return temperature value with html-encoded symbols.
     */
    public static String getLastTemperature(final Storage storage) {
        final String lastTemperature = storage.getLastTemperature();
        try {
            final float _temperature = Float.parseFloat(lastTemperature);
            final int temperature = Math.round(_temperature);

            final StringBuilder builder = new StringBuilder();
            if ( 0 > temperature ) {
                builder.append(MINUS_SIGN);
            }
            builder.append(Math.abs(temperature)).append(DEGREE_SIGN);

            return builder.toString();
        } catch (NumberFormatException ignored) {
        }

        return lastTemperature;
    }

    private static final Locale RU_LOCALE = new Locale("ru", "ru");
    private static final DateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm", RU_LOCALE);
    private static final DateFormat DATE_FORMAT = new SimpleDateFormat("d MMM.", RU_LOCALE);

    public static String getLastUpdateTime(final Context context, final Storage storage) {
        final Date lastUpdateTime = storage.getLastUpdateTime();
        if ( Storage.DEFAULT_LAST_UPDATE_TIME_VALUE == lastUpdateTime.getTime() ) {
            return context
                    .getString(R.string.widget_last_update_date_with_suffix, Storage.DEFAULT_VALUE);
        }
        final Calendar currentTimeCalendar = Calendar.getInstance();
        final Calendar lastUpdateTimeCalendar = Calendar.getInstance();
        lastUpdateTimeCalendar.setTime(lastUpdateTime);

        final long lastUpdateDay = resetHoursAndMinutes(lastUpdateTimeCalendar);
        final long currentDay = resetHoursAndMinutes(currentTimeCalendar);

        final DateFormat dateFormat;
        final int strResId;
        if ( currentDay > lastUpdateDay ) {
            currentTimeCalendar.roll(Calendar.HOUR_OF_DAY, -5);
            if ( lastUpdateTimeCalendar.getTimeInMillis() < currentTimeCalendar.getTimeInMillis() ) {
                dateFormat = DATE_FORMAT;
                strResId = R.string.widget_last_update_date;
            } else {
                dateFormat = TIME_FORMAT;
                strResId = R.string.widget_last_update_date_with_suffix;
            }
        } else {
            dateFormat = TIME_FORMAT;
            strResId = R.string.widget_last_update_date_with_suffix;
        }

        final String date = dateFormat.format(lastUpdateTime).toUpperCase();
        return context.getString(strResId, date);
    }

    private static long resetHoursAndMinutes(final Calendar _calendar) {
        final Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(_calendar.getTimeInMillis());

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTimeInMillis();
    }
}




Java Source Code List

com.mbelsky.nsuweather.activity.SettingsActivity.java
com.mbelsky.nsuweather.http.NetworkConstants.java
com.mbelsky.nsuweather.http.parser.WeatherResponseParser.java
com.mbelsky.nsuweather.http.request.WeatherRequest.java
com.mbelsky.nsuweather.model.Weather.java
com.mbelsky.nsuweather.preferences.Storage.java
com.mbelsky.nsuweather.service.UpdateWeatherService.java
com.mbelsky.nsuweather.utils.AppWidgetHelper.java
com.mbelsky.nsuweather.utils.Formatter.java
com.mbelsky.nsuweather.utils.ViewHelper.java
com.mbelsky.nsuweather.view.LastUpdateTimeView.java
com.mbelsky.nsuweather.view.NoConnectionView.java
com.mbelsky.nsuweather.view.TemperatureView.java
com.mbelsky.nsuweather.widgetprovider.WeatherWidgetProvider.java