一种使用Firebase在Android中存储日期和时间的方法? [英] A method to store date and time in Android using Firebase?

查看:53
本文介绍了一种使用Firebase在Android中存储日期和时间的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android Studio的新手,也是Firebase数据库的新手.我已经创建了一个应用程序,但是我不知道如何存储日期和时间.你能帮我吗?
下面是我的代码:

I'm new to Android Studio and new to the Firebase database as well. I have created an app but I don't know how to store date and time. Can you help me guys?
Here below is my code:

public class TimeBookActivity extends AppCompatActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {

    private FirebaseAuth auth;
    private DatabaseReference databaseReference;
     Button bnt_picDate_picDate;
     TextView textView3;
    int time, day, month, year, hour, minute, a;
    int timeFinal, dayFinal, monthFinal,yearFinal, hourFinal, minuteFinal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_book);

        auth = FirebaseAuth.getInstance();
        if (auth.getCurrentUser()== null){
            startActivity(new Intent(TimeBookActivity.this, LoginActivity.class));
        }

        databaseReference = FirebaseDatabase.getInstance().getReference();
        textView3 = (TextView) findViewById(R.id.textView3);
        bnt_picDate_picDate = (Button) findViewById(R.id.bnt_picDate_picDate);

        FirebaseUser user = auth.getCurrentUser();

        bnt_picDate_picDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v == bnt_picDate_picDate){

                    Calendar c = Calendar.getInstance();
                    year = c.get(Calendar.YEAR);
                    month = c.get(Calendar.MONTH);
                    day = c.get(Calendar.DAY_OF_MONTH);

                    DatePickerDialog datePickerDialog = new DatePickerDialog(TimeBookActivity.this, TimeBookActivity.this, year, month, day);
                    datePickerDialog.show();

                }


            }
        });


    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onDateSet(DatePicker view, int i, int i1, int i2) {
        String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        yearFinal = i;
        monthFinal = i1 + 1;
        dayFinal = i2;
        String monthS = months[i1];

        Calendar c = Calendar.getInstance();
        hour = c.get(Calendar.HOUR_OF_DAY);
        minute = c.get(Calendar.MINUTE);
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);

        textView3.setText("Year: "+yearFinal+ "\n" +
                "Month: "+monthS+ "\n" +
                "Day: "+dayFinal+ "\n" +
                "Hour: "+hourFinal+ "\n" +
                "Minute: "+minuteFinal+ "\n" +
                "\n" +"Congratulation You are Booked!");

        TimePickerDialog timePickerDialog = new TimePickerDialog(TimeBookActivity.this,TimeBookActivity.this,hour,minute, DateFormat.is24HourFormat(TimeBookActivity.this));

        DatePickerDialog datePickerDialog = new DatePickerDialog(TimeBookActivity.this, TimeBookActivity.this, year, month, day);

    }

    @Override
    public void onTimeSet(TimePicker view, int i, int i1) {
        hourFinal = i;
        minuteFinal = i1;


    }
}

推荐答案

Calendar c = Calendar.getInstance();
            c.set(Syear, Smonth, Sday, Shours, Sminute);
            long startDate = c.getTimeInMillis();

其中Syear,Smonth,Sday,Shours,Sminute可以从OnDateSet和 OnTimeSet 现在您可以将Datetime放到FireBase中了

where Syear, Smonth, Sday, Shours, Sminute you can get it from your OnDateSet and OnTimeSet now you can put Datetime to your FireBase as long

这篇关于一种使用Firebase在Android中存储日期和时间的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆