Android Open Source - rex-weather Day Formatter






From Project

Back to project page rex-weather.

License

The source code is released under:

Copyright (c) 2014, Vy-Shane Sin Fat All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met...

If you think the Android project rex-weather 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 mu.node.rexweather.app.Helpers;
/* w  w  w . ja  v  a2 s .co m*/
import android.content.Context;

import java.text.SimpleDateFormat;
import java.util.Date;

import mu.node.rexweather.app.R;

/**
 * Quick and dirty day formatter helper class.
 */
public class DayFormatter {
    final static long MILLISECONDS_IN_SECONDS = 1000;
    final Context mContext;

    public DayFormatter(Context context) {
        mContext = context;
    }

    /**
     * Format a Unix timestamp into a human readable week day String such as "Today", "Tomorrow"
     * and "Wednesday"
     */
    public String format(final long unixTimestamp) {
        final long milliseconds = unixTimestamp * MILLISECONDS_IN_SECONDS;
        String day;

        if (isToday(milliseconds)) {
            day = mContext.getResources().getString(R.string.today);
        } else if (isTomorrow(milliseconds)) {
            day = mContext.getResources().getString(R.string.tomorrow);
        } else {
            day = getDayOfWeek(milliseconds);
        }

        return day;
    }

    private String getDayOfWeek(final long milliseconds) {
        return new SimpleDateFormat("EEEE").format(new Date(milliseconds));
    }

    private boolean isToday(final long milliseconds) {
        final SimpleDateFormat dayInYearFormat = new SimpleDateFormat("yyyyD");
        final String nowHash = dayInYearFormat.format(new Date());
        final String comparisonHash = dayInYearFormat.format(new Date(milliseconds));
        return nowHash.equals(comparisonHash);
    }

    private boolean isTomorrow(final long milliseconds) {
        final SimpleDateFormat dayInYearFormat = new SimpleDateFormat("yyyyD");
        final int tomorrowHash = Integer.parseInt(dayInYearFormat.format(new Date())) + 1;
        final int comparisonHash = Integer.parseInt(dayInYearFormat.format(new Date(milliseconds)));
        return comparisonHash == tomorrowHash;
    }
}




Java Source Code List

mu.node.rexweather.app.WeatherActivity.java
mu.node.rexweather.app.Helpers.DayFormatter.java
mu.node.rexweather.app.Helpers.TemperatureFormatter.java
mu.node.rexweather.app.Models.CurrentWeather.java
mu.node.rexweather.app.Models.WeatherForecast.java
mu.node.rexweather.app.Services.LocationService.java
mu.node.rexweather.app.Services.WeatherService.java