I retrieve following values from the media store
MediaStore.Images.Media.DATE_TAKEN
MediaStore.Images.Media.DATE_MODIFIED
And read the dates from the result like following:
int dateTakenColumn = cursor.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN);
int dateModifiedColumn = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED);
String dateToken = cursor.getString(dateTakenColumn);
String dateModified = cursor.getString(dateModifiedColumn);
long lDateToken = dateToken != null ? Long.parseLong(dateToken) : 0;
long lDateModified = dateModified != null ? Long.parseLong(dateModified) : 0;
And can see following behaviour (example values):
lDateToken looks like following: 1450696995000 <= CORRECT
lDateModified looks like following: 1450696995 <= WRONG
It seems like the modification dates are all cut off. I checked the real files last modified date with a file explorer, and the values should be fine, but I always get such short numbers from my media files.
Any ideas on why this happens?
PS: checked this http://developer.android.com/reference/android/provider/MediaStore.Images.ImageColumns.html, but the modified field is not listed there...
DATE_TAKEN is in milliseconds since 1970.
See the docs
DATE_MODIFIED is in seconds since 1970, so just multiply it by 1000 and it'll be fine.
See the docs
Just multiply it by 1000 to get correct date
fun convertLongToDate(time: Long): String =
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
DateTimeFormatter.ofPattern("dd MMMM yyyy").format(
Instant.ofEpochMilli(time*1000)
.atZone(ZoneId.systemDefault())
.toLocalDate())
} else {
SimpleDateFormat("dd MMMM yyyy").format(
Date(time * 1000)
)
}
Related
I have an app on the PlayStore and I am building a feature where the user will not see ads more than a specific number in one day.
I am thinking about comparing the current date and time to the previously saved one but haven't find a proper way to do that.
How can I compare date and time to know if 24 hours have passed or not?
Some posts that I found but not helpful:
medium.com
stackoverflow
stackoverflow
tl;dr
[This Answer uses Java syntax. You’ll have to translate to Kotlin syntax.]
if
(
Duration // Represents elapsed time on the scale of hours-minutes-seconds.
.between( // Calculates elapsed time between two points in time.
Instant.parse( "2021-03-23T15:30:57.013678Z" ) , // Last moment when an ad was show.
Instant.now() // Current moment.
) // Returns a `Duration` object.
.toHours() // Extract total number of whole hours from the `Duration` object.
>= 24L // Test if equals-to or greater-than 24 hours.
)
{ show ad }
java.time
You asked:
… know if 24 hours have passed or not?
Use the modern java.time classes defined in JSR 310. The java.time classes are built into Android 26 and later. Most of the functionality is available in earlier Android using the latest tooling’s “API desugaring“.
Instant adShown = Instant.parse( "2021-03-23T15:30:57.013678Z" ) ;
Instant now = Instant.now() ;
Duration d = Duration.between( adShown , now ) ;
long hoursSinceAdShown = d.toHours() ;
if( hoursSinceAdShown >= 24L ) { … show ad }
Record your next ad-showing as text in standard ISO 8601 format.
String output = Instant.now().toString() ;
2021-03-23T15:30:57.013678Z
Your Question asked for two different things:
Once per day
Every 24 hours
The first involves a calendar, dates, and a time zone. The second does not. I showed you code for the second.
You can use a scheduled executor service to trigger from a background thread the next showing of an ad at a specific moment. Search Stack Overflow to learn more as this has been covered many times already.
Use this code to check the current date, Yesterday or Particulardate. Pass Epoch time to this method
// input format (we get a value as Epoch)
private val inputFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
private val outputFormat = SimpleDateFormat("MMM dd")
// have to pass the time value as Epoch time.
private fun calculateDateMonth(time: String): String {
var returnValue = ""
val dateTime = DateTime((time.toLong()) * 1000L)
val inputTime = inputFormat.parse(dateTime.toString())
val convertDateMonth = outputFormat.format(inputTime!!)
val timeInMilliseconds = outputFormat.parse(convertDateMonth)!!
val mTime: Calendar = Calendar.getInstance()
mTime.setTimeInMillis(timeInMilliseconds.time)
val now = Calendar.getInstance()
returnValue = when {
now[Calendar.DATE] == mTime[Calendar.DATE] // check isToday
now[Calendar.DATE] - mTime[Calendar.DATE] == 1 // check Yesterday
else -> convertDateMonth // Month and Date
}
return returnValue
}
let say i will like to automatically change my textview text at 02:00pm everyday how do I implement this functionality.
val df = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.JAPAN).parse("2:00pm")
val systemDat = Calendar.getInstance(Locale.JAPAN).after(df)
if (systemDat) {
binding.includeTokyoSession.text_one.text = "successful"
} else {
binding.includeTokyoSession.text_one.text = "failure"
}
I suppose you want to change the text of your TextView after a particular time, but it seems that you're not aware of the date when comparing and you have a couple of mistakes in your code.
First, this line of code:
DateFormat.getTimeInstance(DateFormat.SHORT, Locale.JAPAN).parse("2:00pm")
will return a Date instance with this date and time in your local timezone 01-01-1970 02:00:00. However, you need to get a Date instance with today's date and the time 14:00:00.
Second, this line of code:
Calendar.getInstance(Locale.JAPAN).after(df)
this is a wrong usage of the Calendar::after() function, and that's because you can only pass a Calendar object to the function in order to get the right comparison result, otherwise it will always return false.
In your case you're passing a Date object.
Following is the implementation of the Calendar::after() function.
public boolean after(Object when) {
return when instanceof Calendar
&& compareTo((Calendar)when) > 0;
}
If you want to proper compare the current time today with 14:00 (comparing only the time today), here is a modification to your code:
val calendarToCompare = Calendar.getInstance(Locale.JAPAN).apply {
set(Calendar.HOUR_OF_DAY, 14)
set(Calendar.MINUTE, 0)
set(Calendar.SECOND, 0)
set(Calendar.MILLISECOND, 0)
}
val systemDat = Calendar.getInstance().after(calendarToCompare)
if (systemDat) {
textview.text = "successful"
} else {
textview.text = "failure"
}
If you want to perform a lifecycle-aware view update (ex. to set the text of your textview), you can check this gist.
I am using an entity framework to calculate how many days from a date range with the target date range.
Let say I passed in a from date A to date B. In each row of the date I have previous log date and current log date. I want to see how many days was sitting inside between the previous log date and current log date.
My code i was currently written is:
var days = toDate.Subtract(fromDate).TotalDays;
var usageLog = (from usageLogs in context.UsageLogs
where
(usageLogs.PreviousLogDate != null
&& (DbFunctions.TruncateTime(usageLogs.CurrentServerLogDate) >= fromDate.Date && DbFunctions.TruncateTime(usageLogs.PreviousLogDate) <= toDate.Date)) ||
(usageLogs.PreviousLogDate == null
&& ((DbFunctions.TruncateTime(usageLogs.CurrentServerLogDate) >= toDate.Date) && (DbFunctions.TruncateTime(usageLogs.CurrentServerLogDate) >= fromDate.Date)))
select new
{
PackageName = usageLogs.PackageName,
estimateUsageCount = (double)usageLogs.UsageCount
});
var statistics = (from usage in usageLog
group usage
by new
{
usage.PackageName
}
into grp
select new
{
PackageName = grp.Key.PackageName,
UsageCount = grp.Sum(c => c.estimateUsageCount)
});
return statistics.ToList();
}
estimateUsageCount in the select statement is where I want to calculate the number of days sitting in between the previous and current log dates.
I am thinking to write function to do the job but don't know how to do it. I know the calculation is quite complicated. Does anyone give me an idea of how to do it.
You can do it like this:
select new
{
PackageName = usageLogs.PackageName,
estimateUsageCount = (usageLogs.CurrentServerLogs - usageLogs.PreviousServerLogs).Days
});
For the range between two dates, I found this excel formula:
=MAX(0,NETWORKDAYS(MAX(Date1, Date2),MIN(dateA,dateA)))
that it can be translated into c# like this, using the ternary operator:
0 > ((date1 > date2 ? date1 : date2) - (dateA < dateB ? dateA : dateB)).Days ? 0 : ((date1 > date2 ? date1 : date2) - (dateA < dateB ? dateA : dateB)).Days
i runing this code on android after load a cursor with the query i pass to the adapter, but my date is in long in milliseconds format so i need to format properly before load the adapter!
problem is this code is taking 14 seconds to pass a 50 items load, the problem get worst if i call it inside the adapter getView cause get slow when i scrool, if i take this function out the program runs smoothly
this is call inside my listfragment
private String dateFormatPatternEdited(long timeMS) {
android.text.format.DateFormat df = new android.text.format.DateFormat();
final Calendar eDate = Calendar.getInstance();
Calendar sDate = Calendar.getInstance();
sDate.setTimeInMillis(timeMS);
long daysBetween = 0;
while (sDate.before(eDate)) {
sDate.add(Calendar.DAY_OF_MONTH, 1);
daysBetween++;
}
String mDateFormatPattern = FuelTrackerApplication.dateFormat.format(timeMS);
if (daysBetween < 2){
mDateFormatPattern = FuelTrackerApplication.timeFormat.format(timeMS);
} else if(daysBetween < 365){
mDateFormatPattern = df.format(FuelTrackerApplication.dateFormatPattern,timeMS).toString();
}
return mDateFormatPattern;
}
and this is were i initialize the date formats i gonna use its called inside onCreate in FuelTrackerApplication i dont think theres nothing wrong with this
public void initializeDateFormat() {
android.text.format.DateFormat df = new android.text.format.DateFormat();
dateFormatPattern = "MMM dd";
if (android.os.Build.VERSION.SDK_INT >= 18){
dateFormatPattern = df.getBestDateTimePattern(Locale.getDefault(), dateFormatPattern);
}
dateFormat = df.getMediumDateFormat(getApplicationContext());
timeFormat = df.getTimeFormat(getApplicationContext());
dateFormat2 = df.getLongDateFormat(getApplicationContext());
}
Ok just a few things. Depending on how long ago your dates are going back. You are only interested if the days between go more then 365. So if your dates are going back for years, you're doing extra work.
while (sDate.before(eDate) && daysBetween <= 365) {
sDate.add(Calendar.DAY_OF_MONTH, 1);
daysBetween++;
}
Will let it break, it means if you have 20 entries going back 5 years, you don't do so much work.
It might be worth while to possibly just check the milliseconds difference. I'm not sure if this is precise enough, but it should work. It means you don't need to loop everything E.g
long millisecondsToday = getMilliseconds;
long timeMs = // you already have this
long millisecondsDifference = millisecondsToday - timeMs;
if (millisecondsDifference < MILLISECONDS_TWO_DAYS) // set a final variable out of this method
// etc
If might also be worth while initialising some of your variables once outside of the method. Like your df, that is being created 50 times, and then just having something set on it. Same with your eDate.
i got this incredible faster and practicaly remove the hole function
instead goes like this
lDateBetween = NOW - timeMS;
if (lDateBetween < DAY)
return FuelTrackerApplication.timeFormat.format(timeMS);
else if (lDateBetween < YEAR)
return df.format(FuelTrackerApplication.dateFormatPattern,timeMS).toString();
else return FuelTrackerApplication.dateFormat.format(timeMS);
all calculation is made using milliseconds i also put 2 final NOW and YEAR, also df and lDateBetween
i think is the fastest i can get!
I have a datepicker, and I pick up two dates, like 2012-04-08 and 2012-05-11. Because I have a database and need store dates as strings I convert them to 20120408 and 20120511 (strings so far). My code contains the next steps. I call my function with these strings:
public void durva(String datefrom, String dateto) throws ParseException {
datefrom = GlobalVars.getDateStringFrom();
dateto = GlobalVars.getDateStringTo();
Log.i("DateFrom", datefrom);
Log.i("Dateto", dateto);
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyyMMdd"); //-de most yyMMdd
formatter2.setLenient(false);
Date dates1;
Date dates2;
long mili1;
long mili2;
long diff;
String dates="";
String convertedDates = "";
dates1 = formatter2.parse(datefrom);
mili1 = dates1.getTime();
Log.i("date1", String.valueOf(mili1));
dates2 = formatter2.parse(dateto);
mili2 = dates2.getTime();
Log.i("date2", String.valueOf(mili2));
diff = 1+((mili2-mili1)/86400000);
Log.i("diff", String.valueOf(diff));
long [] millis = new long[(int) diff];
for (int i=0;i<diff;i++)
{
millis[i] = mili1+(86400000*i);
Log.i("millii", String.valueOf(millis[i]));
dates = dates + String.valueOf(millis[i]) + " ";
SimpleDateFormat formatterX = new SimpleDateFormat("yyyyMMdd");
String dateString = formatterX.format(new Date(millis[i]));
convertedDates = convertedDates + dateString + " ";
}
Log.i("DATES", convertedDates);
}
I use the a created GlobalVars when I pick a date and covert them to this string format I mentioned above. Then I convert them to millisecs. Then I convert them back to my format but it is not important, since the millisecs are already messed up. With
for (int i=0;i<diff;i++)
{
millis[i] = mili1+(86400000*i);
Log.i("millii", String.valueOf(millis[i]));
I always increase the millisecs, but what happens after the 25th value? It travels back in time and continues from another value! In this example I get: 20120408 20120409 .. 20120502 20120314..20120322 . I add 86400000 (millisecs per day) for jumping a whole day.
What's happening here?
Thank you in advance!
You should use 86400000L, or declare i as long:
millis[i] = mili1+(86400000L*i);
Otherwise both i and 86400000 are 32 bit integers, so the result is calculated as a 32-bit integer. Unfortunately 86400000*25 is too big to fit in 32 bits, so it wraps around.
86400000*25 is -2134967296.
Another thing you should be careful about is that not all days have 24 hours thanks to DST.