Reading and writing java.util.Date from Parcelable class

JavaAndroid

Java Problem Overview


I'm working with Parcelable class. How can I read and write java.util.Date object to and from this class?

Java Solutions


Solution 1 - Java

Use writeSerializable where Date is Serializable. (But not a good idea. See below for another better way)

@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

   @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());
    		
    }

Solution 2 - Java

In Kotlin we may create extension for Parcel - the simplest solution.

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()

Solution 3 - Java

Use date.getTime() for get Long format:

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];
        }
    };
}

Solution 4 - Java

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)
    }
}}

Solution 5 - Java

Date class implements Serializable...

so you can write

parcel.writeSerializable(java.util.Date)

and you can read like

java.util.Date date = (java.util.Date)parcel.readSerializable();

Solution 6 - Java

Try this in this way::

for write::

yourParse.writeSerializable(YourGivenDate)

for read::

Date myDate = yourParse.readSerializable();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMesutView Question on Stackoverflow
Solution 1 - JavaPankaj KumarView Answer on Stackoverflow
Solution 2 - JavaPavel ShorokhovView Answer on Stackoverflow
Solution 3 - JavaApp-SoftwareFactoryView Answer on Stackoverflow
Solution 4 - JavaShyam KumarView Answer on Stackoverflow
Solution 5 - JavaGopal GopiView Answer on Stackoverflow
Solution 6 - JavaSatyaki MukherjeeView Answer on Stackoverflow