I have some documents and its created time is in milliseconds. I need to separate them as Today, Yesterday, Last 7 Days, Last 30 Days, More than 30 Days.
I used the following code:convertSimpleDayFormat(1347022979786);
public static String convertSimpleDayFormat(Long val) {
long displayTime = System.currentTimeMillis() - val;
displayTime = displayTime/86400000;
String displayTimeVal = "";
if(displayTime <1)
{
displayTimeVal = "today";
}
else if(displayTime<2)
{
displayTimeVal = "yesterday";
}
else if(displayTime<7)
{
displayTimeVal = "last7days";
}
else if(displayTime<30)
{
displayTimeVal = "last30days";
}
else
{
displayTimeVal = "morethan30days";
}
return displayTimeVal;
}
I am subtracting the current time and passing the milliseconds and converting to one day.
But the issue I'm facing is, I couldn't calculate the exact time for the date in milliseconds.
I want to calculate for Today as: From Midnight 00:00 to Midnight 24:00. (Exactly for 24 hours.)
Similarly I want to exactly convert the Milliseconds into Today, Yesterday, Last 7 days, Last 30 Days and More than 30 Days.
private static Calendar clearTimes(Calendar c) {
c.set(Calendar.HOUR_OF_DAY,0);
c.set(Calendar.MINUTE,0);
c.set(Calendar.SECOND,0);
c.set(Calendar.MILLISECOND,0);
return c;
}
public static String convertSimpleDayFormat(long val) {
Calendar today=Calendar.getInstance();
today=clearTimes(today);
Calendar yesterday=Calendar.getInstance();
yesterday.add(Calendar.DAY_OF_YEAR,-1);
yesterday=clearTimes(yesterday);
Calendar last7days=Calendar.getInstance();
last7days.add(Calendar.DAY_OF_YEAR,-7);
last7days=clearTimes(last7days);
Calendar last30days=Calendar.getInstance();
last30days.add(Calendar.DAY_OF_YEAR,-30);
last30days=clearTimes(last30days);
if(val >today.getTimeInMillis())
{
return "today";
}
else if(val>yesterday.getTimeInMillis())
{
return "yesterday";
}
else if(val>last7days.getTimeInMillis())
{
return "last7days";
}
else if(val>last30days.getTimeInMillis())
{
return "last30days";
}
else
{
return "morethan30days";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With