I've been looking for something and its not clear, but the answer should be.
I'm working on an app using the phone's locale time.
If I just use DateTime.now() to get the current time, do I get the time of the current timezone the phone is in (assuming this is auto-updated on the phone) or the current time of the phones origin locale??
So if the phone's locale is say Germany (GMT+1) at 14:25 and using my app I travel to Portugal (GMT), the time on my phone will update to GMT 13:25 when I cross into Portugal, and time goes back an hour.
So when my service looks for the current time with
DateTime CurrentHomeTime=DateTime.Now()
what time will CurrentHomeTime say?? 13:25 for the current country's timezone, or 14:25 for the origin locale of the phone??
In Android you would use the Time class in order to get the current time:
Time now = new Time();
now.setToNow();
The default constructor above will use the current time zone.
You can also initialize to another time zone if you like:
Time now = new Time("GMT")
http://developer.android.com/reference/android/text/format/Time.html
http://developer.android.com/reference/java/util/TimeZone.html
Related
I am using (maybe incorrectly) a Joda DateTime to store a reminder time in a reminders app. When the timezone changes on the device (because of DST or just moving to a different TZ) I want to be able to reset the reminders to use the current timezone but keep the time.
For example: I set a reminder for Friday May 15th, at 15:00. If the timezone changes before that time, I want to be able to recreate the reminder for Friday May 15th at 15:00 local time.
I have already tried to use withZoneRetainFields but I haven't been able to keep the time unchanged:
new DateTime(reminderTimeMillis, DateTimeZone.forID(previousTimeZone)).withZoneRetainFields(DateTimeZone.getDefault())
Well, that method should basically work:
#Test
public void change_timezone_of_reminder() {
DateTime originalReminderDateTime = LocalDateTime.parse("2015-05-15T15:00").toDateTime(
DateTimeZone.forID("Europe/Berlin"));
assertThat(originalReminderDateTime.toString(), equalTo("2015-05-15T15:00:00.000+02:00"));
long reminderMillis = originalReminderDateTime.getMillis();
DateTime updatedTime = new DateTime(reminderMillis, DateTimeZone.forID("Europe/Berlin"))
.withZoneRetainFields(DateTimeZone.forID("America/New_York"));
assertThat(updatedTime.toLocalDateTime(), equalTo(LocalDateTime.parse("2015-05-15T15:00")));
assertThat(updatedTime.toString(), equalTo("2015-05-15T15:00:00.000-04:00"));
}
So, how are you evaluating that the time field is not changing?
Also, if you are storing the reminder time as millis-after-epoch, you don't need to adjust it for DST shifts, if you're calculating the millis using a full timezone ("Europe/Berlin") rather than a fixed offset.
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 want to subtract two dates in Android project.
When I use the statement:
DateTime now = new DateTime(DateTimeZone.UTC);
It gives 2014-03-17T12:49:06.670Z value instead of 2014-03-17T14:49:06.670Z (this is my current time on Android device)
When I convert this DateTime (2014-03-17T12:30:08.673+02:00) to UTC Time (2014-03-17T10:30:08.673Z) it gives the correct result but not for DateTime now = new DateTime(DateTimeZone.UTC);
What is wrong with new DateTime(DateTimeZone.UTC);?
Any help would be appreciated.
Try getting your local time this way:
DateTime now = new DateTime();
This time is "your current time on Android device". More precisely DateTime calculates its fields with respect to a time zone. It means it will return UTC+2 if you are located in Eastern European Countries (Winter Time) or Wester European Countries (Summer time).
On my PC nothing is wrong with new DateTime(DateTimeZone.UTC). The type DateTime internally stores the given time zone (here UTC) and uses it for the output of its method toString(), hence the UTC-string-format and not another alternative format with offset = +02:00.
Hi All please any one can tell how to convert the UTC time into the android device current time....
Actually when we are using the GPS service in mobile I am displaying lat,lon,altitude and time
Here the time is coming in format of UTC milliseconds I want to convert that time into the device current time
Try this code
String dat = new SimpleDateFormat("ddMMyy").format(location.getTime())
Date d = new Date(System.currentTimeMillis());
d.toLocaleString(); //to phone's locale
d.toGMTString(); //in GMT
I create a GPS tracking, but when I use DDMS to send coordinate to the emulator and I want to get time of this coordinate through
Date date = new Date(gpsPos.getTime_stamp()); //gpsPos is an obj
gpsPos.setDate_time(date.toString());
The time I got from the function in miliseconds and I converted it to date using date class in java, but the date I got from converted is different from the real date of device.
How can i get the time location equals real time ? any idea? thx
what is the different between date.toString() and date.toGMTString()?
EDIT:
how can we solve it i want my gps time is equal to the real time (logic enough) in order to use it test sth later?
Date(milliSeconds) will create the Date object based on GMT, Date.toString will create a string based on your locale (by default but can be modified). If the milliseconds you used to create the Date object is actually based on your Locale then your time will be off by 4 hours if you're in EDT as an example.