Q. Complete the method “changeDateFormat(String paragraph)” which takes a string input containing dates in mm/dd/yyyy format and converts them to “dd/mm/yyyy” format. For example, “My birthday is on 04/25/1980” becomes “My birthday is on 25/04/1980”
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Sentences { public static String changeDateFormat(String paragraph) { throw new RunTimeException("Complete this method."); // remove this line } public static void main(String[] args) { System.out.println(changeDateFormat("The season started on the 07/25/2015 and finished on the 08/09/2015, with a bang!!!.")); } } |
A. Here is the solution.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import java.text.SimpleDateFormat; import java.util.Date; public class Sentences { public static String changeDateFormat(String paragraph) { if(paragraph == null || paragraph.length() == 0) { return paragraph; } SimpleDateFormat sdfInput = new SimpleDateFormat("mm/dd/yyyy"); SimpleDateFormat sdfOutput = new SimpleDateFormat("dd/mm/yyyy"); StringBuilder sb = new StringBuilder(paragraph); String[] tokens = paragraph.split("\\s+"); for (String token : tokens) { try { if(token.contains("/")) { Date parsedDate = sdfInput.parse(token); String formattedDate = sdfOutput.format(parsedDate); int indexOf = paragraph.indexOf(token); sb.replace(indexOf, indexOf+formattedDate.length(), formattedDate); } } catch(Exception ex) { } } return sb.toString(); } public static void main(String[] args) { System.out.println(changeDateFormat("The season started on the 07/25/2015 and finished on the 08/09/2015, with a bang!!!.")); } } |
Key Points
1. Fail fast by checking for “paragraph == null || paragraph.length() == 0”.
2. Define date formats for input (i.e. mm/dd/yyyy) and output (i.e. dd/mm/yyyy) formats with “SimpleDateFormat” class.
3. Split the input string into tokens on space with regex “\\s+“, which means 1 or more spaces. The dates “07/25/2015” and 08/09/2015 will be one of the tokens along with the other tokens like “The”, “season”, “started”, and so on..
4. Loop through the tokens and whenever a token contains “/”, it could be date, so try to apply the output format. If it was not a date, but just a “/”, an exception will be thrown and ignored.
5. If it was a date token, replace the input format with the output format using “indexof” to find the position and “replace”.
|
1 2 3 4 |
int indexOf = paragraph.indexOf(token); sb.replace(indexOf, indexOf+formattedDate.length(), formattedDate); |
Can this be solved by applying the regular expressions?
Using a regex as shown below will validate the digits, but NOT the actual dates. Regex would not know that you can’t have 30 days in the month of February.
|
1 2 3 4 |
if (str.matches("\\d{2}/\\d{2}/\\d{4}")) { ... } |
So, you still need to apply the “SimpleDateFormat” to validate the date. Here is the revised solution with regular expressions.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Sentences { public static String changeDateFormat(String paragraph) { if(paragraph == null || paragraph.length() == 0) { return paragraph; } SimpleDateFormat sdfInput = new SimpleDateFormat("mm/dd/yyyy"); SimpleDateFormat sdfOutput = new SimpleDateFormat("dd/mm/yyyy"); StringBuilder sb = new StringBuilder(paragraph); //regex to extract date digits Matcher m = Pattern.compile("\\d{2}/\\d{2}/\\d{4}").matcher(sb); while (m.find()) { String foundDate = m.group(); Date parsedDate; try { parsedDate = sdfInput.parse(foundDate); String formattedDate = sdfOutput.format(parsedDate); sb.replace(m.start(), m.end(), formattedDate); } catch (ParseException e) { e.printStackTrace(); } } return sb.toString(); } public static void main(String[] args) { System.out.println(changeDateFormat("The season started on the 07/25/2015 and finished on the 08/09/2015, with a bang!!!.")); } } |
The output on both approaches will be:
|
1 2 3 |
The season started on the 25/07/2015 and finished on the 09/08/2015, with a bang!!!. |
More coding challenges to sharpen your programming skills in Java
1. 13 More coding problems & solutions
2. 20+ Can you write Java code? type coding problems
3. Beginner level, what is wrong with this code?