how to get near time for the present time [Android Studio]

how to get near time for the present time [Android Studio]



i have 5 times
example:



4:21 AM



12:1 PM



3:32 PM



6:30 PM



8:4 PM



and the current time is



10:4 AM



I want to do a comparison
What is the next time closest to the current time



the result will be:



NEXT TIME : 12:1 PM






12:1 PM?? Did you mean 12:10 PM or 12:01 PM?

– Andreas
Sep 12 '18 at 17:19



12:1 PM


12:10 PM


12:01 PM






i mean 12:01 PM

– Black.JAC
Sep 12 '18 at 17:30




3 Answers
3



You can turn a time to a date object and then into a long (milliseconds since Jan 1, 1970), and calculate the difference in milliseconds


long diffInMs = currentDate.getTime() - anotherDate.getTime();



And then check which has the smallest difference, but being also equal to or greater than zero. Negative difference means old date and you want the next closest date



To convert the times to dates check this: https://stackoverflow.com/a/8826392/2597775



Basically, it says:


SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

String inputString = "00:01:30.500";

Date date = sdf.parse("1970-01-01 " + inputString);






no bro you don't understand me.. wait me i will share you photo

– Black.JAC
Sep 12 '18 at 17:29







both of the answers exactly answered your question, convert to epoch time, subtract and compare them. They were really nice to give you the code or even the method instead of pseudo code. Your question showed that you haven't done anything toward the problem yet.

– Toan Le
Sep 12 '18 at 21:22




Update: Added logic to rollover at midnight, and added alternative using binary search.



First parse the inputs to a time in milliseconds of the day, by parsing the time string as if it's in UTC time zone.



Then find the smallest value on or after the "current" value, or just smallest value if no value is on or after the "current" value (rollover).



Example:


public static String findNext(String current, String... times) throws ParseException
SimpleDateFormat fmt = new SimpleDateFormat("hh:mm a");
fmt.setTimeZone(TimeZone.getTimeZone("UTC"));
long currentMillis = fmt.parse(current).getTime();
long bestMillis = 0, minMillis = 0;
String bestTime = null, minTime = null;
for (String time : times) millis < bestMillis))
bestMillis = millis;
bestTime = time;

if (minTime == null
return (bestTime != null ? bestTime : minTime);



Test


System.out.println(findNext("10:4 AM",
"4:21 AM", "12:1 PM", "3:32 PM", "6:30 PM", "8:4 PM"));
System.out.println(findNext("10:4 PM",
"4:21 AM", "12:1 PM", "3:32 PM", "6:30 PM", "8:4 PM"));



Output


12:1 PM
4:21 AM



If the given times are guaranteed to already be sorted, then it can be done with a binary search:


public static String findNext(String current, String... times)
SimpleDateFormat fmt = new SimpleDateFormat("hh:mm a");
fmt.setTimeZone(TimeZone.getTimeZone("UTC"));
int idx = Arrays.binarySearch(times, current, new Comparator<String>()
@Override
public int compare(String s1, String s2)
try
return fmt.parse(s1).compareTo(fmt.parse(s2));
catch (ParseException e)
throw new RuntimeException(e);


);
if (idx < 0)
idx = -idx - 1;
return times[idx < times.length ? idx : 0];






Calling findNext with 10:4 PM will return null. Maybe it should wrap around and return 4:21 AM, not sure. If so, adding if(bestTime == null) return times[0]; at the end of the method would work, assuming the reference times are sorted.

– SirRaffleBuffle
Sep 12 '18 at 17:49


findNext


10:4 PM


null


4:21 AM


if(bestTime == null) return times[0];






@SirRaffleBuffle But if times values are already sorted, then a binary search would be much better. No such assumption can be made, since question didn't specifically say so. Just because sample data is sorted, doesn't mean actual data will be.

– Andreas
Sep 12 '18 at 18:01



times






@SirRaffleBuffle it's true!! i change the time of phone to 9:4 PM and the function return NULL !!!!!!!! why ?? it was supposed to show me then next time ( 4:21 AM )

– Black.JAC
Sep 13 '18 at 5:33






this code for VB.NET and it work 100% can you convert it to JAVA android

– Black.JAC
Sep 13 '18 at 6:08






link

– Black.JAC
Sep 13 '18 at 6:09



i specially design a function to solve your problem , use function as you needed.



it will works for you surely.


/**
*
* @author Niravdas
*/
public class TimeDiff
String times = "04:21 AM", "12:01 PM", "03:32 PM", "06:30 PM", "08:04 PM";
String findingafter="10:04 AM";

public TimeDiff()

int time=nextTimeArrayIndex(findingafter,times);
if(time>=0)
System.out.println("NEXT TIME: "+times[time]);


public static void main(String argv)

new TimeDiff();

int nextTimeArrayIndex(String Finding,String fromArray)

int shortest=-1,shortestsec=-1;
long minsecdif=(24*60*60+1),minsec=(24*60*60+1);
int hr=Integer.parseInt(Finding.substring(0, 2));
int min=Integer.parseInt(Finding.substring(3, 5));
long seconds = convertToSec(hr, min, 0, Finding.substring(Finding.length()-2));
System.out.println("seconds :" + seconds);
for(int i=0;i<fromArray.length;i++)

int temphr=Integer.parseInt(fromArray[i].substring(0, 2));
int tempmin = Integer.parseInt(fromArray[i].substring(3,5));
long tempsec = convertToSec(temphr, tempmin, 0, fromArray[i].substring(Finding.length()-2));
System.out.println("Compared to :" + tempsec);
if((tempsec - seconds) > 0 && minsecdif > (tempsec - seconds))

minsecdif = (tempsec - seconds);
shortest = i;

if(minsec > tempsec)

minsec = tempsec;
shortestsec=i;


if(shortest >=0)

return shortest;

else

return shortestsec;




long convertToSec(int hr,int min,int sec,String AMorPM)

if(hr==12)

hr=0;

long secs = (hr*60*60) + (min*60) + (sec*60);
if(AMorPM.equalsIgnoreCase("PM"))

secs += (12*60*60);

return secs;






i hope it will solve your problem.






i have problem bro

– Black.JAC
Sep 15 '18 at 16:19






what Happened Bro ??

– Niravdas
Sep 15 '18 at 18:02



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ