As of April 12th, you must go to Progress SupportLink to create new support cases or to access existing cases. Please, bookmark the SupportLink URL and use the new portal to contact the support team.
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
G
Gary
said
almost 8 years ago
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);
}
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);
}
Damien Bell
said
almost 8 years ago
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.
fabreax
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
I wrote methods to parse the timestamp to unix time in ms and back:
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstGary
I wrote methods to parse the timestamp to unix time in ms and back:
Damien Bell
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.
fabreax
Thanks a lot, it's perfect !
-
Can I add KinveyReferences and other custom Arrays to the User entity?
-
How can I use custom enums within my Entity?
-
Linking an image does not save with LinkedData
-
How can I get all records saved by the current user?
-
Why don't all users see the same data when querying?
-
Retrieving related file from datastore collection - continued
-
Problem implementing sign up / login
-
Can Android API be used for Java?
-
I'm not receiving push notifications
-
It is impossible to receive object according to the link
See all 259 topics