dev.langchain4j.model.output.DateOutputParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of langchain4j Show documentation
Show all versions of langchain4j Show documentation
Java implementation of LangChain: Integrate your Java application with countless AI tools and services
smoothly
package dev.langchain4j.model.output;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateOutputParser implements OutputParser {
private static final String DATE_PATTERN = "yyyy-MM-dd";
private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat(DATE_PATTERN);
@Override
public Date parse(String string) {
string = string.trim();
// SimpleDateFormat silently accepts dd-MM-yyyy; but parses it strangely.
if (string.indexOf("-") != 4 || string.indexOf("-", 5) != 7) {
throw new RuntimeException("Invalid date format: " + string);
}
try {
return SIMPLE_DATE_FORMAT.parse(string);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
@Override
public String formatInstructions() {
return DATE_PATTERN;
}
}