/*
Swisscom Safe Connect
Copyright (C) 2015 Swisscom
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 com.swisscom.safeconnect.utils;
import android.content.Context;
import android.text.format.DateUtils;
import com.swisscom.safeconnect.R;
import java.text.DateFormat;
import java.util.Date;
/**
* Created by vadim on 6/15/15.
*/
public class DateProvider {
public static String getFormattedDate(Context context, long timestamp){
if (timestamp == 0) {
return "-";
}
DateFormat df = android.text.format.DateFormat.getDateFormat(context);
DateFormat tf = android.text.format.DateFormat.getTimeFormat(context);
Date date = new Date(timestamp * 1000);
Date now = new Date();
//this hour -> xx mins ago
if (date.getTime() >= (now.getTime() - (60 * 60 * 1000))) {
int minutes = (int)((now.getTime() - date.getTime()) / 1000 / 60);
return (minutes != 1 ? String.format(context.getString(R.string.pref_summ_minutes_ago), minutes)
: String.format(context.getString(R.string.pref_summ_1_minute_ago), minutes));
}
//today --> today, 17:03
if (DateUtils.isToday(date.getTime())) {
return String.format(context.getString(R.string.pref_summ_today), tf.format(date));
}
//day, time
return String.format(context.getString(R.string.pref_summ_date), df.format(date), tf.format(date));
}
}