我想使用ServerValue.Timestamp长格式iam的时间戳值 [英] I want the value of timestamp in long format iam using ServerValue.Timestamp

查看:276
本文介绍了我想使用ServerValue.Timestamp长格式iam的时间戳值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Iam使用ServerValue.TimeStamp并使用散列表上传到firebase

HashMap< String,Object> map = new HashMap();
map.put(timestamp,ServerValue.TIMESTAMP);



但是我需要获取TimeStamp的值没有将其上传到firebase
我试图将其强制转换为long,但总是收到ClassCastException

  Map< String,对象> currentTime = new HashMap<>(); 
currentTime.put(timestamp,ServerValue.TIMESTAMP);
return(long)currentTime.get(timestamp);

任何帮助?!

解决方案

它为我工作,当我在模型类的小改变取代

 地图< String,String> ;时间戳; 



  Object timestamp; 

因为Object类是java中所有类的父类,所以任何子类都可以正常工作。你保存在firebase中的值是可以的,当检索值只是你需要的是把这个对象转换为Long



这里是我使用的模型

  public class UserModel {
private String name,phone,email,birthDate,userType,address;
对象时间戳;

//用于firebase检索数据
public UserModel(){
}
$ b $ public UserModel(String name,String phone,String email,String birthDate ,字符串userType,字符串地址){
this.name = name;
this.phone = phone;
this.email = email;
this.birthDate = birthDate;
this.userType = userType;
this.address = address;
//从firebase服务器自动获取日期
this.timestamp = ServerValue.TIMESTAMP;
}

public String getName(){
return name;
}

public String getPhone(){
return phone;
}

public String getEmail(){
return email;
}

public String getBirthDate(){
return birthDate;
}

public String getUserType(){
return userType;
}

public String getAddress(){
return address;

$ b @Override
public String toString(){
String data =user name+ this.name +,user Address:
+ this.address +,用户电话+ this.phone +,用户电子邮件:+ this.email
+,用户birthdate:+ this.birthDate +,用户类型:+ this.userType
+,注册时间+ this.timestamp;
返回数据;

$ b / * public String getLoginDate(){
DateFormat dateFormat = new SimpleDateFormat(yyyy-MM-dd'T'HH:mm:ss.mmm'Z' );
日期日期=新日期(si)
return;




$ block $
$和$ i将其上传到firebase成功




这里是我从firebase下载数据时收到的reslt


Iam using ServerValue.TimeStamp and upload to firebase using hashmap HashMap<String, Object> map = new HashMap(); map.put("timestamp", ServerValue.TIMESTAMP);

but I need to get the value of TimeStamp in long without upload it to the firebase I have tried to cast it to long but always receive ClassCastException

        Map<String, Object> currentTime = new HashMap<>();
    currentTime.put("timestamp", ServerValue.TIMESTAMP);
    return (long) currentTime.get("timestamp");  

any help ?!

解决方案

it worked for me when i small change in model class by replacing

Map<String , String> timestamp;

with

Object timestamp;

as Object class is the parent of all classes in java so any subclass will work fine with it as when you save the value in firebase it's ok and when retrieve value all just you need is to cast this object to Long

here's my Model i used

public class UserModel {
private String name , phone, email, birthDate, userType, address;
Object timestamp;

// for firebase retrieving data
public UserModel() {
}

public UserModel(String name, String phone, String email, String birthDate, String userType, String address) {
    this.name = name;
    this.phone = phone;
    this.email = email;
    this.birthDate = birthDate;
    this.userType = userType;
    this.address = address;
    //get Date from firebase server automatic
    this.timestamp =  ServerValue.TIMESTAMP;
}

public String getName() {
    return name;
}

public String getPhone() {
    return phone;
}

public String getEmail() {
    return email;
}

public String getBirthDate() {
    return birthDate;
}

public String getUserType() {
    return userType;
}

public String getAddress() {
    return address;
}

@Override
public String toString() {
     String data = " user name" + this.name+" , user Address : "
             +this.address+" , user phone "+this.phone+" , user email : "+this.email
             +" , user birthdate : "+this.birthDate+" , user type : "+this.userType
             + " , registered time "+this.timestamp;
    return data;
}

/* public String getLoginDate() {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.mmm'Z'");
    Date date = new Date(si)
    return ;
}*/
}

and i uploaded it in firebase successufly

and here is the reslt i recieved when download data from firebase

这篇关于我想使用ServerValue.Timestamp长格式iam的时间戳值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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