I'm trying to parse a date that i get from JavaScript script evaluated with rhino library into java.util.Date, can i convert a org.mozilla.javascript.NativeDate into a java.util.Date ?
If convert NativeDate into a string with the Context.tostring method i get a date in the following format :
Wed Oct 12 2011 16:17:59 GMT+0200 (CEST)
How can i parse this string date representation in to a java.util.Date object ?
In Rhino use
context.jsToJava(nativeDateObj, Date.class);
Bvesco's answer works well. However doing this the other way round (java to js) is not entirely as simple - Context.javaTojs() does not work for dates. I eventually found the solution here - use the javascript constructor:
Object js = context.newObject(scope, "Date", new Object[] {date.getTime()});
The above post also mentioned the following alternative to convert a date from js to java (I haven't confirmed this):
Date date = new Date((long) ScriptRuntime.toNumber(s));
Have you tried;?
java.sql.Date.valueOf("date string");
Related
Okay, so here's my issue in Android right now. On our Database there's a timestamp in this format 8/15/2013 2:00:48 PM and through a .NET WebService I get that same time like this in Android: 2013-08-15T14:00:48-07:00. Now I want to convert this format into a Date Time format that I can use for comparison (for example this webservice provides every instance where a device failed at logging in so we want to check the amount of time between occurances to see if there's any issues). Below I have this code where I'm trying to use JODA Time but it's still not returning the correct format:
public static Date convertStringToDate(String input) {
String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern);
DateTime dateTime = formatter.parseDateTime(input);
return dateTime.toDate();
//printout shows: Thu Aug 15 17:00:48 EDT 2013
}
I know that the server is returning some crappy time format that is hard to work with (it took a while to get this to work in the iOS App we have, and even there it's still rather clunky) so I don't mind changing the webservice or the query if that would make things easier.
I have a very similar format, and I parse it using SimpleDateFormat, try this:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZ", Locale.US);
Date dateTime = format .parse(value);
What i understand is that you have your correct instance of date already and what you need is to parse it to String.
I suggest you use:
DateFormat formatter = new SimpleDateFormat("d/MM/yyyy hh:mm:ss a");
//this will give you the format '8/15/2013 2:00:48 PM'
String d = formatter.format(date);
Hope this helps.
EDIT:
Also seams you want to have your date instance in -07:00 timezone
So you can change your line
DateTime dateTime = formatter.parseDateTime(input);
for
DateTime dateTime = formatter.withZone(DateTimeZone.forID("-07:00")).parseDateTime(input);
I want to parse below date in Java,
2012-11-29T09:15:00.002-08:00
Which date format I have to use to parse it?
java.text.SimpleDateFormat will parse it.
Actually, this format is an XSD date format, and the simplest way to parse it (without any use of an external library) is to use DataTypeConverter.parseDateTime(String lexicalXSDDateTime) in the javax.xml.bind package. This will return you a java.util.Calendar object, which you can retrieve a Date using Calendar.getTime().
Alternatively, there are solutions on SO that speaks about the same formatting, such as: What's the best way to parse an XML dateTime in Java?
enter link description here.
I hope this helps.
Using Date format as "yyyy-MM-dd'T'HH:mm:ss.SSSz"
E.g.:
String string = "2012-11-29T09:15:00.002-08:00";
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssSSSz", Locale.ENGLISH).parse(string);
System.out.println(date);
Is there a date formatting tool for Atom Dates.
According to this link:
https://www.rfc-editor.org/rfc/rfc4287
Such date values happen to be compatible with the following
specifications: [ISO.8601.1988], [W3C.NOTE-datetime-19980827], and
[W3C.REC-xmlschema-2-20041028].
Example Date constructs:
<updated>2003-12-13T18:30:02Z</updated>
<updated>2003-12-13T18:30:02.25Z</updated>
<updated>2003-12-13T18:30:02+01:00</updated>
<updated>2003-12-13T18:30:02.25+01:00</updated>
I tried to use Joda ISODateTimeFormat.dateTime(); but it seems it doesn't handle the parsing when there is no milliseconds (2003-12-13T18:30:02Z for exemple).
What is the simplest way to parse all these date formats?
This is ISO 8601 format, the standard format used in for example XML. Joda Time supports this format very well, you can just pass these strings to the constructor of DateTime:
DateTime timestamp = new DateTime("2003-12-13T18:30:02Z");
Works without any problems, also if there are no milliseconds in the string.
It seems to be xml dateTime. Then the best choice is javax.xml.datatype.XMLGregorianCalendar.
DatatypeFactory f = DatatypeFactory.newInstance();
XMLGregorianCalendar xgc = f.newXMLGregorianCalendar("2003-12-13T18:30:02.25Z");
System.out.println(xgc);
System.out.println(xgc.toGregorianCalendar().getTime());
output
2003-12-13T18:30:02.25Z
Sat Dec 13 20:30:02 EET 2003
See more in API
DateUtils from Apache Commons / Lang has a parseDate method that supports multiple patterns. That may work for you. (The patterns must be formatted according to the SimpleDateFormat syntax)
Im having a little issue with parsing json date.
Here is what I would like to parse:
{"driver": "247","firstName": "XXXXX","lastName": "XXXXX","lastLatitudeUpdate": "5/21/2012 4:49:17 PM","suspended": "false","checkedin": "0"}
I am having trouble parsing "lastLatitudeUpdate" is it because there are spaces in between? Thanks in advance for the help.
Assuming you are on Android and therefore working with java (yes you don't mention that, only the tag in your question suggests it...)
Like mentioned here (and in various other places) you can parse a date in java using the SimpleDateFormat class:
SimpleDateFormat parserSDF=new SimpleDateFormat("M/d/yyyy h:m:s a");
Date d = parserSDF.parse(dateField,0);
Of course you have to first parse you json input with some library (e.g. standard library from json.org or Google gson) and then parse the string you'll get there for the field into a date.
Short answer: No, there is no way for the JSON engine to recognize a string as a Date object.
Long answer:
There is no 'date' type in JSON. However, this JSON is fine, the catch is that lastLatitudeUpdate will be parsed as a string. In order to convert this to a date you should try something like
var my_object= JSON.parse({"driver": "247","firstName": "XXXXX","lastName": "XXXXX","lastLatitudeUpdate": "5/21/2012 4:49:17 PM","suspended": "false","checkedin": "0"});
my_object.lastLatitudeUpdate= Date.parse(my_object.lastLatitudeUpdate)
This function will give a timestamp. However, you have to make sure the string is correctly recognized, you may have to do some extra work.
Some links for hints
http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
http://www.java-samples.com/showtutorial.php?tutorialid=406
How are you parsing the date? In Chrome this seems to work fine:
new Date("5/21/2012 4:49:17 PM");
Mon May 21 2012 16:49:17 GMT-0400 (US Eastern Daylight Time)
hi can any one please help me on how to convert from 10/21/2009 to Wed Oct 21 18:48:12 UTC+0530 2009 in my java class
Quite frankly, you can't convert 10/21/2009 to Wed Oct 21 18:48:12 UTC+0530 for the simple reason that the first date does contain neither time nor timezone information.
String dateString = new SimpleDateFormat("dd/MM/yyyy").parse(d).toString();
Here you'll get more information: https://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html.
You don't specify if the original value (10/21/2009) is a date object or a string, I'm going to assume that it is in fact a String, so you first need to parse the string and convert it to a date object using the SimpleDateFormatter class
import java.text.SimpleDateFormatter;
SimpleDateFormatter sdf = new SimpleDateFormatter("MM/dd/yyyy");
Date d = sdf.parse("10/21/2009");
Once you have a date object, you can just call toString() on it which will get you the string you want, or create another SimpleDateFormatter object with the pattern that you want (check the javadocs) and then call format().
There's a couple of things you should be aware of regarding the DateFormatter classes available in the JDK
If the string can not be parsed, the parse method will throw an exception, you will need to handle the exception accordingly
The date formatter classes available in the JDK are not threadsafe. Don't create a private/protected/public member variable of these types, instead create the formatters/parsers in the methods where you need them