admin管理员组

文章数量:1287773

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.

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.

Share Improve this question edited Aug 12, 2013 at 13:41 Hick asked Aug 12, 2013 at 13:05 HickHick 36.4k47 gold badges160 silver badges250 bronze badges 2
  • is there any possiblity to get long instead of string ? from js – Suresh Atta 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? – BackSlash Commented Aug 12, 2013 at 13:11
Add a ment  | 

5 Answers 5

Reset to default 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");

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

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.

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)

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.

本文标签: How to convert date in Javascript to Java DateStack Overflow