This week's book giveaway is in the Other Open Source APIs forum. We're giving away four copies of Storm Applied and have Sean Allen, Peter Pathirana & Matthew Jankowski on-line! See this thread for details.
I have a assignment and it is urgent. I have two dates, one is String and the format is "mm/dd/yyyy", and other is Date, format is "mm-dd-yyyy". How can I change format from one to other so I can compare? Thanks in advance.
Use the class java.text.SimpleDateFormat (look it up in the API documentation) to convert the strings with the dates to java.util.Date objects. Then you can use methods in class Date like equals(), before(), after() to compare the two dates.
You can rearrange strings with substrings and concatenation to put them both in yyyy/mm/dd format, and then they'll compare correctly.
But it would be a lot more educational to figure out the Date approach. Hang in there until you make this work just so you can say you know Date. See if upper case MM helps in your pattern. Lower case mm is minutes. Once you get parsing working, you can use one date format to parse and another to format back to string. Something along the lines ...
And just for grins, I have a string function based on REXX: translate(input,outtable,intable). For each character of the input, if that character is found in InTable, it is replaced with the corresponding character from OutTable. The conventional use is:
See what this alternative usage does:
And it was about 100x faster than the Date technique last time I checked.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi