Created
August 18, 2017 08:48
-
-
Save JonathandelaSen/3a778ad705ea5f815af8c544cfaf5b7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.text.format.DateUtils; | |
import com.example.jonathan.medsblaprueba.R; | |
import com.example.jonathan.medsblaprueba.models.chat.ChatAbstract; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.Locale; | |
/** | |
* Created by jonathan on 27/03/17. | |
*/ | |
public class UtilsDate { | |
private static final int SECONDS_IN_MILIS = 1000; | |
private static final int MINUTES_IN_MILIS = SECONDS_IN_MILIS * 60; | |
private static final java.lang.String DEFAULT_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss"; | |
public static boolean moreDifferenceThanAMinute(String previousMessageDate, String actualMessageDate) { | |
SimpleDateFormat simpleDateFormat = | |
new SimpleDateFormat(DEFAULT_TIME_FORMAT); | |
try { | |
Date actual = simpleDateFormat.parse(actualMessageDate); | |
Date previous = simpleDateFormat.parse(previousMessageDate); | |
long different = actual.getTime() - previous.getTime(); | |
long elapsedMinutes = different / MINUTES_IN_MILIS; | |
return elapsedMinutes > 1; | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
return false; | |
} | |
public static boolean isSameDay(String stringDate1, String stringDate2) { | |
SimpleDateFormat simpleDateFormat = | |
new SimpleDateFormat(DEFAULT_TIME_FORMAT, Locale.getDefault()); | |
try { | |
Date date1 = simpleDateFormat.parse(stringDate1); | |
Calendar calendar1 = Calendar.getInstance(); | |
calendar1.setTime(date1); | |
Date date2 = simpleDateFormat.parse(stringDate2); | |
Calendar calendar2 = Calendar.getInstance(); | |
calendar2.setTime(date2); | |
return calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH) && | |
calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH) && | |
calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR); | |
} catch (ParseException e) { | |
return false; | |
} | |
} | |
/** e.g. 20 April 2017 **/ | |
public static String getFormatDateDayMonthYearLong(String stringDate) { | |
SimpleDateFormat simpleDateFormat = | |
new SimpleDateFormat(DEFAULT_TIME_FORMAT, Locale.getDefault()); | |
try { | |
Date date = simpleDateFormat.parse(stringDate); | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTime(date); | |
return calendar.get(Calendar.DAY_OF_MONTH) + " " + | |
calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault()) + " " + | |
calendar.get(Calendar.YEAR); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
return ""; | |
} | |
/** e.g. 3.16 **/ | |
public static String getFormatDateTime(String stringDate) { | |
SimpleDateFormat simpleDateFormat = | |
new SimpleDateFormat(DEFAULT_TIME_FORMAT, Locale.getDefault()); | |
try { | |
Date date = simpleDateFormat.parse(stringDate); | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTime(date); | |
return getMinuteHour(calendar); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
return ""; | |
} | |
public static String getFormatDateCompleteMessage(String stringDate) { | |
SimpleDateFormat simpleDateFormat = | |
new SimpleDateFormat(DEFAULT_TIME_FORMAT, Locale.getDefault()); | |
try { | |
Date date = simpleDateFormat.parse(stringDate); | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTime(date); | |
if (DateUtils.isToday(date.getTime())){ | |
return getMinuteHour(calendar); | |
} | |
return getDayMonthYear(calendar) + ", " + getMinuteHour(calendar); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
return ""; | |
} | |
/** e.g. 15:27 **/ | |
private static String getMinuteHour(Calendar calendar) { | |
int minute = calendar.get(Calendar.MINUTE); | |
String minuteString = minute < 10 ? "0" + minute : String.valueOf(minute); | |
return calendar.get(Calendar.HOUR_OF_DAY) + "." + minuteString; | |
} | |
public static String getCurrentDate(){ | |
SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_TIME_FORMAT); | |
return sdf.format(new Date()); | |
} | |
/** e.g. 27/04/2017 **/ | |
private static String getDayMonthYear(Calendar calendar) { | |
return calendar.get(Calendar.DAY_OF_MONTH) + "/" + calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.YEAR); | |
} | |
public static boolean isSooner(String date1, String date2) { | |
SimpleDateFormat simpleDateFormat = | |
new SimpleDateFormat(DEFAULT_TIME_FORMAT, Locale.getDefault()); | |
try { | |
Date dateAux1 = simpleDateFormat.parse(date1); | |
Date dateAux2 = simpleDateFormat.parse(date2); | |
return dateAux1.compareTo(dateAux2) <= 0; | |
} catch (ParseException e) { | |
return true; | |
} | |
} | |
public static boolean moreDifferenceThanAMinuteTs(String previousMessageDate, String actualMessageDate) { | |
double different = Long.valueOf(actualMessageDate) - Long.valueOf(previousMessageDate); | |
double elapsedMinutes = different / MINUTES_IN_MILIS; | |
return elapsedMinutes > 1; | |
} | |
public static boolean isSameDayTs(String stringDate1, String stringDate2) { | |
Calendar cal1 = Calendar.getInstance(); | |
cal1.setTimeInMillis(Long.valueOf(stringDate1)); | |
Calendar cal2 = Calendar.getInstance(); | |
cal2.setTimeInMillis(Long.valueOf(stringDate2)); | |
return isSameDay(cal1, cal2); | |
} | |
public static boolean isSameDay(Calendar cal1, Calendar cal2) { | |
return cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH) && | |
cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) && | |
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR); | |
} | |
public static boolean isToday(Calendar cal) { | |
return isSameDay(cal, Calendar.getInstance()); | |
} | |
public static boolean isYesterday(Calendar cal) { | |
return isBeforeDay(cal, Calendar.getInstance()); | |
} | |
public static boolean isBeforeDay(Calendar cal1, Calendar cal2) { | |
if (cal1.get(Calendar.ERA) < cal2.get(Calendar.ERA)) return true; | |
if (cal1.get(Calendar.ERA) > cal2.get(Calendar.ERA)) return false; | |
if (cal1.get(Calendar.YEAR) < cal2.get(Calendar.YEAR)) return true; | |
if (cal1.get(Calendar.YEAR) > cal2.get(Calendar.YEAR)) return false; | |
return cal1.get(Calendar.DAY_OF_YEAR) < cal2.get(Calendar.DAY_OF_YEAR); | |
} | |
/** e.g. 20 April 2017 **/ | |
public static String getFormatDateDayMonthYearLongTs(String stringDate, Context context) { | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTimeInMillis(Long.valueOf(stringDate)); | |
if (isToday(calendar)){ | |
return context.getResources().getString(R.string.today); | |
} | |
if (isYesterday(calendar)){ | |
return context.getResources().getString(R.string.yesterday); | |
} | |
// if (isYesterday(calendar)){ | |
// return context.getResources().getString(R.string.today); | |
// } | |
return calendar.get(Calendar.DAY_OF_MONTH) + " " + | |
calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault()) + " " + | |
calendar.get(Calendar.YEAR); | |
} | |
/** e.g. 3.16 **/ | |
public static String getFormatDateTimeTs(String stringDate) { | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTimeInMillis(Long.valueOf(stringDate)); | |
return getMinuteHour(calendar); | |
} | |
public static String getFormatDateCompleteMessageTs(String stringDate) { | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTimeInMillis(Long.valueOf(stringDate)); | |
if (DateUtils.isToday(Long.valueOf(stringDate))) { | |
return getMinuteHour(calendar); | |
} | |
return getDayMonthYear(calendar) + ", " + getMinuteHour(calendar); | |
} | |
public static boolean isSoonerTs(String date1, String date2) { | |
return Long.valueOf(date1) < Long.valueOf(date2); | |
} | |
public static String getCurrentTimeMillis() { | |
return String.valueOf(System.currentTimeMillis()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment