3

My timestamp from javascript is this:

 2013-08-11T19:13:20.000Z

I need to convert this into Java.Date format so that I can do a bubble sort to figure out which is the oldest element from the database which is the newest one. How do I go about doing this?

I've a PostgresDb in my server, serving me data with this time stamp using Javascript. On my Android phone, I need to not only show new data, but also the old data, depending upon how the user scrolls. Both data will be retrieved based on how the user wanting it. Now everytime a user calls for new data, the data first gets added to SQLite DB in Android and then gets retrieved from there to be displayed.

The thing is any "new" data, irrespective of its time stamp will be stored sequentially. But I need to show in order, based on time. Hence the problem.

2
  • is there any possiblity to get long instead of string ? from js Commented Aug 12, 2013 at 13:08
  • 2
    I need to convert this into Java.Date format so that I can do a bubble sort to figure out which is the oldest element from the database which is the newest one Have you considered to use the ORDER BY statement inside the query instead of ordering data yourself with java? Commented Aug 12, 2013 at 13:11

5 Answers 5

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

Date date = format.parse("2013-08-11T19:13:20.000Z");

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

2

I think this is the best way to parse this date

javax.xml.bind.DatatypeConverter.parseDateTime("2013-08-11T19:13:20.000Z");

this parser is specialized for parsing only this date format and should be very efficient

Comments

1

If it is a valid format string, pass it as a parameter to date object, and then use getTime to get the timestamp,which you can pass to your java code to convert into java date.

var tDate = new Date("2013-08-11T19:13:20.000Z");
alert(tDate.getTime());

or you can directly pass the string to your Java code and do all the conversions there.

Comments

1

I need to convert this into Java.Date format so that I can do a bubble sort to figure out which is the oldest element from the database which is the newest one

You don't really need that. If you stored the date in the database you can order your data in the query:

SELECT yourFields FROM yourTable ORDER BY yourDate DESC

Then with java you can use

yourResultSet.first();

To move the cursor to the first element (the newest one) and

yourResultSet.last();

To move the cursor to the last element (the oldest one)

3 Comments

which will cause me a tiny problem, as the data I get from my database, irrspective of what I do, I've to store it in a local sqllite database in Android. And then retrieve data from it to display. It will be difficult to show which is the new data and which are the old, if I don't sort it by Time at the client end.
@Hick I don't get the point. What is the problem with getting the data ordered with the query? SQL and SQLite do support the ORDER BY statement
@Hick The thing is any "new" data, irrespective of its time stamp will be stored sequentially. But I need to show in order, based on time. Hence the problem. Store in the SQLite db also the date (or the timestamp), and you can use that query
0

Joda-Time is a great library or dealing with all the warts of the Java time APIs, and ISODateTimeFormat is probably what you're looking for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.