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

com.solab.iso8583.parse.Date14ParseInfo Maven / Gradle / Ivy

Go to download

Java implementation of the ISO 8583 protocol, focused on making the creation, edition and reading of ISO8583 messages as simple and flexible as possible.

There is a newer version: 1.22.5
Show newest version
/*
 * j8583 A Java implementation of the ISO8583 protocol
 * Copyright (C) 2007 Enrique Zamudio Lopez
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
package com.solab.iso8583.parse;

import com.solab.iso8583.CustomField;
import com.solab.iso8583.IsoType;
import com.solab.iso8583.IsoValue;
import com.solab.iso8583.util.Bcd;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

/**
 * Blabla.
 *
 * @author Enrique Zamudio         Date: 06/01/16 10:52 AM
 */
public class Date14ParseInfo extends DateTimeParseInfo {

    /**
     * Instantiates a new Date 14 parse info.
     */
    public Date14ParseInfo() {
   		super(IsoType.DATE14, 14);
   	}

   	@Override
   	public  IsoValue parse(final int field, final byte[] buf,
                                       final int pos, final CustomField custom)
   			throws ParseException, UnsupportedEncodingException {
   		if (pos < 0) {
   			throw new ParseException(String.format("Invalid DATE14 field %d position %d",
                       field, pos), pos);
   		}
   		if (pos+14 > buf.length) {
   			throw new ParseException(String.format("Insufficient data for DATE14 field %d, pos %d",
                       field, pos), pos);
   		}
   		//A SimpleDateFormat in the case of dates won't help because of the missing data
   		//we have to use the current date for reference and change what comes in the buffer
        Calendar cal = Calendar.getInstance();
   		//Set the month in the date
        if (forceStringDecoding) {
            cal.set(Calendar.YEAR, Integer.parseInt(new String(buf, pos, 4, getCharacterEncoding()), 10));
            cal.set(Calendar.MONTH, Integer.parseInt(new String(buf, pos, 2, getCharacterEncoding()), 10)-1);
            cal.set(Calendar.DATE, Integer.parseInt(new String(buf, pos+2, 2, getCharacterEncoding()), 10));
            cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(new String(buf, pos+4, 2, getCharacterEncoding()), 10));
            cal.set(Calendar.MINUTE, Integer.parseInt(new String(buf, pos+6, 2, getCharacterEncoding()), 10));
            cal.set(Calendar.SECOND, Integer.parseInt(new String(buf, pos+8, 2, getCharacterEncoding()), 10));
        } else {
            cal.set(Calendar.YEAR, ((buf[pos] - 48) * 1000) + ((buf[pos + 1] - 48) * 100) + ((buf[pos + 2] - 48) * 10) + buf[pos + 3] - 48);
            cal.set(Calendar.MONTH, ((buf[pos+4] - 48) * 10) + buf[pos + 5] - 49);
            cal.set(Calendar.DATE, ((buf[pos + 6] - 48) * 10) + buf[pos + 7] - 48);
            cal.set(Calendar.HOUR_OF_DAY, ((buf[pos + 8] - 48) * 10) + buf[pos + 9] - 48);
            cal.set(Calendar.MINUTE, ((buf[pos + 10] - 48) * 10) + buf[pos + 11] - 48);
            cal.set(Calendar.SECOND, ((buf[pos + 12] - 48) * 10) + buf[pos + 13] - 48);
        }
        cal.set(Calendar.MILLISECOND,0);
        return createValue(cal, true);
   	}

   	@Override
   	public  IsoValue parseBinary(final int field, final byte[] buf,
                                          final int pos, final CustomField custom)
               throws ParseException {
        if (pos < 0) {
            throw new ParseException(String.format("Invalid DATE14 field %d position %d",
                field, pos), pos);
        }
        if (pos+7 > buf.length) {
            throw new ParseException(String.format("Insufficient data for DATE14 field %d, pos %d",
                field, pos), pos);
        }
   		int[] tens = new int[7];
   		int start = 0;
   		for (int i = pos; i < pos + tens.length; i++) {
   			tens[start++] = Bcd.parseBcdLength(buf[i]);
   		}
   		Calendar cal = Calendar.getInstance();
   		//A SimpleDateFormat in the case of dates won't help because of the missing data
   		//we have to use the current date for reference and change what comes in the buffer
   		//Set the month in the date
        cal.set(Calendar.YEAR, (tens[0] * 100) + tens[1]);
   		cal.set(Calendar.MONTH, tens[2] - 1);
   		cal.set(Calendar.DATE, tens[3]);
   		cal.set(Calendar.HOUR_OF_DAY, tens[4]);
   		cal.set(Calendar.MINUTE, tens[5]);
   		cal.set(Calendar.SECOND, tens[6]);
   		cal.set(Calendar.MILLISECOND,0);
        return createValue(cal, true);
   	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy