I am currently trying to work with Location updating.
I've been using the below to work out the time-stamps of when the last update was processed:
String NewLocationTime = DateFormat.getTimeInstance().format(new Date());
However, to work out the difference between two times, I needed to parse it to a Time format. However, for some reason the Android Studio didn't recognize the DateTimeFormatter, and because I don't know in what format the String is going to come up, I am not quite sure what to put in the formatting either.
I believe it would be much easier if I was to be able to get a Time object straight away, so I can use something like:
long diffInMinutes = java.time.Duration.between(dateTime1, dateTime2).toMinutes();
Can anyone tell me how I get the Time object straight away, or why the Android Studio does not recognize the DateTimeFormatter?
Thank you
Use new Date().getTime() and what you get is the time in milliseconds and you can work with it as you want.
You need to use Date and use Date.getTime() for difference calculation. I do though recommend Joda-Time.
Also look at Calendar.
Time an sql aware wrapper around Date and there is no such thing as DateTime or DateTimeFormatter.
Related
in my application i need to get current Date and time, every time the user inputs data with it.
I know i can use System.currentTimeMillis(), but it can give me wrong time(because it gives system time, witch can be edited by user)
So i see the only way is to call server for current time, when the user makes data input. but i am not sure that internet connection is always awailable.
Is there any way to get current time (not system time) in android, without using internet connection?
If you don't want system time you need some other source then.
There are a few possibilities that I know:
Get it from web - Internet needed
Get it from router - enabled wifi needed (NTP)
Get it from GPS - GPS fix needed
All of these aren't very helpful I believe. I don't think you can find a way of getting current time without connecting so something externally.
In my opinion you should use system time and assume it's set correctly. When it's not your application shouldn't crash and should gently know user that he has wrong content because of wrong dates ...
I believe there's no way to get the current system time without the timezone.
A good approach would be getting the current system time first
long time= System.currentTimeMillis();
And then getting the correct TimeZone to handle it
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
long dateInMillis = System.currentTimeMillis();
String format = "yyyy-MM-dd HH:mm:ss";
final SimpleDateFormat sdf = new SimpleDateFormat(format);
String dateString = sdf.format(new Date(dateInMillis));
Use dateString as your variable which contains current date time as timestamp.
Well, I googled about your topic, and i got logic solution but not tested:
" Use The network-provided values ", in the android phone settings, it;a as shown in the picture bellow:
The screen I show is DateTimeSettings. The checkbox "Use network-provided values" is associated to the shared preference String KEY_AUTO_TIME = "auto_time"; and also to Settings.System.AUTO_TIME
This settings is observed by an observed called mAutoTimeObserver in the 2 network ServiceStateTrackers: GsmServiceStateTracker and CdmaServiceStateTracker.
Both implementations call a method called revertToNitz() when the settings becomes true. Apparently NITZ is the equivalent of NTP in the carrier world.
Bottom line: You can set the time to the value provided by the carrier thanks to revertToNitz(). Unfortunately, I haven't found a mechanism to get the network time. If you really need to do this, I'm afraid, you'll have to copy these ServiceStateTrackers implementations, catch the intent raised by the framework (I suppose), and add a getter to mSavedTime.
For more informations, i suggest you to check this link here
Use the ScheduledExecutorService with scheduleAtFixedRate to send you "clock ticks". If the user initiates an event and the number of accumulated "clock ticks" since the last event doesn't match the time change on the system clock, you're being lied to.
You don't need to know the correct time. You need to know the correct interval. This can be done with any periodic source, even a local one. (Timekeeping is two jobs: a metronome and a labeler for the intervals of the metronome. You don't want the system's labels because they can be made to lie, but the metronome ticks on even if the labels are changed.)
I'd recommend a relatively slow tick rate (<= 1 tick per minute) and rather sloppy comparisons (within 2%, maybe) since the various clocks may not be all that accurate.
I'm really stuck with a certain problem and I'm hoping someone can help me understand the problem and come to a solution. I've looked online a fair bit but can't see an answer unless it's been staring me in the face :-/
Basically, I'm creating a very basic TV Guide app. It parses data from an RSS feed which has days offset (yesterday was -1. today is 0, tomorrow is 1, etc etc) and I'm trying to implement a DatePicker that allows the user to see what is on a particular channel when they select yesterday, today, tomorrow, etc.. but if they pick a date that is out-with the range (at the moment it's a week in advance), a simple Toast message will be displayed.
My questions I guess are, firstly, how do I use maybe an IF ELSE to either parse the specific channel data for the day the user wants or display an error Toast message, and, how do I go about converting the days from what the user has put in compared to the actual date today into integers? If they select yesterday's date it will go to URL "http://example.com/-1/channel", if they select tomorrow's date it will go to URL "http://example.com/1/channel" etc etc etc.
Code is available if anyone needs to see it, but I think if someone would be kind enough to explain the logic, I'd like to see if I can come to the answer myself...
Thanks a lot folks!!
You should use a DatePicker to allow the user to choose the when.
Time in Android is stored on a long (not an int). And the long time can easily be converted back and forth between long (always milli-seconds) and a Date object.
The Date object gives you all sorts of tools to compare before and after, look at months, minutes, hours, etc.
The current time is determined by:
long nowMs = System.currentTimeMillis();
int nowSec = (int)(nowMs / 1000);
There is also a very important Calendar object. This allows you to parse textual date formats as delivered by your http functions in and out of various dates.
For example:
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss Z");
String text = sdf.format(cal.getTime();
You will have to put all these tools together with a DatePicker example such as the one here Create a DatePicker to complete your TV Guide application.
Reference:
Date
Calendar
DatePicker
EDIT : Check David's Answer its better.
First Filter the date selected with today's date. You can compare it by date.isbefore(date) or date.isafter(date) these booleans will let you tell know if a date is of past or future or present. then to further calculate the days inbetween you can make a method with switch statement that will basically convert the selected date and the current date into miliseconds(Date.getTimeinmiliseconds)
if the date is of past take the difference of present time in miliseconds and past date in miliseconds. If the date is of future do the opposite. Take the difference and convert it to days difference with appropriate sign(negative/positive).
Please refer this link for a better coding example
Well instead of 2011.10.19 10:30 I want to have something like 10:30, Wednesday or instead of 2011.09.19 10:30 I want to have something like 10:30, September. I know that I can code this somehow but I want clean and not greedy solution. The code will run on android devices so I need something that acts gracefully with resources and something that is well written. PreatyTime doesn't helps me cause it calculates distance from this moment , and there is no formatting like 12:00, Monday.
Does someone know some good pattern that I can follow when writing this kind of things ?
have a look at this
It will give you any format you want,by just passing a Calendar object.
For example:
Calendar cal=Calendar.getInstance();
String myFormat=String.format("%1$tH:%1$tM %1$tB",cal);
would give you format like "10:30 September".
I'm trying to program an android app that show's the current week_of_the_year but haven't found anything similar to what I want, I've seen the date picker but that doesn't show the week numbers and I've also been on android developer site.
So is there any way to view the current week_of_the_year in a really simple way?
If there's anyone who can show me this I'll be greatly appreciated.
new GregorianCalendar().get(Calendar.WEEK_OF_YEAR);
This should give you the current week in the current time zone. If you need it for a specific date, there are alternate constructors for GregorianCalendar.
The easiest way to do this on Android is probably to use the Time class:
int currentWeek = new Time().getWeekNumber();
I am new in Android.I can't find any way to calculate the time difference.So please help me. I designed a simple application. In this program I took 3 EditTexts and in these EditText I want to input the login time and logout time then store these values in the Database, And in the 3rd EditText I want to display the difference between these two time.
You can use standard Java api calls:
System.nanoTime()
System.currentTimeMillis()
Also, check out these two links for calculating time difference.
An easier approach -
Date interestingDate = new Date();
different in milliseconds between the actual current date and interestingDate by doing:
(new Date()).getTime() - interestingDate.getTime();
Check the referenced answer in this link.