Java 如何从服务器获取日期和时间?

Java 如何从服务器获取日期和时间?,java,android,datetime,Java,Android,Datetime,我想从服务器或任何API中抽出时间,因为当用户从移动设备更改它时。他/她可以通过更改日期获得当天奖励。谁能告诉我如何从服务器上实现时间。所以,若用户更改它的移动日期,他并没有得到那个日期奖励 import com.google.firebase.database.FirebaseDatabase; import java.util.Calendar; public class DailyCheckins extends AppCompatActivity { boolean sho

我想从服务器或任何API中抽出时间,因为当用户从移动设备更改它时。他/她可以通过更改日期获得当天奖励。谁能告诉我如何从服务器上实现时间。所以,若用户更改它的移动日期,他并没有得到那个日期奖励

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();
        }
    }
}

有人提到我,但我不知道如何实现。

使用HttpGet、Client和Response,我设法从Response Date头获取服务器的当前时间。我可以随时打电话给你,并且会得到自信的回复(谷歌几乎100%可用,我可以相信你会得到正确的日期和时间)

检查此项目:

项目说明:

将此添加到清单中:

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

当然可以。我已经在答案上添加了我的项目链接。看看这个,你能帮我在上面的jave文件库中实现吗。。让我来帮你。谢谢,实现后把代码发给我!你想什么时候到。格林尼治标准时间?
 <uses-library
            android:name="org.apache.http.legacy"
            android:required="false" />
android {
        useLibrary 'org.apache.http.legacy'
    }