[英]Java convert timestamp object to date format
In my Android app, I'm showing an List<Object>
, it contains String , int and Timestamp values, and I don't know which item is the Timestamp one. 在我的Android应用中,我正在显示List<Object>
,它包含String , int和Timestamp值,但我不知道哪个项目是Timestamp。
Now it appears like this: Timestamp(seconds=1556208432, nanoseconds=754000000) 现在看起来像这样:Timestamp(seconds = 1556208432,nanoseconds = 754000000)
What I wanna do is parse this into a readable Date format. 我想做的就是将此解析为可读的Date格式。
Here's the code I tried: 这是我尝试的代码:
TextView t2 = view.findViewById(android.R.id.text2);
String tS2 = String.valueOf(values.get(position));
if(tS2.contains("Timestamp(seconds=")) {
long ts = Timestamp.parse(tS2);
Date date = new Date(ts);
Log.d(TAG, "date: "+date);
} else {
t2.setText(tS2);
}
And parse it with SimpleDateFormat, like this: 并使用SimpleDateFormat解析它,如下所示:
String myFormat = "yyyy MMM dd";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.ENGLISH);
t2.setText(sdf.format(something));
I get this error: 我收到此错误:
java.lang.IllegalArgumentException at java.util.Date.parse java.util.Date.parse上的java.lang.IllegalArgumentException
EDIT1: EDIT1:
It dies with the error below when it gets to the Date part. 到达日期部分时,死于下面的错误。
Object time = values.get(position);
com.google.firebase.Timestamp ts = (com.google.firebase.Timestamp) time;
String str = ts.toString();
Date date = new Date(Long.parseLong(str));
java.lang.NumberFormatException: For input string: "Timestamp(seconds=1556208432, nanoseconds=754000000)" java.lang.NumberFormatException:对于输入字符串:“ Timestamp(seconds = 1556208432,nanoseconds = 754000000)”
You are converting the timestamp
to string
which will not convert the value it's holding to string but describe the object itself . 您正在将timestamp
转换为string
,这不会将其持有的值转换为字符串,而是描述了对象本身 。
To get the date of the Firebase timestamp just call toDate()
on it. 要获取Firebase时间戳的日期,只需对其调用toDate()
。
For more info check this out 有关更多信息, 请查看此
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.