Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write and read org.joda.time.Date in Parcelable class

I am creating a class that Implements the Parcelable,.

public class PosicaoResumoMobile implements Parcelable {

private Float _Latitude;
private Float _Longitude;
private org.joda.time.DateTime _DataHora;
    ...

But this class has an attribute of type org.joda.time.DateTime. How can I write this attribute in the following method implementing the Parcelable since it is not possible out.writeDateTime (_DataHora).

@Override
public void writeToParcel(Parcel out, int flags) 
{
    //TODO: How to write org.joda.time.DateTime 
    out.writeFloat(_Latitude);
    out.writeFloat(_Longitude);
}

and read

private PosicaoResumoMobile(Parcel in){
    //TODO: How to read org.joda.time.DateTime 
    Float latitude = in.readFloat();
    Float longitude = in.readFloat();
}
like image 655
Renan Barbosa Avatar asked May 13 '14 19:05

Renan Barbosa


Video Answer


2 Answers

You should be able to get the milliseconds using getMillis()

// to write to parcel
out.writeFloat(_Latitude);
out.writeLong(jodaDTInstance.getMillis())

// to read from parcel
Float longitude = in.readFloat();
jodaDTInstance = new DateTime(in.readLong());
like image 152
Salem Avatar answered Nov 14 '22 22:11

Salem


Based on @artworkad comment, if you need to include Timezone information, you will have to use toString() and DateTime.parse():

To write to parcel:

dest.writeString(mDateTime.toString());

To read from parcel:

mDateTime = DateTime.parse(in.readString());
like image 44
francisco_ssb Avatar answered Nov 14 '22 21:11

francisco_ssb