Skip to content

Instantly share code, notes, and snippets.

@andersonmo
Created August 14, 2019 05:35
Show Gist options
  • Save andersonmo/753868a35ca08746381c2117b4545897 to your computer and use it in GitHub Desktop.
Save andersonmo/753868a35ca08746381c2117b4545897 to your computer and use it in GitHub Desktop.
Q3 - Reformatting Dates (www.hackerrank.com)
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
//---- My code ----
import java.time.format.*;
class Result {
/*
* Complete the 'reformatDate' function below.
*
* The function is expected to return a STRING_ARRAY.
* The function accepts STRING_ARRAY dates as parameter.
*/
public static List<String> reformatDate(List<String> dates) {
// Write your code here
List<String> outputDate = new ArrayList<>();
try{
for (String tempStr : dates){
tempStr = tempStr.replace("st", "").replace("nd", "").replace("rd", "").replace("th", "");
Date tempDate = new SimpleDateFormat("dd MMM yyyy", Locale.ENGLISH).parse(tempStr);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar tempC = Calendar.getInstance();
tempC.setTime(tempDate);
int year = tempC.get(Calendar.YEAR);
if (year >= 1900 && year <= 2100){
String dateStr = df.format(tempDate).toString();
outputDate.add(dateStr);
} else {
System.out.println("Year out of range");
}
}
} catch (Exception ex ){
System.out.println(ex);
}
return outputDate;
}
}
//---- End my code ----
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int datesCount = Integer.parseInt(bufferedReader.readLine().trim());
List<String> dates = IntStream.range(0, datesCount).mapToObj(i -> {
try {
return bufferedReader.readLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
})
.collect(toList());
List<String> result = Result.reformatDate(dates);
bufferedWriter.write(
result.stream()
.collect(joining("\n"))
+ "\n"
);
bufferedReader.close();
bufferedWriter.close();
}
}
@andersonmo
Copy link
Author

Given a date string in the format Day Month Year, where:

Day is in the set {"1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", …, "29th", "30th", "31st"}.
Month is in the set {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}.
Year is in the inclusive range [1900, 2100].

Convert the date string to the format YYYY-MM-DD, where:

YYYY denotes the 4 digit year.
MM denotes the 2 digit month.
DD denotes the 2 digit day.

For example:

1st Mar 1984 → 1984-03-01
2nd Feb 2013 → 2013-02-02
4th Apr 1900 → 1900-04-04

Function Description
Complete the function reformatDate in the editor below. The function must return an array of converted date strings in the order presented.

reformatDate has the following parameter(s):
dates[dates[0],...dates[n-1]]: an array of date strings in the format Day Month Year

Constraints
The values of Day, Month, and Year are restricted to the value ranges specified above.
The given dates are guaranteed to be valid, so no error handling is necessary.
1 ≤ n ≤ 104

Input Format for Custom Testing
Input from stdin will be processed as follows and passed to the function.
The first line contains an integer n, the size of the array dates.
Each of the next n lines contains a string, dates[i] where 0 ≤ i < n.

Sample Case
Sample Input

10
20th Oct 2052
6th Jun 1933
26th May 1960
20th Sep 1958
16th Mar 2068
25th May 1912
16th Dec 2018
26th Dec 2061
4th Nov 2030
28th Jul 1963

Sample Output

2052-10-20
1933-06-06
1960-05-26
1958-09-20
2068-03-16
1912-05-25
2018-12-16
2061-12-26
2030-11-04
1963-07-28

Explanation
We convert the following n = 10 dates:

20th Oct 2052 → 2052-10-20
6th Jun 1933 → 1933-06-06
26th May 1960 → 1960-05-26
20th Sep 1958 → 1958-09-20
16th Mar 2068 → 2068-03-16
25th May 1912 → 1912-05-25
16th Dec 2018 → 2018-12-16
26th Dec 2061 → 2061-12-26
4th Nov 2030 → 2030-11-04
28th Jul 1963 → 1963-07-28

We then return the array ["2052-10-20", "1933-06-06", "1960-05-26", "1958-09-20", "2068-03-16", "1912-05-25", "2018-12-16", "2061-12-26", "2030-11-04", "1963-07-28"] as our answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment