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

I am creating a class that implements 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 to write this attribute in the next method that implements Parcelable, because out.writeDateTime (_DataHora) is impossible.

@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();
}
+4
source share
3 answers

You can get milliseconds with 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());
+7
source

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

To write on the package:

dest.writeString(mDateTime.toString());

Read from package:

mDateTime = DateTime.parse(in.readString());
+2
source

:

:

out.writeSerializable(myDateObject)

:

myDateObject =  in.readSerializable() as Date
0

Source: https://habr.com/ru/post/1540427/


All Articles