I'm working with Parcelable class. How can I read and write java.util.Date
object to and from this class?
Asked
Active
Viewed 3.4k times
6 Answers
187
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());
}

Pankaj Kumar
- 81,967
- 29
- 167
- 186
-
3But Serializing operations consume much performance. How can overcome this? – Mesut Jan 09 '14 at 10:26
-
1You really should do it using longs as Serializable is not good for performance, which is noticeable on older devices. You may also want to look at Joda as it contains lots of useful tools and has a DateTime object that I often find more useful. – Graham Smith Jan 09 '14 at 10:32
-
Now is ok. I prefer to use long instead of serializing. – Mesut Jan 09 '14 at 10:36
-
Remember take care of null dates – Victor Pinto Sep 26 '14 at 16:58
-
1How to take care of null? – Vlado Pandžić May 18 '17 at 11:50
-
@PankajKumar what if there's actually a `Long` field in the class won't the parcel read it instead of the date becase I have such scenario. – Jocky Doe Feb 05 '18 at 22:28
-
1@JockyDoe It will not. Sequence of fields while parcelling and un-parcelling matters. So check if you are using same order for both fields. – Pankaj Kumar Feb 06 '18 at 01:47
-
Do you know how to do the same but for LocalDate/LocalTime objects? the above method does not work for these types – d0rf47 Oct 23 '20 at 01:22
19
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()

Pavel Shorokhov
- 4,485
- 1
- 35
- 44
15
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];
}
};
}

Martijn Pieters
- 1,048,767
- 296
- 4,058
- 3,343

App-SoftwareFactory
- 235
- 3
- 3
-
This is a good solution, but will not work properly for dates before 1/1/1970. -1L is actually a valid value to represent one millisecond before 1/1/1970. So I would rather use a value like Long.MIN_VALUE instead for better safety. – BladeCoder Jul 14 '17 at 13:47
2
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)
}
}}

Shyam Kumar
- 909
- 11
- 12
1
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();

Gopal Gopi
- 11,101
- 1
- 30
- 43
-
1While this will work I suggest users of SO takea look at the answer using longs. – Graham Smith Jan 09 '14 at 10:39
0
Try this in this way::
for write::
yourParse.writeSerializable(YourGivenDate)
for read::
Date myDate = yourParse.readSerializable();

Satyaki Mukherjee
- 2,857
- 1
- 22
- 26