Last active
August 29, 2015 14:19
-
-
Save oxguy3/7bb9b390dc990966f1db to your computer and use it in GitHub Desktop.
CoeBot date parsing
This file contains hidden or 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
import java.util.Scanner; | |
import org.joda.time.DateTime; | |
import org.joda.time.DateTimeZone; | |
import org.joda.time.Period; | |
import org.joda.time.PeriodType; | |
import org.joda.time.format.DateTimeFormat; | |
import org.joda.time.format.DateTimeFormatter; | |
import org.joda.time.format.PeriodFormatter; | |
import org.joda.time.format.PeriodFormatterBuilder; | |
/** | |
* Demo for date parsing in CoeBot | |
* | |
* Requires Joda-Time, found at joda.org/joda-time | |
* | |
* @author oxguy3 | |
* | |
*/ | |
public class CoebotDateParsing { | |
public static void main(String[] args) { | |
@SuppressWarnings("resource") | |
Scanner scan = new Scanner(System.in); | |
System.out.println("CoeBot string parsing demo"); | |
String s = null; | |
while (true) { | |
System.out.println("\nEnter some text to parse: "); | |
s = scan.nextLine(); | |
System.out.println(parseMessage(s)); | |
} | |
} | |
public static String parseMessage(String message) { | |
if (message.contains("(_DATE_") && message.contains("_)")) { | |
message = handleDatetime(message, "(_DATE_", "_)", "MMM d, yyyy"); | |
} | |
if (message.contains("(_TIME_") && message.contains("_)")) { | |
message = handleDatetime(message, "(_TIME_", "_)", "h:mm a"); | |
} | |
if (message.contains("(_TIME24_") && message.contains("_)")) { | |
message = handleDatetime(message, "(_TIME24_", "_)", "k:mm"); | |
} | |
if (message.contains("(_DATETIME_") && message.contains("_)")) { | |
message = handleDatetime(message, "(_DATETIME_", "_)", "MMM d, yyyy h:mm a"); | |
} | |
if (message.contains("(_DATETIME24_") && message.contains("_)")) { | |
message = handleDatetime(message, "(_DATETIME24_", "_)", "MMM d, yyyy k:mm"); | |
} | |
if (message.contains("(_UNTIL_") && message.contains("_)")) { | |
PeriodFormatter formatDHM = new PeriodFormatterBuilder() | |
.printZeroNever() | |
.appendDays() | |
.appendSuffix(" day", " days") | |
.appendSeparator(", ") | |
.printZeroNever() | |
.appendHours() | |
.appendSuffix(" hr", " hrs") | |
.appendSeparator(", ") | |
.printZeroRarelyLast() | |
.appendMinutes() | |
.appendSuffix(" min") | |
.toFormatter(); | |
message = handleUntil(message, "(_UNTIL_", "_)", formatDHM); | |
} | |
if (message.contains("(_UNTILSHORT_") && message.contains("_)")) { | |
PeriodFormatter formatShortDHM = new PeriodFormatterBuilder() | |
.printZeroNever() | |
.appendDays() | |
.appendSuffix("d") | |
.printZeroNever() | |
.appendHours() | |
.appendSuffix("h") | |
.printZeroRarelyLast() | |
.appendMinutes() | |
.appendSuffix("m") | |
.toFormatter(); | |
message = handleUntil(message, "(_UNTILSHORT_", "_)", formatShortDHM); | |
} | |
if (message.contains("(_UNTILLONG_") && message.contains("_)")) { | |
PeriodFormatter formatLongDHM = new PeriodFormatterBuilder() | |
.printZeroNever() | |
.appendDays() | |
.appendSuffix(" day", " days") | |
.appendSeparator(", ", " and ") | |
.printZeroNever() | |
.appendHours() | |
.appendSuffix(" hour", " hours") | |
.appendSeparator(", ", " and ") | |
.printZeroRarelyLast() | |
.appendMinutes() | |
.appendSuffix(" minute", " minutes") | |
.toFormatter(); | |
message = handleUntil(message, "(_UNTILLONG_", "_)", formatLongDHM); | |
} | |
return message; | |
} | |
public static String handleDatetime( | |
String message, String prefix, String suffix, String format) | |
{ | |
int commandStart = message.indexOf(prefix); | |
int commandEnd = message.indexOf(suffix); | |
String replaced = message | |
.substring(commandStart, commandEnd + suffix.length()); | |
DateTimeZone tz; | |
if (commandStart + prefix.length() < commandEnd) { | |
String tzid = message | |
.substring(commandStart + prefix.length(), commandEnd); | |
try { | |
tz = DateTimeZone.forID(tzid); | |
} catch (IllegalArgumentException e) { | |
tz = DateTimeZone.UTC; | |
} | |
} else { | |
tz = DateTimeZone.UTC; | |
} | |
DateTimeFormatter fmt = DateTimeFormat.forPattern(format); | |
fmt = fmt.withZone(tz); | |
String dateStr = fmt.print(new DateTime()); | |
message = message.replace(replaced, dateStr); | |
return message; | |
} | |
public static String handleUntil( | |
String message, String prefix, String suffix, PeriodFormatter formatter) | |
{ | |
int commandStart = message.indexOf(prefix); | |
int commandEnd = message.indexOf(suffix); | |
if (commandStart + prefix.length() < commandEnd) { | |
String replaced = message | |
.substring(commandStart, commandEnd + suffix.length()); | |
String dateStr = message | |
.substring(commandStart + prefix.length(), commandEnd); | |
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm"); | |
String until; | |
try { | |
DateTime future = fmt.parseDateTime(dateStr); | |
PeriodType pType = PeriodType.dayTime() | |
.withMillisRemoved() | |
.withSecondsRemoved(); | |
Period period = new Period(new DateTime(), future, pType); | |
until = period.toString(formatter); | |
} catch (IllegalArgumentException e) { | |
until = "Unknown date"; | |
System.out.println(dateStr); | |
e.printStackTrace(); | |
} | |
message = message.replace(replaced, until); | |
} | |
return message; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment