Reputation: 11
I am working on an Android based app and using JAVA for my web services too. I mean my website is built with JAVA too. So here is thing, I am new at JAVA programming and this is for my learning stuffs. I have been searching for help to create something like "TIME REMAINING" notification in Android. So deal is I don't know how to use TIME/SQL related class in JAVA.
What I want to do is for example, event is happened on 1st July, 2015 at 12:00 am and will be available for next 48 hours. So, I want to add expire time in database. I don't want to use "2015-07-12 06:47:39.154 " for my database, instead of that I want to save milliseconds where I can subtract current time and can say that you have few hours left etc. And in my android app, I want to show it like SNAPCHAT. Like how you can see photo cropped in circle during 24 hours.
I will work on this for my android stuff. The thing I am looking for is when my app communicates to server, my website will respond in percentage of remaining time. Like if 36 hours remaining than 75, if 30 than 65 or some, if 24 than 50 etc. So, can anyone help me how can I use it? I am looking for help so, help will be appreciated.
My actual time code in java,
//for timestamp
long retryDate = System.currentTimeMillis();
int sec = 604800;
Timestamp original = new Timestamp(retryDate);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(original.getTime());
cal.add(Calendar.SECOND, sec);
Timestamp later = new Timestamp(cal.getTime().getTime());
Upvotes: 0
Views: 178
Reputation: 285
try this,
long timeAfterTwoDays =(System.currentTimeMillis() + 172800000);
store this in your database. This will give you exact time after 48 hours.
So when you call it from your database, do this,
long timeFromDB = //your saved time
long currentTime = System.currentTimeMillis();
int remainingTime = ((timeFromDB - currentTime))/1728000;
This will give you remaining time in percentage. Hope this answer will be helpful as you said help will be appreciated. If you face any problem or error, let me know here. I have tested it and I had to do couple tricks but it will work. ;)
Upvotes: 1