All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.langchain4j.model.output.DateOutputParser Maven / Gradle / Ivy

Go to download

Java implementation of LangChain: Integrate your Java application with countless AI tools and services smoothly

There is a newer version: 0.36.2
Show newest version
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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy