This commit is contained in:
Enrique Zamudio
2018-04-17 13:40:27 -05:00
parent f61826f064
commit d7cf38ffd5
7 changed files with 105 additions and 27 deletions
+7 -3
View File
@@ -64,7 +64,9 @@ public enum IsoType {
/** variable length byte array with 4-digit header length. */
LLLLBIN(false, 0),
/** Date in format yyMMddHHmmss. */
DATE12(false,12);
DATE12(false,12),
/** Date in format yyMMdd */
DATE6(false,6);
private boolean needsLen;
private int length;
@@ -99,8 +101,10 @@ public enum IsoType {
} else if (this == DATE12) {
sdf = new SimpleDateFormat("yyMMddHHmmss");
} else if (this == DATE14) {
sdf = new SimpleDateFormat("YYYYMMddHHmmss");
} else {
sdf = new SimpleDateFormat("YYYYMMddHHmmss");
} else if (this == DATE6) {
sdf = new SimpleDateFormat("yyMMdd");
} else {
throw new IllegalArgumentException("Cannot format date as " + this);
}
if (tz != null) {
@@ -97,11 +97,7 @@ public class Date10ParseInfo extends DateTimeParseInfo {
cal.set(Calendar.MINUTE, tens[3]);
cal.set(Calendar.SECOND, tens[4]);
cal.set(Calendar.MILLISECOND,0);
if (tz != null) {
cal.setTimeZone(tz);
}
adjustWithFutureTolerance(cal);
return new IsoValue<>(type, cal.getTime(), null);
return createValue(cal, true);
}
}
@@ -94,11 +94,7 @@ public class Date12ParseInfo extends DateTimeParseInfo {
cal.set(Calendar.MINUTE, tens[4]);
cal.set(Calendar.SECOND, tens[5]);
cal.set(Calendar.MILLISECOND,0);
if (tz != null) {
cal.setTimeZone(tz);
}
adjustWithFutureTolerance(cal);
return new IsoValue<>(type, cal.getTime(), null);
return createValue(cal, true);
}
}
@@ -84,11 +84,7 @@ public class Date14ParseInfo extends DateTimeParseInfo {
cal.set(Calendar.MINUTE, tens[5]);
cal.set(Calendar.SECOND, tens[6]);
cal.set(Calendar.MILLISECOND,0);
if (tz != null) {
cal.setTimeZone(tz);
}
adjustWithFutureTolerance(cal);
return new IsoValue<>(type, cal.getTime(), null);
return createValue(cal, true);
}
}
@@ -86,11 +86,7 @@ public class Date4ParseInfo extends DateTimeParseInfo {
cal.set(Calendar.MONTH, tens[0] - 1);
cal.set(Calendar.DATE, tens[1]);
cal.set(Calendar.MILLISECOND,0);
if (tz != null) {
cal.setTimeZone(tz);
}
DateTimeParseInfo.adjustWithFutureTolerance(cal);
return new IsoValue<>(type, cal.getTime(), null);
return createValue(cal, true);
}
}
@@ -0,0 +1,93 @@
package com.solab.iso8583.parse;
import com.solab.iso8583.CustomField;
import com.solab.iso8583.IsoType;
import com.solab.iso8583.IsoValue;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
/**
* Parser for fields of type DATE6.
*
* @author Enrique Zamudio
* Date: 4/17/18 12:41 PM
*/
public class Date6ParseInfo extends DateTimeParseInfo {
public Date6ParseInfo() {
super(IsoType.DATE6, 6);
}
@Override
public <T> IsoValue<Date> parse(int field, byte[] buf, int pos, CustomField<T> custom) throws ParseException, UnsupportedEncodingException {
if (pos < 0) {
throw new ParseException(String.format("Invalid DATE6 field %d position %d",
field, pos), pos);
}
if (pos+6 > buf.length) {
throw new ParseException(String.format(
"Insufficient data for DATE6 field %d, pos %d", field, pos), pos);
}
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
//Set the month in the date
int year;
if (forceStringDecoding) {
year = Integer.parseInt(new String(buf, pos, 2, getCharacterEncoding()), 10);
cal.set(Calendar.MONTH, Integer.parseInt(new String(buf, pos+2, 2, getCharacterEncoding()), 10)-1);
cal.set(Calendar.DATE, Integer.parseInt(new String(buf, pos+4, 2, getCharacterEncoding()), 10));
} else {
year = ((buf[pos] - 48) * 10) + buf[pos + 1] - 48;
cal.set(Calendar.MONTH, ((buf[pos + 2] - 48) * 10) + buf[pos + 3] - 49);
cal.set(Calendar.DATE, ((buf[pos + 4] - 48) * 10) + buf[pos + 5] - 48);
}
if (year > 50) {
cal.set(Calendar.YEAR, 1900+year);
} else {
cal.set(Calendar.YEAR, 2000+year);
}
return createValue(cal, false);
}
@Override
public <T> IsoValue<Date> parseBinary(int field, byte[] buf, int pos, CustomField<T> custom) throws ParseException, UnsupportedEncodingException {
if (pos < 0) {
throw new ParseException(String.format("Invalid DATE6 field %d position %d",
field, pos), pos);
}
if (pos+3 > buf.length) {
throw new ParseException(String.format("Insufficient data for DATE6 field %d, pos %d",
field, pos), pos);
}
int[] tens = new int[3];
int start = 0;
for (int i = pos; i < pos + tens.length; i++) {
tens[start++] = (((buf[i] & 0xf0) >> 4) * 10) + (buf[i] & 0x0f);
}
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
if (tens[0] > 50) {
cal.set(Calendar.YEAR, 1900+tens[0]);
} else {
cal.set(Calendar.YEAR, 2000+tens[0]);
}
cal.set(Calendar.MONTH, tens[1] - 1);
cal.set(Calendar.DATE, tens[2]);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND,0);
if (tz != null) {
cal.setTimeZone(tz);
}
return createValue(cal, true);
}
}
@@ -93,9 +93,6 @@ public class DateExpParseInfo extends DateTimeParseInfo {
cal.set(Calendar.YEAR, cal.get(Calendar.YEAR)
- (cal.get(Calendar.YEAR) % 100) + tens[0]);
cal.set(Calendar.MONTH, tens[1] - 1);
if (tz != null) {
cal.setTimeZone(tz);
}
return new IsoValue<>(type, cal.getTime(), null);
return createValue(cal, false);
}
}