Start a new topic
Answered

The best way to store a datetime

Hi,


I'm looking for the best way to store a datetime (timestamp) in Kinvey to allow ordering and querying.


I have seen that you store dates like "2015-06-26T10:03:57.921Z".


Do you have a java code example to store and retrieve dates ?


Thanks


Best Answer

I wrote methods to parse the timestamp to unix time in ms and back:


    /**
     * Parses Kinvey Last Modified Time String into time in unix in milliseconds
     * 
     * @param kinveyLastModifiedTimestampString
     * 
     * @return Kinvey LMT parsed into time in unix in ms or -1 if there was an error
     */
    public static long parseKinveyLastModifiedTimestampToMillis(String kinveyLastModifiedTimestampString){

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

        try {
            Date date = simpleDateFormat.parse(kinveyLastModifiedTimestampString);
            return date.getTime();
        }
        catch (Exception e){

            return -1;
        }
    }

    /**
     * Parse time in unix in milliseconds into Kinvey Last Modified Time string
     * 
     * @param timeMillis
     * 
     * @return Time in unix in ms parsed into Kinvey LMT string
     */
    public static String parseMillisToKinveyLastModifiedTimestamp(long timeMillis){

        Date date = new Date(timeMillis);
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        formatter.setTimeZone(TimeZone.getTimeZone("UTC"));

        return formatter.format(date);
    }

   


Answer

I wrote methods to parse the timestamp to unix time in ms and back:


    /**
     * Parses Kinvey Last Modified Time String into time in unix in milliseconds
     * 
     * @param kinveyLastModifiedTimestampString
     * 
     * @return Kinvey LMT parsed into time in unix in ms or -1 if there was an error
     */
    public static long parseKinveyLastModifiedTimestampToMillis(String kinveyLastModifiedTimestampString){

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

        try {
            Date date = simpleDateFormat.parse(kinveyLastModifiedTimestampString);
            return date.getTime();
        }
        catch (Exception e){

            return -1;
        }
    }

    /**
     * Parse time in unix in milliseconds into Kinvey Last Modified Time string
     * 
     * @param timeMillis
     * 
     * @return Time in unix in ms parsed into Kinvey LMT string
     */
    public static String parseMillisToKinveyLastModifiedTimestamp(long timeMillis){

        Date date = new Date(timeMillis);
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        formatter.setTimeZone(TimeZone.getTimeZone("UTC"));

        return formatter.format(date);
    }

   

Thanks so much for the assist on this one Gary.


I was going to recommend something more similar to the first as it's how I usually attack date-time problems.


Enjoy your afternoon all.

Thanks a lot, it's perfect !

Login or Signup to post a comment