I am localizing my date strings, but having issues.
I expect UK locales to have dayOfTheMonth then Month while US ones to have Month then DayOfTheMonth.
It seems to be the case for getMediumDateFormat but not for getDateFormat.
If I use:
android.text.format.DateFormat.getMediumDateFormat
then I get:
UK: 25 Nov 2015
US: Nov 25, 2015
Which is what I expect (day and month are in reverse order)
However, if I use:
android.text.format.DateFormat.getDateFormat
Then I get:
UK and US: 25/11/2015
In both cases the format is: dd/MM/yyyy
Did I miss something here?
Is there a better way to get a localized short date format?
Related
I had a situation where the Java runtime returned sort of "inverted" millisecond values when reading dates from the database (in java.sql.Date). The millisecond value was approximately the same amount of days, but counted backwards from year 0.
The problem was solved by just restarting the Java runtime.
But: I found out that Java handles these "inverted" values almost correctly except of the week day.
When you run the following code:
System.out.println(new java.util.Date(253402214400000l));
System.out.println(new java.util.Date(-377648784000000l));
You will get the following output:
Fri Dec 31 01:00:00 CET 9999
Tue Dec 31 01:00:00 CET 9999
Another example:
System.out.println(new java.util.Date(-294192000000l));
System.out.println(new java.util.Date(-123967324800000l));
Result:
Mon Sep 05 01:00:00 CET 1960
Mon Sep 05 01:00:00 CET 1960
When using online converters, the result will be different for the particular second line. It will result in a negative date (the year is negative) close to the real, positive date:
Example1:
253402214400000 = Fri Dec 31 9999 01:00:00
-377648784000000 = Tue Oct 15 -9998 02:00:00
Example 2:
-294192000000 = Mon Sep 05 1960 02:00:00
-123967324800000 = Mon Aug 19 -1959 02:00:00
I have not found any information about this "topic".
So, what's the myth behind "inverted" dates? Why does Java handle them almost correctly? And what is the sense of a JDBC ResultSet returning "inverted" millisecond values when calling resultSet.getDate(1).getTime()?
When you are passing a negative number in the Date constructor then it is considered as number of milleseconds before 1/1/1970. The Javadoc says:
date - milliseconds since January 1, 1970, 00:00:00 GMT not to exceed
the milliseconds representation for the year 8099. A negative number
indicates the number of milliseconds before January 1, 1970,
You can see the result which you get when you try to provide the Long.MIN_VALUE and Long.MAX_VALUE in the Date constructor.
DateFormat df = new SimpleDateFormat("d MMM yyyy G, HH:mm:ss.S Z");
System.out.println(df.format(new Date(Long.MIN_VALUE)));
System.out.println(df.format(new Date(Long.MAX_VALUE)));
Ideone Demo
I found out that Java handles these "inverted" values almost correctly except of the week day.
In your first example, the two dates are not the same - one is BC and the other one AD (which explains why the day of the week is different):
Date d1 = new Date(253402214400000l);
Date d2 = new Date(-377648784000000l);
DateFormat fmt = new SimpleDateFormat("yyyy G");
System.out.println(fmt.format(d1)); //9999 AD
System.out.println(fmt.format(d2)); //9999 BC
So your observation is just a coincidence (however there may be a date formatter somewhere that has gone wild and negates the years or the years may actually be negative in your database).
The difference with online converters is probably due to how the year 0 is taken into account and/or variations in the calendar used for the calculations.
Please refer to the simple code below. I want to deduct one month from current month and expecting value of February 2015 in output.
Calendar today = Calendar.getInstance();
System.out.println(today.getTime().toString());
today.set(Calendar.MONTH, -1);
Date date = today.getTime();
System.out.println(date.toString());
The program output is as below. I don't see the month February as expected, instead is shows Dec 2014 as month.
Tue Mar 31 11:48:34 EDT 2015
Wed Dec 31 11:48:34 EST 2014
Thanks for your time and help.
You're setting the month number to be -1, i.e. "December of the year before". You want to add -1 month:
today.add(Calendar.MONTH, -1);
I'd strongly recommend using Joda Time or Java 8's java.time package if you possibly can though - both are much nicer APIs than Date/Calendar.
I currently have two Calendar instances that are set to specific dates and times, they are:
Fri 14th March 2015 - 09:00:00 GMT
Mon 17th March 2015 - 10:30:15 GMT
What i'd like is a way to get the difference in days, hours, minutes and seconds, in a Day:HH:MM:SS format. Note the dates can be in different timezones. So in this example, the answer should be:
3:1:30:15
Any idea how I can achieve this?
I'm using Java 7 and Clojure 1.4 for this.
I'm writing up some database tests in Clojure for a table that contains Date objects, using OracleDB over JDBC.
I need to compare the date I receive (a Date object) with a String - so presumably I need to convert that string to a Date object. After some googling, I found Java's SimpleDateFormat.
This is what I use (with extra stuff for debugging)
(defn parseDate [date]
(do (debug (str "Parsing date: " date ))
(let [ dateobj (java.text.SimpleDateFormat. "dd-MMM-YY")
parsed (do (. dateobj setLenient false) (. dateobj parse date))]
(debug (str "Result: " parsed)) parsed)))
I throw in some dates, and I get the following output..
Parsing date: 01-jan-12
Result: Mon Jan 02 00:00:00 GMT 2012
Parsing date: 01-jan-13
Result: Mon Dec 31 00:00:00 GMT 2012
Parsing date: 00-jan-12
Result: Mon Jan 02 00:00:00 GMT 2012
Parsing date: 02-jan-13
Result: Mon Dec 31 00:00:00 GMT 2012
That doesn't seem right, at all.
The Date object returned is something like this: #<Date Mon Jan 02 00:00:00 GMT 2012>, which is clearly not equal to what I get back from the database, for example, #<Date 2012-01-01>.
Does anyone have any ideas about this?
NOTE: I get the same result whether I use setLenient or not (and with either true or false).
Answer (Courtesy of Jon Skeet Link to answer)
I was using YY in my format string, where I should have actually used yy (Since Y is the week-year and y is the simple year).
I believe the problem is that you're using YY instead of yy. From the docs of SimpleDateFormat, YY refers to the week-year rather than the year.
Week-years are somewhat odd, and unless you know you want to use it, you probably don't. In particular, mixing week-year with "day of month" and "month" is almost never appropriate. You'd normally use "week-year, week-of-year, day-of-week" together.
To put it another way: the two systems are a bit like RGB and HSV for colours; it's as if you've defined a format of "red, green, hue" which doesn't make a lot of sense :)
Could you please help with a RE that will extract Date in HTML stream.
e.g needed date is Wednesday, April 25 2012
Thanks
(Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday),?\s+(January|February|March|April|May|June|July|August|September|October|November|December)\s+[0-3]?[0-9],?\s+[0-2][0-9][0-9][0-9]
But note that it will also find invalid dates, like ones with February 31.