forked from nusCS2113-AY1920S1/duke
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from CEGLincoln/master
added class convert
- Loading branch information
Showing
17 changed files
with
160 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
T|1|read book | ||
D|0|return book|Monday | ||
E|0|meeting|U-Town | ||
P|0|lecture|1600|1800 | ||
D|1|return book|09/09/09 2359 | ||
E|0|meeting|here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package duke.parser; | ||
|
||
import java.text.DateFormat; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.time.LocalDate; | ||
import java.time.ZoneId; | ||
import java.util.Date; | ||
|
||
/** | ||
* Convert between String and Date. | ||
*/ | ||
public class Convert { | ||
|
||
/** | ||
* Returns the suffix to be used after the days in the Date, useful for printing the Date in the desired format. | ||
* | ||
* @param n indication the Day of the month | ||
* @return the suffix accordingly to the day of the month needed | ||
*/ | ||
public static String getDaySuffix(int n) { | ||
if (n >= 11 && n <= 13) { | ||
return "th"; | ||
} | ||
switch (n % 10) { | ||
case 1: | ||
return "st"; | ||
case 2: | ||
return "nd"; | ||
case 3: | ||
return "rd"; | ||
default: | ||
return "th"; | ||
} | ||
} | ||
|
||
/** | ||
* Returns a {@link Date} representation of a String in the format "dd/MM/yyyy hhmm" or "dd/MM/yyyy". | ||
* | ||
* @param date String in the format "dd/MM/yyyy hhmm" or "dd/MM/yyyy", used to extract a {@link Date} instance from | ||
* @return The {@link Date} instance created from the argument string or null | ||
*/ | ||
public static Date stringToDate(String date) { | ||
DateFormat formatter; | ||
if (date.length() > 11) { | ||
formatter = new SimpleDateFormat("dd/MM/yyyy hhmm"); | ||
} else { | ||
formatter = new SimpleDateFormat("dd/MM/yyyy"); | ||
} | ||
try { | ||
return formatter.parse(date); | ||
} catch (ParseException e) { | ||
System.out.println("Warning: Unable to convert \"" + date + "\" to a Date."); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Returns the {@link Date } instance as a String to be printed in the file. | ||
* | ||
* @param date deadline {@link Date} for finishing the task | ||
* @return String the date for the deadline | ||
*/ | ||
public static String getDateString(Date date, String dateString) { | ||
if (date == null) { | ||
return dateString; | ||
} | ||
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); | ||
String pattern; | ||
if (dateString.length() > 11) { | ||
pattern = "d'" + getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy, ha "; | ||
} else { | ||
pattern = "d'" + getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy"; | ||
} | ||
SimpleDateFormat formatter = new SimpleDateFormat(pattern); | ||
return formatter.format(date); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.