Java Simple Date Formatter parsing error - parsing

How to parse "Tue Jun 05 00:00:00 GMT+05:30 2012" text to Date in Java?
I have used format "EEE MMM dd HH:mm:ss z yyyy" But not working.

The trick that you may have missed is that you must set the locale because "Tue" or "Jun" can only be understood in English.
This works :
SimpleDateFormat ft = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
Date dt = ft.parse("Tue Jun 05 00:00:00 GMT+05:30 2012");

Related

Date formatter Mon, 03 May 2021 00:00:00 GMT

I want to convert this String to Date
Mon, 03 May 2021 00:00:00 GMT
let dateString = "Mon, 03 May 2021 00:00:00 GMT"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy hh:mm:ss 'Z'"
date = dateFormatter.date(from: dateString)
but it doesn't work, date is nil. So what is the correct format for this case?
use the date format as like EEE, dd MMM yyyy hh:mm:ss Z
let dateString = "Mon, 03 May 2021 00:00:00 GMT"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy hh:mm:ss Z"
let date = dateFormatter.date(from: dateString)
print(date)
You need to use HH since this is 24h format and since the day and month names are in English you also need to set the locale for the formatter so it doesn't use the users current locale
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"

DateFormatter giving incorrect Result [duplicate]

This question already has an answer here:
NSDateFormatter show wrong year
(1 answer)
Closed 3 years ago.
I try to format my UTC date but the result seems very wrong
DateFormatter:
let df = DateFormatter()
df.calendar = Calendar(identifier: .iso8601)
df.dateFormat = "MMMM dd YYYY hh mm"
df.locale = Locale(identifier: "en_US_POSIX")
df.timeZone = TimeZone(identifier: "UTC")
Example 1
Date 1:
po dateStart
▿ 2018-12-31 00:00:00 +0000
- timeIntervalSinceReferenceDate : 567907200.0
Giving:
po df.string(from: dateStart)
"December 31 2019 12 00"
Example 2
Date 2:
po dateEnd
▿ 2021-01-03 23:59:59 +0000
- timeIntervalSinceReferenceDate : 631411199.0
Giving:
po df.string(from: dateEnd)
"January 03 2020 11 59"
Why does this happen and how can I get the correct Date (Year)?
You should use "MMMM dd yyyy hh mm" instead of "MMMM dd YYYY hh mm" rest of seems ok to me.

Most efficient way to create date formatter for May 23 '17, 12:30

What format i should use for getting the date with '. like May 23 '17, 12:30. Tried MMM dd ' yy hh:mm a so im getting Mar 23, yy hh:mm
Not sure what you mean with most efficient here but to get the result you want use
let formatter = DateFormatter()
formatter.dateFormat = "MMM dd ''yy, hh:mm a"

NSDate, Can't get proper date format

This is the timestamp I am trying to get my app to read: Thu, 03 Mar 2016 16:06:42 GMT
This is the closest I have, but haven't had any success:
[df setDateFormat:#"EEE, dd MMM YYYY HH:mm:ss z"];
EDIT: My current format prints-to/reads: Tue, 02 M02 2016 11:36:57 GMT-5
is there anything I'm missing?
Final Edit:
I should have just posted all my code...
[df setLocale:[NSLocale systemLocale]];
This was messing things up for me.
I believe your year is incorrect. Change your dateformat to
[df setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
See documentation on formatter dates here
I tried to use your date format and convert it to a nsdate and back to a string. It should work with the format string you used.
let time = "Thu, 03 Mar 2016 16:06:42 GMT"
let dateformat = "EEE, dd MMM yyyy HH:mm:ss z"
var dateformatter = NSDateFormatter()
dateformatter.dateFormat = dateformat
dateformatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
var date = dateformatter.dateFromString(time)
var newTime = dateformatter.stringFromDate(date!)
// newTime = "Thu, 03 Mar 2016 16:06:42 GMT"
Edit: I changed the dateformat to use lowercase ys for the year as suggested by Devster101. This is the correct format according to this spezification which is used since iOS 7

How to format NSDateFormatter for this date

How would I format a NSDateFormatter to create a date from the following format?
Sat May 16 13:45:36 2015
The part of Sat May 16 is the part that I am unsure how to do
I know the last part would be HH:mm:ss yyyy
let dateString = "Sat May 16 13:45:36 2015"
let df = NSDateFormatter()
df.dateFormat = "E MMM dd HH:mm:ss yyyy"
df.dateFromString(dateString) // "May 16, 2015, 1:45 PM"
You should take a look at this answer also.

Resources