I am using an angularjs date time picker that when used directly is in the format of:
2016-01-18T05:00:00:000Z
this angularjs format works for me, but later I store it off and when retrieved it is in the format of:
Mon Jan 18 2016 00:00:00 GMT-0500 (Eastern Standard Time)
Which does not work for me. So I need to take the non-working String representation of the date (second one), and convert it into my working representation (first one). I would imagine I have to convert it like String > Date > Diff String, but looking at SimpleDateFormat I don't even see where the T comes from in 2016-01-18T05:00:00:000Z
Your first format is ISO 8601 Time zone. Try use the pattern yyyy-MM-dd'T'HH:mm:ss.SSSZ.
Related
I am trying to get my current system date and am formatting it into Etc/UTC and then into this "dd.MM.yyyy HH:mm z" format. The problem is this that the format function after formatting the date returns a string instead of date. Here is the code spinet below
final Date currentDate = new Date();
final SimpleDateFormat dateFormatter = new SimpleDateFormat("dd.MM.yyyy HH:mm z");
dateFormatter.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String finalDateTime= dateFormatter.format(currentDate);
System.out.println(finalDateTime);
Is there any alternative solution which allows me to format the date by keeping me within this date object because I have researched every date library after java 8 and before java 8, it seems like if I want to format any type of date or dateTime, I have to use a formatter which converts the given date into string.
Any Alternative solutions or it is not possible?
Date represents a single point in time. That's it, nothing more, nothing less. It does not contain any information about time zones.
Wed Jun 06 12:38:15 BST 2018 is the same Date (instant) as Wed Jun 06 11:38:15 GMT 2018, just in a different time zone. It's like the words "humans" and "homo sapians". They refer to the same species, they are just kind of in a different "format".
So you don't need to change the date in any way. You just need to format it differently. This is why formatters return Strings. Only Strings can represent one particular format of a date.
I've been searching all over and just can't find a explanation or reason why this is happening but the parse(String) method of DateFormat just isn't parsing my String correctly.
I'm trying to parse a String into the date format that is used for HTTP headers and got as far as getting the String on its own such as:
Thu, 11 Nov 2010 18:34:22 GMT
Which is in the format:
E, d MMM yyyy HH:mm:ss z
But when I use df.parse(dateStr); this is what I get out of it:
Thu Nov 11 18:34:22 GMT 2010
Which is nothing like what I wanted, why is the year now after the GMT? Why is there no comma anymore? And why is the date after the month?
I'm completely confused about this now and can't find a solution but I really need the date to be in that format. Is the comma messing things up? or the colons?
Thanks for your time,
Infinitifizz
P.S.
Forgot to mention this but I've tried dateformat.setLenient(false) and it makes no difference.
P.P.S
I'm trying to do this to compare the dates with date1.before(date2) and after() etc to see if one is newer than the other but I can't do this because the parsing isn't working.
Even though they look the same but just the format is different, they are not the same because after calling getTime() on both of them (When I have provided 2 identical dates) the longs are not the same. As in the date is:
Thu, 11 Nov 2010 19:38:52 GMT for a lastModified() on a File
If I input the String "Thu, 11 Nov 2010 19:38:52 GMT" and then compare their longs once converting the string to a date using parse() and then calling getTime() on that date I get:
lastModified = 1289504332671
fromString = 1289504332000
It is only the last 3 digits that are different, does this have any significance?
Thanks again for your time and sorry I didn't put this bit in first,
Infinitifizz
The result format is the default format of Date#toString() (click link to see the javadoc). You're apparently doing a System.out.println(date). You would like to use SimpleDateFormat#format() instead with another pattern to format it in the desired format. E.g.
String newDateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
Update: You shouldn't care about the format when using Date. You should only care about the format at that point when Date is to be converted (displayed) as String. As to the difference in timestamps, the Date uses millisecond precision for the timestamp while HTTP header uses second precision. You'd like to divide the timestamps by 1000 before comparing.
I'm trying to pass a Date-Object to a SOAP-API and need the output of the date-object itself to be yyyy-MM-dd
I'm already converting my string into a date-object like this:
// String __startDatum = "2013-02-05";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date convertedDate = dateFormat.parse(__startDatum);
For now the output of convertedDate will be Tue Feb 05 00:00:00 MEZ 2013.
How could I change the output of convertedDate to be 2013-02-05?
Please keep in mind I still need it to be a date-object not a string!
For now the output of convertedDate will be Tue Feb 05 00:00:00 MEZ 2013.
There's no "output of convertedDate" - it's just a Date variable. The only way to get "Tue Feb 05 00:00:00 MEZ 2013" would be to call toString() on it, either implicitly or explicitly - and you can't change the format used by Date.toString().
It's important to understand that a Date is just a number of milliseconds since the Unix epoch. It doesn't have a time zone; it doesn't have a calendar system; it doesn't have a particular format.
If you want a better API which allows you to create an object which just represents a date (rather than a date/time) you should look at Joda Time which is a far nicer date and time API than the built-in one. It's reasonably large - primarily due to the time zone data, I believe - so you may want to look for a cut down version tailored to Android. It's mostly a pleasure to work with though - at least compared with Date and Calendar.
I am facing an issue with parsing/converting a dateString from browser to Date format in Java.
The following are some of the values that I got from the browser.
dateStr = "01-01-2010 05:06:22";
dateStr = "Mon Oct 11 07:00:00 EDT 2010";
dateStr = "Fri May 25 2012 08:00:00 GMT-0400 (Eastern Daylight Time)";
Each dateStr is in different format and at the back-end I was using a SimpleDateFormat with a particular format to convert dateStr. Since the input dateStr format is not consistent, any idea about how this can be handled in the backend?
There is little access to the frontend code/folks.
PS: I did google about this and read the related topics here on StackOverflow.
Any sort of help is greatly appreciated. Thank you.
You have all the pieces you need. First define patterns for every possible date format you can get from the client. Then simply iterate over these formats and try parsing input using each of them and SimpleDateFormat.
Continue looping if SimpleDateFormat returns null (parse error) until you get valid Date. You might ask: what to do if input matches more then one pattern (is 02-01-2012 2nd of January or February the 1st?) Well, then you have bigger issues...
I've been searching all over and just can't find a explanation or reason why this is happening but the parse(String) method of DateFormat just isn't parsing my String correctly.
I'm trying to parse a String into the date format that is used for HTTP headers and got as far as getting the String on its own such as:
Thu, 11 Nov 2010 18:34:22 GMT
Which is in the format:
E, d MMM yyyy HH:mm:ss z
But when I use df.parse(dateStr); this is what I get out of it:
Thu Nov 11 18:34:22 GMT 2010
Which is nothing like what I wanted, why is the year now after the GMT? Why is there no comma anymore? And why is the date after the month?
I'm completely confused about this now and can't find a solution but I really need the date to be in that format. Is the comma messing things up? or the colons?
Thanks for your time,
Infinitifizz
P.S.
Forgot to mention this but I've tried dateformat.setLenient(false) and it makes no difference.
P.P.S
I'm trying to do this to compare the dates with date1.before(date2) and after() etc to see if one is newer than the other but I can't do this because the parsing isn't working.
Even though they look the same but just the format is different, they are not the same because after calling getTime() on both of them (When I have provided 2 identical dates) the longs are not the same. As in the date is:
Thu, 11 Nov 2010 19:38:52 GMT for a lastModified() on a File
If I input the String "Thu, 11 Nov 2010 19:38:52 GMT" and then compare their longs once converting the string to a date using parse() and then calling getTime() on that date I get:
lastModified = 1289504332671
fromString = 1289504332000
It is only the last 3 digits that are different, does this have any significance?
Thanks again for your time and sorry I didn't put this bit in first,
Infinitifizz
The result format is the default format of Date#toString() (click link to see the javadoc). You're apparently doing a System.out.println(date). You would like to use SimpleDateFormat#format() instead with another pattern to format it in the desired format. E.g.
String newDateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
Update: You shouldn't care about the format when using Date. You should only care about the format at that point when Date is to be converted (displayed) as String. As to the difference in timestamps, the Date uses millisecond precision for the timestamp while HTTP header uses second precision. You'd like to divide the timestamps by 1000 before comparing.