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);
Related
I am receiving a timestamp like : 07/23/2019.08.45 from a system.
I need to convert this to Epoch.
I was not able to parse this format by using SimpleDateFormat.
I tried:
SimpleDateFormat SDF = new SimpleDateFormat("MM/dd/yyyy.HH:mm");
Date dateInstance =SDF.parse("07/23/2019.08.45");
but it didn't work
I also tried to split and parse the time and date but still got the unable to parse error
Can someone please help.
What would be the most efficient way to get this done.
You are not providing the correct date format:
SimpleDateFormat SDF = new SimpleDateFormat("MM/dd/yyyy.HH:mm");
Date dateInstance =SDF.parse("07/23/2019.08:45");
Notice : instead of .
I am developing a spring application and in one of my controller i have following lines to parse from string to date and format the parsed date to required format. But again i need to parse back formatted string into date without using any SimpleDateFormat, so is it possible to do so ?
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
Date pick=dateFormat.parse(request.getParameter("pickDate"));
String pick_date=dateFormat2.format(pick);
Edit:
I found in the wikipedia that china has the locale yyyy-MM-dd. check this reference date format by country
set locale to China you'll get the required date format
Try this
String d1="12-27-2010";
Stirng[] splitdata=d1.split("-");
int month=Integer.parseInt(splitdata[0]);
int day=Integer.parseInt(splitdata[1]);
int year=Integer.parseInt(splitdata[2]);
Calender cal=Calender.getInstance(Locale.CHINA);
cal.set(year,month,day);
Date d=cal.getTime();
This should work
If you know your data format you can do that. by using simple string operations
Ex:
if your data format is
MM-dd-yyyy
then you can convert to yyyy-MM-dd like this
String d1="12-27-2010";
Stirng[] splitdata=d1.split("-");
String s2= splitdate[2]+"-"+splitdate[0]+"-"+splitdate[1];
You can use Concatenation Operator(+) for that
Yes possible; just write the code to do the parsing. Should not be that difficult ...
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)
I need to convert a SQL Server timestamp format (stored on Timestamp java data type) into this format yyyy-mm-ddThh:mm:ss.sssZ (it's the date format parsed by Alfresco)
How can I do it?
thanks for your time!
Andrea
Try this SimpleDateFormat:
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" );
String yourformattedDate = sdf.format(yourDate);
You could try and get the time (in ms) from your TimeStamp instance, create a Date instance and use the DateFormatter class to format it anyway you want. That is, assuming DateFormatter supports the format you want.