简体   繁体   中英

Best way to store dates in Google App Engine targeted for mobile apps

What is the best way to store dates in Google App Engine as a back-end for both iOS and Android mobile apps?

In Android the way I like to store dates is using:

System.currentTimeMillis()

This makes it easy to put that date into a local SQLite database via a content provider.

This leads me to believe I should also store milliseconds as the date in all entities on Google App Engine. However, some code I have seen for Google App Engine stores dates using:

import java.util.date;

private Date createdDate;

In the constructor..:

this.createdDate = new Date();

This generates a date string which looks nice in Google App Engine's console:

在此处输入图片说明

However, I feel I should probably be storing all dates in Google App Engine in Long milliseconds so that everything is consistent.

Would it be a good idea to use only milliseconds in Google App Engine, Android and iOS to store dates so that everything is consistent across the board? Or is there another way to do this?

There is no significant difference between these two options as you can always convert Date into milliseconds, and milliseconds into Date. Storing and passing dates as Long values takes less space, but these days the difference is negligible.

1 - I use this function to send dates on json requests to server (Android to GAE):

public static String GetUtcDate()
{
    SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));

    //Time in GMT
    return dateFormatGmt.format(new Date());
}

2 - I receive strings from requests on java servlets and then I use this function to save dates on GAE entities:

public static Date GetDate(String date)
{
    try
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return sdf.parse(date);
    }
    catch (Exception e)
    {
        return new Date();
    }
}

3 - I retrieve the dates from GAE Datastore like this:

(Date) myEntity.getProperty("PropertyName");

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM