package de.unituebingen.ub.ubtools.solrmarcMixin;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.marc4j.marc.DataField;
import org.marc4j.marc.Record;
import org.marc4j.marc.Subfield;
import org.marc4j.marc.VariableField;
import org.solrmarc.index.SolrIndexerMixin;

public class KrimDok extends SolrIndexerMixin {
    private final static DateFormat recordingDateFormat = new SimpleDateFormat("yyyyMM");

    /**
     * get thew records's recoding date from a LOK-tagged field
     *
     * @param record  the record
     * @return The date
     */
    public String getLocalRecordingDate(final Record record) {
        for (final VariableField variableField : record.getVariableFields("LOK")) {
            final DataField lokfield = (DataField) variableField;
            final Subfield subfield0 = lokfield.getSubfield('0');
            if (subfield0 != null && subfield0.getData().equals("938  ")) {
                final Subfield subfield_a = lokfield.getSubfield('a');
                if (subfield_a != null) {
                    final String dateCandidate = subfield_a.getData();
                    try {
                        // We only use this check that we have a valid date.
                        final Date recodingDate = recordingDateFormat.parse("20" + dateCandidate);

                        return dateCandidate;
                    } catch (Exception e) {
                        System.err.println("strange local recoding date: " + dateCandidate);
                    }
                }
            }
        }
        return null;
    }
}

@@ -586,16 +586,35 @@
 The date
+ in ISO-8601 format
 %0A     */
@@ -1386,16 +1386,23 @@
   return
+ %2220%22 +
  dateCan
@@ -1407,16 +1407,78 @@
 andidate
+.substring(0, 2) + %22-%22 + dateCandidate.substring(2, 4) + %22-01%22
 ;%0A      

Return a date that SolrMarc can hopefully parse.