by bane · 12y ago · 0 comments
Date thing = new Date(someStringWithaDate,"MMM dd, yyyy");
String newDateThing = thing.convertDateFormat("yyyy-mm-dd");
and be done with it. Or at worst Date thing = new Date(someStringWithaDate,"MMM dd, yyyy");
Date newDateThing = thing.convertDateFormat("yyyy-mm-dd");
String someStringWithaDateButFormattedLikeIWant = newDateThing.toString();
But the overwrought standard library just completely got in the way and I don't even remember how many lines of code and objects I ended up with just to do this before I said "fuck it" and did the equivalent of (with a some utilities to convert abreviated dates to numbers. String.replaceAll("([A-Z]{3}) (\d{2}), (\d{4})","$3-$2-$1");
Of course now I'm not getting whatever date validation the Date class might offer (if it does), and I ended up using a 50 cent solution when I really needed a $1.50 solution, but the standard library only offered an overpriced $20 way that I didn't need.The end result is that we're all poorer for it.
DateFormat format1 = new SimpleDateFormat("MMM dd, yyyy");
DateFormat format2 = new SimpleDateFormat("yyy-mm-dd");
String originalDateString = ...;
String newDateString = format2.format(format1.parse(originalDateString));
Even in something like Python, the basic pattern is the same: a call to strptime to convert from string => date object in the original format, and a call to strftime to convert from date object => string in desired format.Still it seems conceptually weird that the idea of a DateFormat object can be a thing, when it's just a String with the magic Format language.
Parsing or formatting (the verbs) seem more like actions to be taken on a Date itself. It seems much tidyier to me to just keep all that contextually in the Date class.
From a practical standpoint, SimpleDateFormat and its analogues don't operate directly on the format string you supply, but instead compile it into a form that is easier to process. If Date.format was the only way to format a date, then the format pattern would have to be recompiled on every call (or else cached, which would affect thread safety).
Now, String.split exists, despite having to recompile the regular expression every time it is called (except for certain optimized cases), so presumably Date.format could also exist, but DateFormat would still be preferable for many cases.
Why isn't all this garbage just bundled into the Date class? It seems to me that among the many things somebody might want to do on a date, converting the String representation might be one of them.
okay here's what I'm talking about. From the API docs on Date
toGMTString() - *Deprecated.* As of JDK version 1.1, replaced by DateFormat.format(Date date), using a GMT TimeZone.
toLocaleString() - *Deprecated.* As of JDK version 1.1, replaced by DateFormat.format(Date date).
toString() - Converts this Date object to a String of the form: dow mon dd hh:mm:ss zzz yyyy
All either deprecated or useless (dow mon dd hh:mm:ss zzz yyyy? really?). And the documentation doesn't point to the better classes to do what I want.Okay, so what's a DateFormat?
DateFormat is an abstract class for date/time formatting subclasses which formats and parses dates or time in a language-independent manner.
Okay that sounds promising, of course they're admitting they don't have the concept of a "Date Format" object type since it's all just static stuff, so shouldn't all this just be in the Date class? That feels weird.Wait there's a DateFormatter, that sounds promising, it's all verby sounding.
DateFormatter is an InternationalFormatter that does its formatting by way of an instance of java.text.DateFormat.
Well what the hell is an InternationalFormatter? And down the rabbit hole I went.I did eventually figure it out. But it was so inelegant and stupid I felt ashamed to have it in my code. If I had read one more line in the DateFormatter doc I suppose I could have found SimpleDateFormat, but the bizarre complexity about converting formats was already so mind bendingly dumb.
This is stupid and obtuse design, overly parsing the problem and unnecessarily separating what should be connected pieces of the puzzle.
Solution (needs revision, but this would be a useful point to start a discussion):
Classes
Date - a representation of a specific date and time down to millionths of a second resolution
methods:
add, sub Dates - returns Duration
add,sub Duration, returns Dates
one of the constructors takes a String that ostensibly contains a Date of some sort, and a format String to parse it, throws exception if the date isn't valid
setOutPutFormat - takes a Format string and the next call toString() emits on that format.
all the get, set hour minute day year methods so you can pick it apart change pieces as you want
setOutPutCalendar - changes the Date to whatever recognized calendar system (Chinese, Arabic, Hebrew, etc.), setOutPutFormat is context aware of this
all the timezone stuff
after and before
get/set 2 digit year
Date comparison methods like isBefore(Date) to see if another Date object is before or after this one
Done. All of Date, DateFormat and SimpleDateFormat is there, problems solvedDuration - an representation of a time Duration (6 days, 7 minutes, 14 second), and since all time intervals are durations, that's it.
Methods:
Constructor can be an ISO 8601 String for Duration or two Date objects representing the beginning and end of a duration. Since Date objects should have their own timezone and locals, the magic black box of this class should figure out the duration for me. It's not my problem to sort this out. If the begin and end dates produce a negative duration that's cool because now I have a countdown.
Duration comparison methods, like isWithin(Duration) to check if another duration object is within this one or isWithin(Date) to see if a Date is within the duration or isLongerthan(Duration)
Duration math like add/subtract other durations from the begin and end of the Duration
Done.However Joda does a lot more, like taking into account the various historical, political changes in time zone, and the distinction between a duration between two human-understandable semantic times, versus a duration as a fixed span of time.
I'm also not sure I understand the distinction between the two durations, if I set a startDate object up on the Islamic Tabular Fatimid calendar and say it's year 14 and endDate is some pre-reformation Julian calendar and say it's year 231, shouldn't the Duration class be able to figure out a fixed span of time from that? I really could care less how it does it, just that the right number of years, months and days fall out of it. Or if I just want to create the object with some defined span, they're the same thing.