3 ms·
I think you are overlooking the fact that perhaps there was an easier and/or more idiomatic way to convert from one format to another, but that you missed out o
by brown9-2 13y ago
I think you are overlooking the fact that perhaps there was an easier and/or more idiomatic way to convert from one format to another, but that you missed out on how to do it with the (admittingly poor) standard date/time libraries since you haven't had to solve this particular problem in Java before. You probably just want something like:
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.
- bane 13y agoTrue, it's been so long since I Java'd at all, that I simply don't remember the idiomatic ways to do things. And those ways have probably changed quite a bit in the intervening years. 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.
- kbolino 13y agoFrom a theoretical perspective, date formatting is a behavior that can be accomplished in a number of different ways. While the pre-Java 8 API only provides SimpleDateFormat, JodaTime provides a similar class and a slew of formatters based upon ISO8601 that don't use patterns at all. 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.