本文整理汇总了Java中com.joestelmach.natty.DateGroup类的典型用法代码示例。如果您正苦于以下问题:Java DateGroup类的具体用法?Java DateGroup怎么用?Java DateGroup使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DateGroup类属于com.joestelmach.natty包,在下文中一共展示了DateGroup类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: match
import com.joestelmach.natty.DateGroup; //导入依赖的package包/类
@Override
public SlotMatch match(String token, Context context)
{
Parser parser = new Parser(context.getTimeZone());
List<DateGroup> groups = parser.parse(token);
for (DateGroup group : groups)
{
if (!group.isDateInferred() && !group.isTimeInferred())
{
List<Date> dates = group.getDates();
// natty is very aggressive so will match date on text that is largely not a date, which is
// not what we want
String matchText = group.getText();
float percMatch = (float) matchText.length() / (float) token.length();
if (!dates.isEmpty() && percMatch > 0.75)
{
return new SlotMatch(this, token,
new DateTime(dates.get(0), DateTimeZone.forTimeZone(context.getTimeZone())));
}
}
}
return null;
}
示例2: parseStringAsDate
import com.joestelmach.natty.DateGroup; //导入依赖的package包/类
/**
* Converts a string to a date. Uses DateAdaptorJ, if that fails, falls back
* to Natty.
*
* @param in String to convert to a date.
* @return A date based on the string.
* @throws IndexParseFieldException If the field cannot be parsed as a date.
*/
public static Date parseStringAsDate(String in) throws IndexParseFieldException //TODO: come back to this and add more formats (TimezonedTimestampAdaptor) and tests https://github.com/PearsonEducation/Docussandra/issues/2
{
DateAdapter adapter = getTimezonedTimestampAdaptor();
try
{
return adapter.parse(in);
} catch (ParseException e)//fall back to a DateAdaptor
{
Parser parser = new Parser(TimeZone.getTimeZone("GMT"));//assume all dates are GMT
List<DateGroup> dg = parser.parse(in);
if (dg.isEmpty())
{
throw new IndexParseFieldException(in);
}
List<Date> dates = dg.get(0).getDates();
if (dates.isEmpty())
{
throw new IndexParseFieldException(in);
}
return dates.get(0);//dang; that actually works
}
}
示例3: singleIntervalUnitWithUntil
import com.joestelmach.natty.DateGroup; //导入依赖的package包/类
private String singleIntervalUnitWithUntil(String parameters, DateGroup group) {
String returnNotParsed = parameters;
recursUntil = LocalDateTime.ofInstant(group.getDates().get(0).toInstant(), ZoneId.systemDefault());
String[] splitParameters = parameters.split(ONE_SPACE);
if (parameters.startsWith("everyday")) {
interval = Period.ofDays(1);
returnNotParsed = returnNotParsed.replaceAll("everyday", EMPTY_STRING);
} else if (splitParameters[0].equals("every") && isIntervalUnit(splitParameters[1])) {
interval = determineIntervalUnit(splitParameters[1]);
returnNotParsed = returnNotParsed.replaceAll("every", EMPTY_STRING);
returnNotParsed = returnNotParsed.replaceAll(splitParameters[1], EMPTY_STRING);
}
returnNotParsed = returnNotParsed.replaceAll("until", EMPTY_STRING);
returnNotParsed = returnNotParsed.replaceAll(group.getText(), EMPTY_STRING);
return returnNotParsed;
}
示例4: runCommand
import com.joestelmach.natty.DateGroup; //导入依赖的package包/类
/**
* Executes command
* @param event passed event
* @param args passed arguments
*/
@Override
public final void runCommand(MessageReceivedEvent event, String args) {
RemindHandler remindHandler = rexCord.getRemindHandler();
//Target message inside quotation marks
int messageStart = args.indexOf("\"");
int messageEnd = args.lastIndexOf("\"");
if (messageStart == messageEnd
|| messageStart == -1
|| messageEnd == -1) {
rexCord.sendMessage(event.getChannel(), "Message not detected");
return;
}
try {
//Do +1 as substring is on [start, end-1] interval
String message = args.substring(messageStart + 1, messageEnd);
String dateQuery = args.substring(0, messageEnd);
//Use Natty Date Parser
Parser parser = new Parser();
List<DateGroup> groups = parser.parse(dateQuery);
//Get Natty's interpreted Date
Date targetDate = groups.get(0).getDates().get(0);
String confirm = String.format("Ok, I'll remind you on %s: \"%s\"",
targetDate.toString(), message);
rexCord.sendMessage(event.getChannel(), confirm);
remindHandler.addReminder(new Reminder(targetDate,
message,
event.getAuthor()));
} catch (Exception e) {
rexCord.sendMessage(event.getChannel(),
"Could not interpret query :(");
}
}
示例5: getTextAndTime
import com.joestelmach.natty.DateGroup; //导入依赖的package包/类
/**
* Parses command arguments and returns a text and a date provided that
* the argument is in the form of "text | human time", the date argument
* has to be in the future
* @param scan
* @return
* @throws InvalidTimeInputException
* @throws TimeInputInPastException
*/
public static Pair<String, Date> getTextAndTime(Scanner scan) throws InvalidTimeInputException, TimeInputInPastException {
String text;
String timeArgument = null;
String[] splitArgumentsRaw = seekScannerToEnd(scan).split("\\|");
if (splitArgumentsRaw.length == 1) {
text = splitArgumentsRaw[0];
} else {
text = splitArgumentsRaw[0];
timeArgument = splitArgumentsRaw[1];
}
text = text.trim();
timeArgument = timeArgument == null ? null: timeArgument.trim();
Date time = null;
Date now = new Date();
if (timeArgument != null) {
Parser parser = new Parser();
List<DateGroup> dateGroups = parser.parse(timeArgument);
try {
time = dateGroups.get(0).getDates().get(0);
} catch (IndexOutOfBoundsException e) {
throw new InvalidTimeInputException();
}
if (time.compareTo(now) < 0) {
throw new TimeInputInPastException();
}
}
return new Pair<>(text, time);
}
示例6: convert
import com.joestelmach.natty.DateGroup; //导入依赖的package包/类
@Override
public Date convert(String source) {
List<DateGroup> groups = dateParser.parse(source);
if (groups.size() > 0 && groups.get(0).getDates().size() > 0) {
return groups.get(0).getDates().get(0);
}
throw new ConverterException("can not convert '" + source + "' to Date");
}
示例7: parseDate
import com.joestelmach.natty.DateGroup; //导入依赖的package包/类
public Date parseDate( String date )
{
Parser parser = new Parser();
List<DateGroup> groups = parser.parse( date );
for ( DateGroup group : groups )
{
List<Date> dates = group.getDates();
return dates.get( 0 );
}
return null;
}
示例8: evaluate
import com.joestelmach.natty.DateGroup; //导入依赖的package包/类
@Override
protected DateTime evaluate(FunctionArgs args, EvaluationContext context, DateTimeZone timezone) {
final String time = valueParam.required(args, context);
final List<DateGroup> dates = new Parser(timezone.toTimeZone()).parse(time);
if (dates.size() == 0) {
final Optional<DateTime> defaultTime = defaultParam.optional(args, context);
if (defaultTime.isPresent()) {
return defaultTime.get();
}
// TODO really? this should probably throw an exception of some sort to be handled in the interpreter
return null;
}
return new DateTime(dates.get(0).getDates().get(0), timezone);
}
示例9: match
import com.joestelmach.natty.DateGroup; //导入依赖的package包/类
@Override
public SlotMatch match(String token, Context context)
{
Parser parser = new Parser(context.getTimeZone());
List<DateGroup> groups = parser.parse(token);
for (DateGroup group : groups)
{
if (!group.isTimeInferred())
{
List<Date> dates = group.getDates();
// natty is very aggressive so will match date on text that is largely not a date, which is
// not what we want
String matchText = group.getText();
float percMatch = (float) matchText.length() / (float) token.length();
if (!dates.isEmpty() && percMatch > 0.75)
{
DateTime theDateTime = new DateTime(dates.get(0),
DateTimeZone.forTimeZone(context.getTimeZone()));
return new SlotMatch(this, token, theDateTime.toLocalTime());
}
}
}
return null;
}
示例10: remindMe
import com.joestelmach.natty.DateGroup; //导入依赖的package包/类
private void remindMe(CommandMessageReceivedEvent event) {
if (event.getArgsString().equals("")) {
event.getChat().sendMessage("Give me ");
return;
}
String str = event.getArgsString();
int idx, idx2 = -1;
if ((idx = str.indexOf(" to ")) != -1 && idx < (idx2 = str.indexOf(" that ")) || idx2 == -1) {
str = str.replaceFirst(" to ", " that ");
}
String[] args = str.split("that", 2);
if (args.length != 2) {
event.getChat().sendMessage("Something went wrong while processing your reminder! Please try again");
return;
}
DateGroup date = TimeParser.parse(args[0]);
if (date == null) {
event.getChat().sendMessage("It seems that the time you entered doesn't make sense. Please try again!");
return;
}
Reminder reminder = new Reminder(
date.getDates().get(0).getTime(),
event.getChat().getId(),
args[1],
event.getMessage().getSender().getUsername());
instance.getReminderManager().addReminder(reminder);
event.getChat().sendMessage(SendableTextMessage.builder()
.message(String.format("*New reminder added!* \n*Reminded at:* _%s_ \n*Reminder:* _%s_",
TimeParser.asString(reminder.getUnixTime()),
reminder.getReminder()))
.replyTo(event.getMessage())
.parseMode(ParseMode.MARKDOWN)
.build());
}
示例11: parse
import com.joestelmach.natty.DateGroup; //导入依赖的package包/类
public static DateGroup parse(String value) {
new Parser(TimeZone.getTimeZone("UTC"));
List<DateGroup> groups = new Parser().parse(value);
if (groups.isEmpty()) {
return null;
}
return groups.get(0);
}
示例12: processBackPart
import com.joestelmach.natty.DateGroup; //导入依赖的package包/类
/**
* Extract recur interval and recur limit (recursUntil) out of parameters after REPEAT keyword
*
* Possible situations (in example format):
* Single interval unit with no recurs until -> every week / everyday
* Single interval unit with recurs until -> every week until dec
* Multiple interval units with no recurs until -> every two months
* Multiple interval units with until -> every two weeks until dec
*
* The reason we split so many situations is that, Natty although accept recurring datetime, it parses recur very differently.
* Sometimes it will go against our original purpose. The details of how it violate our purpose are omitted here.
*
* P.S.
* A recurring task require at least two elements
* 1. Recur starting point
* 2. Recur interval
* (3. Recur limit)
* This is the main reason we require users to input STARTING TIME before REPEAT and followed by INTERVAL and RECURSUNTIL
*
* As for interval unit, we regard 'day', 'week', 'month', 'year' as single unit which cannot be split down into smaller components,
* whereas 'two weeks', 'three months' are multiple interval units.
* Whichever interval format is goes, the interval is always a constant value.
* Intervals like 'every monday to friday' are not allowed in FINI.
*
* @param parameters
* @return notParsed parameters after REPEAT keyword
*/
private String processBackPart(String parameters) {
if (recurFlag) {
isRecurring = true;
} else {
return parameters;
}
List<DateGroup> groups = parser.parse(parameters);
if (!parameters.startsWith("every")) {
interval = Period.ofDays(1);
return parameters;
}
String returnNotParsed = EMPTY_STRING;
if (groups.size() == 0) {
returnNotParsed = singleIntervalUnitWithNoUntil(parameters);
} else if (groups.size() == 1 && parameters.contains("until") && !groups.get(0).isRecurring()) {
returnNotParsed = singleIntervalUnitWithUntil(parameters, groups.get(0));
} else if (groups.size() == 1 && !parameters.contains("until") && groups.get(0).isRecurring()) {
returnNotParsed = multipleIntervalUnitsWithNoUntil(parameters);
} else if (groups.size() == 1 && parameters.contains("until") && groups.get(0).isRecurring()) {
returnNotParsed = multipleIntervalUnitsWithUntil(parameters, groups.get(0));
} else {
// default: everyday endlessly
interval = Period.ofDays(1);
}
return returnNotParsed;
}
示例13: multipleIntervalUnitsWithUntil
import com.joestelmach.natty.DateGroup; //导入依赖的package包/类
private String multipleIntervalUnitsWithUntil(String parameters, DateGroup group) {
recursUntil = LocalDateTime.ofInstant(group.getRecursUntil().toInstant(), ZoneId.systemDefault());
String returnNotParsed = parameters;
String[] splitParameters = parameters.split(ONE_SPACE);
if (isValidNumbering(splitParameters[1]) && isIntervalUnits(splitParameters[2])) {
interval = determineIntervalUnits(splitParameters[1], splitParameters[2]);
returnNotParsed = returnNotParsed.replaceAll("every " + splitParameters[1] + ONE_SPACE + splitParameters[2], EMPTY_STRING);
} else {
interval = Period.ofDays(1);
}
return returnNotParsed;
}
示例14: getDate
import com.joestelmach.natty.DateGroup; //导入依赖的package包/类
private LocalDate getDate(String lessonDate) {
Parser parser = new Parser();
List<DateGroup> groups = parser.parse(lessonDate);
DateGroup group = groups.get(0);
List<Date> dates = group.getDates();
Date date = dates.get(0);
LocalDate lessonLocalDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
return lessonLocalDate;
}
示例15: strToDate
import com.joestelmach.natty.DateGroup; //导入依赖的package包/类
private Date strToDate(String s)
{
Parser parser = new Parser();
List<DateGroup> groups = parser.parse(s);
for(DateGroup group : groups)
{
List<Date> dates = group.getDates();
if (dates.size() > 0)
return dates.get(0);
}
return null;
}