The following test fails:
DateFormat df = new SimpleDateFormat("HH:mm:ss z");
assertEquals("00:00:00 GMT", df.format(new Date(0)));
expected "00:00:00 GMT" but was "01:00:00 GMT"
Could someone point out where I'm being stupid please?
I've spent longer looking at this than is would have taken me to just replace everything with Joda-Time. There's a lesson there somewhere.
Formatting Dates String pattern = "yyyy-MM-dd"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); String date = simpleDateFormat. format(new Date()); System. out. println(date);
The java SimpleDateFormat allows construction of arbitrary non-localized formats. The java DateFormat allows construction of three localized formats each for dates and times, via its factory methods.
parseLong(epochTime); //convert seconds to milliseconds Date date = new Date(unix_seconds * 1000 L); // format of the date SimpleDateFormat jdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String java_date = jdf. format(date); //System. out. println("\n" + java_date + "\n"); IDataUtil.
Java SimpleDateFormat with Locale String pattern = "EEEEE MMMMM yyyy HH:mm:ss. SSSZ"; SimpleDateFormat simpleDateFormat =new SimpleDateFormat(pattern, new Locale("fr", "FR")); String date = simpleDateFormat. format(new Date()); System.
The problem is that Java has a bug around the name of the Europe/London time zone abbreviation in 1970.
In the winter of 1970, the UK was still on UTC+1 - but Java believes it's still called "GMT". So what you're seeing is the local time at midnight UTC on January 1st 1970... it's just that we're used to GMT=UTC, which is why it's confusing.
(As a side note, it would still be worth converting to Joda Time even now. Avoid the built-in libraries like the plague :)
Joda Time prints the time zone abbreviation as "BST". This seems equally bizarre given that it clearly wasn't summer time... but it's possible that the "S" here standards for Standard, as that was during the period of the "British Standard Time experiment". See Wikipedia for details.
(Note that UTC itself didn't even exist at the Unix epoch - it was introduced in 1972. I'm assuming a proleptic UTC for the sake of this answer :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With