从 Parcelable 类读取和写入 java.util.Date
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21017404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading and writing java.util.Date from Parcelable class
提问by Mesut
I'm working with Parcelable class. How can I read and write java.util.Date
object to and from this class?
我正在使用 Parcelable 类。我如何java.util.Date
从这个类读取和写入对象?
采纳答案by Pankaj Kumar
Use writeSerializablewhere Date is Serializable. (But not a good idea. See below for another better way)
使用writeSerializable其中 Date 是可序列化的。(但不是一个好主意。另一种更好的方法见下文)
@Override
public void writeToParcel(Parcel out, int flags) {
// Write object
out.writeSerializable(date_object);
}
private void readFromParcel(Parcel in) {
// Read object
date_object = (java.util.Date) in.readSerializable();
}
But Serializing operations consume much performance. How can overcome this?
但是序列化操作会消耗很多性能。怎样才能克服这个?
So better use is to convert date into Long while writing, and read Long and pass to Date constructor to get Date. See below code
所以更好的用法是在写入时将日期转换为Long,然后读取Long并传递给Date构造函数以获取Date。看下面的代码
@Override
public void writeToParcel(Parcel out, int flags) {
// Write long value of Date
out.writeLong(date_object.getTime());
}
private void readFromParcel(Parcel in) {
// Read Long value and convert to date
date_object = new Date(in.readLong());
}
回答by Gopal Gopi
Date
class implements Serializable
...
Date
类实现Serializable
...
so you can write
所以你可以写
parcel.writeSerializable(java.util.Date)
and you can read like
你可以这样读
java.util.Date date = (java.util.Date)parcel.readSerializable();
回答by Satyaki Mukherjee
Try this in this way::
以这种方式试试这个::
for write::
用于写::
yourParse.writeSerializable(YourGivenDate)
for read::
供阅读::
Date myDate = yourParse.readSerializable();
回答by App-SoftwareFactory
Use date.getTime()for get Long format:
使用date.getTime()获取长格式:
public class MiClass implements Parcelable {
Date date;
public MiClass(Date date) {
this.date = date;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(date != null ? date.getTime() : -1);
}
protected MiClass(Parcel in) {
long tmpDate = in.readLong();
this.date = tmpDate == -1 ? null : new Date(tmpDate);
}
public static final Parcelable.Creator<MiClass> CREATOR = new Parcelable.Creator<MiClass>() {
public MiClass createFromParcel(Parcel source) {
return new MiClass(source);
}
public MiClass[] newArray(int size) {
return new MiClass[size];
}
};
}
回答by Pavel Shorokhov
In Kotlinwe may create extension for Parcel - the simplest solution.
在Kotlin 中,我们可以为 Parcel 创建扩展——最简单的解决方案。
fun Parcel.writeDate(date: Date?) {
writeLong(date?.time ?: -1)
}
fun Parcel.readDate(): Date? {
val long = readLong()
return if (long != -1L) Date(long) else null
}
And use it
并使用它
parcel.writeDate(date)
parcel.readDate()
回答by Shyam Kumar
Try this (Kotlin):
试试这个(科特林):
data class DateParcel(val date: Date?):Parcelable {
constructor(parcel: Parcel) : this(parcel.readValue(Date::class.java.classLoader) as? Date
)
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeValue(date)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<DateParcel> {
override fun createFromParcel(parcel: Parcel): DateParcel {
return DateParcel(parcel)
}
override fun newArray(size: Int): Array<DateParcel?> {
return arrayOfNulls(size)
}
}}