0

I am working on application which is based on getting current time. I am having the issue when i change my mobile time then it also changes time in the app hence the false time value is accepted in the server / database ,

i'm using firebase as my database. i'm using this code to get date and time ,

    Date date = Calendar.getInstance().getTime();

    DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
    String strDate = dateFormat.format(date);**
Dhruv patel
  • 61
  • 1
  • 10
  • 2
    What's your question? – shmosel Jul 19 '18 at 01:40
  • I'm not sure exactly why it would be a surprise that changing the time, er, you know, changes the time. Your phone doesn't have an extra time source that it would use instead of the time you set the phone to. – kshetline Jul 19 '18 at 01:51
  • 1
    When saving to a database and you need to log time. Have the server put in the time. – Joel Libby Jul 19 '18 at 01:55
  • My app is like location based attendance ,if this problem continue Users can go after the lecture ends , change time from the phone and mark their attendance any time . – Dhruv patel Jul 19 '18 at 01:57
  • [How to get current time from internet in android](https://stackoverflow.com/questions/13064750/how-to-get-current-time-from-internet-in-android) – Ole V.V. Jul 19 '18 at 02:43
  • As an aside consider throwing away the long outdated `Date` class and the notoriously troublesome `SimpleDateFormat` and adding [the ThreeTenABP library](https://github.com/JakeWharton/ThreeTenABP) to your project so you may use java.time, the modern Java date and time API, instead. It’s so much nicer to work with. – Ole V.V. Jul 19 '18 at 02:46

1 Answers1

2

You need a server time record on your application.

try the code below:

import com.google.firebase.database.ServerValue;
public Object serverTimestamp = ServerValue.TIMESTAMP;

The serverTimestamp is millisecond. Try the java method below to convert millisecond to date string:-

import org.apache.commons.lang3.time.DateFormatUtils;

 public Striing toDateString(Long date){
 return DateFormatUtils.format(date, "yyy-MM-dd HH:mm");
}
Vr Witness
  • 45
  • 2
  • How can i convert that into date and time , and into the string , i have to store date and time in database as a string – Dhruv patel Jul 19 '18 at 01:59
  • I believe that Firebase (like most DBMSs) has a date and time datatype. By all probability it’s better suited for storing your time than strings are. – Ole V.V. Jul 19 '18 at 03:08