2

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

Infiniti Fizz
  • 1,726
  • 4
  • 24
  • 40

1 Answers1

8

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • great answer (+1). I immediately stopped working on mine when this one appeared. – Sean Patrick Floyd Nov 12 '10 at 17:45
  • I'm not sure I understand you. I want to get a date from a string, not a string from a date, that's why I'm using parse() not format(). – Infiniti Fizz Nov 12 '10 at 17:47
  • Ah sorry, I think I understand! So because I am just using System.out.println(dateFromString) it's using the default toString() rather than the formatted. So as a full piece of code, I'm getting a String (of the date in the format I want) parsing it into a date that I want to use but that's not in the format I want, then using format() to take it from the default format into the format I want? Edit: "...format into the format I want for displaying it?" – Infiniti Fizz Nov 12 '10 at 17:50
  • Hmm still got a problem. The real reason for doing this was the compare the dates, see if one is before or after the other using date1.after(date2) etc. I have tested what you said by getting the lastModified() long from a File and the parsed date from the string.getTime() but they are different times, for exactly the same date, the longs are the same up until the last 3 digits as in date1: 1289504332671, date2: 1289504332000. So the comparisons with before() and after() still aren't working :( – Infiniti Fizz Nov 12 '10 at 17:55
  • I've added the information from that last comment to the question now. Thanks for the help so far BalusC but your solution isn't working for me. Don't think I'm not grateful though :-) – Infiniti Fizz Nov 12 '10 at 18:04
  • Oh lol, I just came back here to give you a tick and tell you I'd sorted it out. I just worked out it gave millisecond precision so was about to say so and that it was my fault it wasn't working, nothing to do with your answer. If only StackOverflow notified me about answer edits as well as new answers and comments! I could have saved myself about 2 hours messing around with Calendar, Date and DateFormat! Thanks for the really quick responses, just too bad I didn't look at them as quickly. Thanks again. – Infiniti Fizz Nov 15 '10 at 00:30
  • I decided I don't need to divide by 1000 because it wouldn't make any difference to the result. Eg, someone requests a page at 18:30:30:500 and the page was modified at say 18:30:30:700 then dividing and comparing would say they were the same time, so it would make sense to tell the client the page was new just in case. As the time from the HTTP has ms at 000, if the seconds are equal, we would assume the page was modified during the second and tell the client the page is new also (if the ms Were equal then I would send new too). Just thought I'd include this for anyone else pondering it too. – Infiniti Fizz Nov 15 '10 at 00:39