How get date and time from server?

Rishav Raina :

I want to take time from server or any API because when user change it date from mobile. He/She can get that day reward by changing date. Can anyone tell how to implement time from server. So, if user change it mobile date he did not get that date reward.

import com.google.firebase.database.FirebaseDatabase;

import java.util.Calendar;

public class DailyCheckins extends AppCompatActivity {

    boolean showedToday = false;
    private TextView coins2;
    private Calendar calendar;
    private int weekday;
    private SharedPreferences coins;
    private ImageButton sun, mon, tue, wed, thu, fri, sat;
    private String todayString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_daily_checkins);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        coins = getSharedPreferences("Rewards", MODE_PRIVATE);

        calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        weekday = calendar.get(Calendar.DAY_OF_WEEK);
        todayString = year + "" + month + "" + day;

        sun = (ImageButton) findViewById(R.id.imgSun);
        mon = (ImageButton) findViewById(R.id.imgMon);
        tue = (ImageButton) findViewById(R.id.imgTue);
        wed = (ImageButton) findViewById(R.id.imgWed);
        thu = (ImageButton) findViewById(R.id.imgThu);
        fri = (ImageButton) findViewById(R.id.imgFri);
        sat = (ImageButton) findViewById(R.id.imgSat);

        sun.setEnabled(false);
        sun.setAlpha(.5f);
        mon.setEnabled(false);
        mon.setAlpha(.5f);
        tue.setEnabled(false);
        tue.setAlpha(.5f);
        wed.setEnabled(false);
        wed.setAlpha(.5f);
        thu.setEnabled(false);
        thu.setAlpha(.5f);
        fri.setEnabled(false);
        fri.setAlpha(.5f);
        sat.setEnabled(false);
        sat.setAlpha(.5f);

        if (weekday==1){
            sun.setEnabled(true);
            sun.setAlpha(1f);
        }
        else if (weekday==2){
            mon.setEnabled(true);
            mon.setAlpha(1f);
        }
        else if (weekday==3){
            tue.setEnabled(true);
            tue.setAlpha(1f);
        }
        else if (weekday==4){
            wed.setEnabled(true);
            wed.setAlpha(1f);
        }
        else if (weekday==5){
            thu.setEnabled(true);
            thu.setAlpha(1f);
        }
        else if (weekday==6){
            fri.setEnabled(true);
            fri.setAlpha(1f);
        }
        else if (weekday==7){
            sat.setEnabled(true);
            sat.setAlpha(1f);
        }
    }

    public void monCheck(View view) {
        SharedPreferences dailyChecks = getSharedPreferences("DAILYCHECKS", 0);
        boolean currentDay = dailyChecks.getBoolean(todayString, false);

        if (!currentDay){
            Toast.makeText(this, "10 Coins Recieved!", Toast.LENGTH_SHORT).show();
            SharedPreferences.Editor daily = dailyChecks.edit();
            daily.putBoolean(todayString, true);
            daily.apply();
            int coinCount = Integer.parseInt(coins.getString("Coins", "0"));
            coinCount = coinCount + 10;
            SharedPreferences.Editor coinsEdit = coins.edit();
            coinsEdit.putString("Coins", String.valueOf(coinCount));
            coinsEdit.apply();
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("History");
            myRef.push().setValue("Monday Daily Checkin - (+10 Coins)");
        }
        else {
            Toast.makeText(this, "Reward already recieved", Toast.LENGTH_SHORT).show();
        }
    }

    public void tueCheck(View view) {
        SharedPreferences dailyChecks = getSharedPreferences("DAILYCHECKS", 0);
        boolean currentDay = dailyChecks.getBoolean(todayString, false);

        if (!currentDay){
            Toast.makeText(this, "10 Coins Recieved!", Toast.LENGTH_SHORT).show();
            SharedPreferences.Editor daily = dailyChecks.edit();
            daily.putBoolean(todayString, true);
            daily.apply();
            int coinCount = Integer.parseInt(coins.getString("Coins", "0"));
            coinCount = coinCount + 10;
            SharedPreferences.Editor coinsEdit = coins.edit();
            coinsEdit.putString("Coins", String.valueOf(coinCount));
            coinsEdit.apply();
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("History");
            myRef.push().setValue("Tuesday Daily Checkin - (+10 Coins)");
        }
        else {
            Toast.makeText(this, "Reward already recieved", Toast.LENGTH_SHORT).show();
        }
    }

    public void wedCheck(View view) {
        SharedPreferences dailyChecks = getSharedPreferences("DAILYCHECKS", 0);
        boolean currentDay = dailyChecks.getBoolean(todayString, false);

        if (!currentDay){
            Toast.makeText(this, "20 Coins Recieved!", Toast.LENGTH_SHORT).show();
            SharedPreferences.Editor daily = dailyChecks.edit();
            daily.putBoolean(todayString, true);
            daily.apply();
            int coinCount = Integer.parseInt(coins.getString("Coins", "0"));
            coinCount = coinCount + 20;
            SharedPreferences.Editor coinsEdit = coins.edit();
            coinsEdit.putString("Coins", String.valueOf(coinCount));
            coinsEdit.apply();
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("History");
            myRef.push().setValue("Wednesday Daily Checkin - (+20 Coins)");
        }
        else {
            Toast.makeText(this, "Reward already recieved", Toast.LENGTH_SHORT).show();
        }
    }

    public void thuCheck(View view) {
        SharedPreferences dailyChecks = getSharedPreferences("DAILYCHECKS", 0);
        boolean currentDay = dailyChecks.getBoolean(todayString, false);

        if (!currentDay){
            Toast.makeText(this, "20 Coins Recieved!", Toast.LENGTH_SHORT).show();
            SharedPreferences.Editor daily = dailyChecks.edit();
            daily.putBoolean(todayString, true);
            daily.apply();
            int coinCount = Integer.parseInt(coins.getString("Coins", "0"));
            coinCount = coinCount + 20;
            SharedPreferences.Editor coinsEdit = coins.edit();
            coinsEdit.putString("Coins", String.valueOf(coinCount));
            coinsEdit.apply();
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("History");
            myRef.push().setValue("Thursday Daily Checkin - (+20 Coins)");
        }
        else {
            Toast.makeText(this, "Reward already recieved", Toast.LENGTH_SHORT).show();
        }
    }

    public void friCheck(View view) {
        SharedPreferences dailyChecks = getSharedPreferences("DAILYCHECKS", 0);
        boolean currentDay = dailyChecks.getBoolean(todayString, false);

        if (!currentDay){
            Toast.makeText(this, "30 Coins Recieved!", Toast.LENGTH_SHORT).show();
            SharedPreferences.Editor daily = dailyChecks.edit();
            daily.putBoolean(todayString, true);
            daily.apply();
            int coinCount = Integer.parseInt(coins.getString("Coins", "0"));
            coinCount = coinCount + 30;
            SharedPreferences.Editor coinsEdit = coins.edit();
            coinsEdit.putString("Coins", String.valueOf(coinCount));
            coinsEdit.apply();
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("History");
            myRef.push().setValue("Friday Daily Checkin - (+30 Coins)");
        }
        else {
            Toast.makeText(this, "Reward already recieved", Toast.LENGTH_SHORT).show();
        }
    }

    public void satCheck(View view) {
        SharedPreferences dailyChecks = getSharedPreferences("DAILYCHECKS", 0);
        boolean currentDay = dailyChecks.getBoolean(todayString, false);

        if (!currentDay){
            Toast.makeText(this, "30 Coins Recieved!", Toast.LENGTH_SHORT).show();
            SharedPreferences.Editor daily = dailyChecks.edit();
            daily.putBoolean(todayString, true);
            daily.apply();
            int coinCount = Integer.parseInt(coins.getString("Coins", "0"));
            coinCount = coinCount + 30;
            SharedPreferences.Editor coinsEdit = coins.edit();
            coinsEdit.putString("Coins", String.valueOf(coinCount));
            coinsEdit.apply();
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("History");
            myRef.push().setValue("Saturday Daily Checkin - (+30 Coins)");
        }
        else {
            Toast.makeText(this, "Reward already recieved", Toast.LENGTH_SHORT).show();
        }
    }

    public void sunCheck(View view) {
        SharedPreferences dailyChecks = getSharedPreferences("DAILYCHECKS", 0);
        boolean currentDay = dailyChecks.getBoolean(todayString, false);

        if (!currentDay){
            Toast.makeText(this, "50 Coins Recieved!", Toast.LENGTH_SHORT).show();
            SharedPreferences.Editor daily = dailyChecks.edit();
            daily.putBoolean(todayString, true);
            daily.apply();
            int coinCount = Integer.parseInt(coins.getString("Coins", "0"));
            coinCount = coinCount + 50;
            SharedPreferences.Editor coinsEdit = coins.edit();
            coinsEdit.putString("Coins", String.valueOf(coinCount));
            coinsEdit.apply();
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("History");
            myRef.push().setValue("Sunday Daily Checkin - (+50 Coins)");
        }
        else {
            Toast.makeText(this, "Reward already recieved", Toast.LENGTH_SHORT).show();
        }
    }
}

someone refer me https://timezonedb.com/api but I didn't know how to implement.

Muntasir Aonik :

Using the HttpGet, Client and Response, I manage to get a server's current time from the response Date Header. I can call this all the times I want and will get confident responses (Google is almost 100% available and I can trust on getting correct Date and Time)

try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(new HttpGet("https://google.com/"));
        StatusLine statusLine = response.getStatusLine();
        if(statusLine.getStatusCode() == HttpStatus.SC_OK){
            String dateStr = response.getFirstHeader("Date").getValue();
            //Here I do something with the Date String
            System.out.println(dateStr);

        } else{
            //Closes the connection.
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    }catch (ClientProtocolException e) {
        Log.d("Response", e.getMessage());
    }catch (IOException e) {
        Log.d("Response", e.getMessage());
    }

Check this Project : Click Here

Project Explanation :

Add this in Manifest inside :

 <uses-library
            android:name="org.apache.http.legacy"
            android:required="false" />

Add this Library In Build.gradle

android {
        useLibrary 'org.apache.http.legacy'
    }

Check the Java file and copy everything .

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=389123&siteId=1