0

I am developing a mobile application where user can make request of goods and services.Here,I am trying to get the accurate date & time of requests when user does submission.I want to know which is the best way to acquire date and time from mobile among following:

  1. GPS
  2. NETWORK PROVIDER
  3. DEVICE TIME AND DATE.

There is some scenario, which need to avoid. -> What if user set another date and time, instead of correct one. -> What if Network is not available.

please lemme know so that I can start working on this.Thank you in advance.

Hammad
  • 3
  • 3
Rushi Ayyappa
  • 2,708
  • 2
  • 16
  • 32
  • 1
    If you want accurate time, don't rely on the client. Get the time from the server instead. You can never rely on GPS or networking being available. – adelphus Nov 05 '15 at 18:40
  • i need to send the time and date as a request parameter.now if i get the date and time as a response from server ,i cannot change the whole logic i have already written. – Rushi Ayyappa Nov 06 '15 at 08:12

4 Answers4

1

network Provider in case of network available after then device

Pankaj K Sharma
  • 240
  • 1
  • 12
0

First check for Network Provider and if available good else go for Device time and date which you will manually set. Last option could be GPS.

  • could you justify why you mentioned GPS at last? – Rushi Ayyappa Nov 06 '15 at 08:33
  • Because if your network provider and device time is not successful in setting your time, you could use GPS co-ordinates to get your location and thereby get the time of the location. –  Nov 06 '15 at 10:20
0

You can get the device time by using:

GregorianCalendar c = new GregorianCalendar();

And then use GregorianCalendar methods to get what you need.

If you really need the date to be accurate, then getting the device date may not me the best option, since it can be incorrect if the user has changed it.

In this case, you'd need to use an NTP Server. How to do this is described here.

Community
  • 1
  • 1
Not Gabriel
  • 531
  • 4
  • 13
0

Getting local date-time from device is more than enough. it can be done easily using this:

long currTime= System.currentTimeMillis();

If you need this time to be displayed in user-readable value, use this:

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(currTime);
return formatter.format(cal.getTime());

I assume that you need a web server for your task, and if this date-time value must be a clue element, then server should get it's own time, instead of depending on mobile device.

artman
  • 632
  • 2
  • 7
  • 16