Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android Firebase时间戳_Java_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

Java Android Firebase时间戳

Java Android Firebase时间戳,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,带有onClickListener的按钮,用于在子项(“日期”)中发布时间戳 它很好用 我的问题是,当我尝试在手机应用程序中检索时,我得到的是当前时间,而不是实际的时间戳时间。 users.child(user.getUid()).child("Date").addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull Da

带有onClickListener的按钮,用于在子项(“日期”)中发布时间戳

它很好用

我的问题是,当我尝试在手机应用程序中检索时,我得到的是当前时间,而不是实际的时间戳时间。

 users.child(user.getUid()).child("Date").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                Date date=new Date();
                SimpleDateFormat sfd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.getDefault());
                sfd.format(new Date());



                TimeSold.setText(String.valueOf(date));


            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
Firebase上的时间戳正在更改,不同用户的时间戳应该不同。在我的应用程序中,它只为所有用户提供相同的当前时间

我的火力基地


如果时间戳保存为
Long
值,请输入:

Date date = new Date(dataSnapshot.getValue(Long.class));
SimpleDateFormat sfd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", 
                                    Locale.getDefault());
String text = sfd.format(date);
TimeSold.setText(text);
onDataChange
回调中

您的解决方案不起作用,因为您正在将当前日期传递到格式,如您所写:

Date date = new Date()
默认情况下,它采用本地当前时间

我的问题是,当我尝试在手机上的应用程序中检索时,我得到的是当前时间,而不是实际的时间戳时间

之所以会发生这种情况,是因为在回调中,您实际上是在创建一个新的
Date
对象,而不是从数据库中获取它。要解决此问题,请使用以下方法:

public static String getTimeDate(long timestamp){
    try{
        DateFormat dateFormat = getDateTimeInstance();
        Date netDate = (new Date(timestamp));
        return dateFormat.format(netDate);
    } catch(Exception e) {
        return "date";
    }
}
public static String getTimeDate(long timestamp){
    try{
        Date netDate = (new Date(timestamp));
        SimpleDateFormat sfd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.getDefault());
        return sfd.format(netDate);
    } catch(Exception e) {
        return "date";
    }
}
以及以下代码行从数据库中获取日期:

ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        long date = ds.getValue(Long.class);
        Log.d(TAG, getTimeDate(date));
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
users.child(user.getUid()).child("Date").addListenerForSingleValueEvent(valueEventListener);
编辑:

要使用格式化日期,请使用以下方法:

public static String getTimeDate(long timestamp){
    try{
        DateFormat dateFormat = getDateTimeInstance();
        Date netDate = (new Date(timestamp));
        return dateFormat.format(netDate);
    } catch(Exception e) {
        return "date";
    }
}
public static String getTimeDate(long timestamp){
    try{
        Date netDate = (new Date(timestamp));
        SimpleDateFormat sfd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.getDefault());
        return sfd.format(netDate);
    } catch(Exception e) {
        return "date";
    }
}

谢谢你,亚历克斯,我现在可以得到实际的时间戳,但我希望它写的正常格式“dd-MM-yyyy-HH:MM:ss”。我应该在我的代码中更改什么???很高兴听到它工作了!:)若要使用格式化的日期,请查看我的更新答案。谢谢Alex,感谢您的努力:)现在它起作用了两个答案都被接受了,但我先检查了Marco的答案。不客气,但您需要知道,这不是这个社区的工作方式。的确,Marco在18秒前回答了问题,但他在2小时后通过添加我答案中的代码给出了答案。规则是奖励第一个以正确方式回答你问题的人。因此,希望您重新考虑接受正确答案。谢谢您,马可,我尝试了您的代码,但其中有许多红色下划线。我认为缺少了一些内容:(