0

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?

5 Answers5

1

If you don't want system time you need some other source then. There are a few possibilities that I know:

  1. Get it from web - Internet needed
  2. Get it from router - enabled wifi needed (NTP)
  3. 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 ...

Mark
  • 5,466
  • 3
  • 23
  • 24
  • Ok. so, i see, internet or GPS is the only way to get REAL current time. But what if user enters some place without any connections(underground, or building). I have an idea, that i can calculate current time by using my last successfull time update(from GPS or internet) and calculate elapsed time. – Иван Зверев Nov 28 '14 at 11:21
  • Possibly phone restart and your idea will fail. Think about trusting system time, why can't you do that? – Mark Nov 28 '14 at 13:19
  • i have tricky users) they have tasks for every day, but they modify system date, and "simulate" every-day work in just 1 day – Иван Зверев Dec 01 '14 at 13:50
  • When that's the case you could register some broadcast receiver for Internet Availability, get current time from server and save it with current SystemClock.uptimeMillis() in your preferences. On app start you can get stored values and current uptime to calculate actual time. Your users would need to have Internet enabled only once per reboot (event without starting your app). Possibly you would also need to clear those values on phone reboot. I don't think anyone will be able to trick this :) – Mark Dec 01 '14 at 14:00
0

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();
Machado
  • 14,105
  • 13
  • 56
  • 97
0
    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.

Gulnaz Ghanchi
  • 485
  • 3
  • 14
0

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:

enter image description here

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

Community
  • 1
  • 1
Ahmad MOUSSA
  • 2,729
  • 19
  • 31
0

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.

Eric Towers
  • 4,175
  • 1
  • 15
  • 17