/** * @return date in yyMMdd format - suitable for ANSI field 8 */ public static String getANSIDate(Date d) { return formatDate (d, "yyMMdd"); } /**
/** * converts a string in DD/MM/YY format to a Date object * Warning: return null on invalid dates (prints Exception to console) * Uses default time zone for this host * @return parsed Date (or null) */ public static Date parse(String s) { return parse(s, TimeZone.getDefault()); } /**
/** * converts a string in DD/MM/YY HH:MM:SS format to a Date object * Warning: return null on invalid dates (prints Exception to console) * Uses default time zone for this host * @return parsed Date (or null) */ public static Date parseDateTime(String s) { return parseDateTime(s, TimeZone.getDefault()); } /**
/** * try to find out suitable date given [YY[YY]]MMDDhhmmss format<br> * (difficult thing being finding out appropiate year) * @param d date formated as [YY[YY]]MMDDhhmmss, typical field 13 + field 12 * @return Date */ public static Date parseISODate (String d) { return parseISODate (d, System.currentTimeMillis()); }
private ISOMsg updatePackAndUnpack (ISOMsg m, ISOPackager p) throws Exception { Date now = new Date(); m.setPackager (p); m.set(7, ISODate.getDateTime(now)); m.set (12, ISODate.formatDate(now, "HHmmss")); m.set (13, ISODate.formatDate (now, "MMdd")); int stan = Integer.parseInt(m.getString(11)) % 1000000; if (++stan == 0L) stan++; m.set (11, Integer.toString(stan)); m.set (4, Integer.toString (stan)); ISOMsg m1 = new ISOMsg(); m1.setPackager(p); m1.unpack (m.pack()); return m1; } private void doTest (ISOPackager packager, String msg, String img)
continue; if (value.equalsIgnoreCase ("$date") ) m.set (new ISOField (i, ISODate.getDateTime(new Date()))); else if (value.toLowerCase().startsWith ("$date") && value.contains("GMT")) { String zoneID = value.substring(value.indexOf("GMT")); m.set (new ISOField (i, ISODate.getDateTime(new Date(), TimeZone.getTimeZone(zoneID))));
@Test public void testGetANSIDate() { GregorianCalendar cal = new GregorianCalendar(aus); cal.set(2011, 02, 25); String result = ISODate.getANSIDate(cal.getTime(), aus); assertThat(result, is("110325")); }
@Test public void testGetExpirationDate() { GregorianCalendar cal = new GregorianCalendar(aus); cal.set(2011, 02, 25); String result = ISODate.getExpirationDate(cal.getTime(), aus); assertThat(result, is("1103")); }
@Test public void testGetDate() { GregorianCalendar cal = new GregorianCalendar(aus); cal.set(2011, 02, 25); String result = ISODate.getDate(cal.getTime(), aus); assertThat(result, is("0325")); }
@Test public void testGetEuropeanDate() { GregorianCalendar cal = new GregorianCalendar(aus); cal.set(2011, 02, 25); String result = ISODate.getEuropeanDate(cal.getTime(), aus); assertThat(result, is("250311")); }
/** * try to find out suitable date given [YY[YY]]MMDDhhmmss format<br> * (difficult thing being finding out appropiate year) * @param d date formated as [YY[YY]]MMDDhhmmss, typical field 13 + field 12 * @param currentTime currentTime in millis * @return Date */ public static Date parseISODate (String d, long currentTime) { return parseISODate (d, currentTime, TimeZone.getDefault() ); } /**
@Test public void testGetDateTime() { // given Calendar cal = new GregorianCalendar(aus); cal.set(2011, Calendar.FEBRUARY, 25, 23, 55, 56); cal.set(Calendar.MILLISECOND, 23); Date date = cal.getTime(); // when String result = ISODate.getDateTime(date, aus); // then assertThat(result, is("0225235556")); Calendar cal2 = new GregorianCalendar(TimeZone.getDefault()); cal2.set(2011, Calendar.FEBRUARY, 25, 23, 55, 56); cal2.set(Calendar.MILLISECOND, 23); Date date2 = cal2.getTime(); // when String result2 = ISODate.getDateTime(date2, TimeZone.getDefault()); // then assertThat(result2, is("0225235556")); }
private ISOMsg createDummyRequest() { ISOMsg m = new ISOMsg("0800"); m.set(7, ISODate.getANSIDate(new Date())); m.set(11, ISOUtil.zeropad(++stan, 6)); m.set(70, "301"); return m; } }
/** * @return date in MMddHHmmss format suitable for FIeld 7 */ public static String getDateTime (Date d) { return formatDate (d, "MMddHHmmss"); } /**
/** * try to find out suitable date given [YY[YY]]MMDDhhmmss format<br> * (difficult thing being finding out appropiate year) * @param d date formated as [YY[YY]]MMDDhhmmss, typical field 13 + field 12 * @param timeZone for GMT for example, use TimeZone.getTimeZone("GMT") * and for Uruguay use TimeZone.getTimeZone("GMT-03:00") * @return Date */ public static Date parseISODate (String d, TimeZone timeZone) { return parseISODate (d, System.currentTimeMillis(), timeZone); }
@Test public void testParseDateZoneWithTimeSentReturnsOKButIgnoresTime() { long expectedDateWithoutTime = 1299074400000L; long unwantedDateIncludingTimeValue = 1299123895000L; java.util.Date result = ISODate.parse("31/02/2011 13:45:55", aus); assertThat(result.getTime(), allOf(is(expectedDateWithoutTime), is(not(unwantedDateIncludingTimeValue)))); }
@Test public void testParseStringTimeZoneNoTimeSentReturnsNull() { java.util.Date result = ISODate.parseDateTime("31/02/2011", aus); assertThat(result, is(nullValue())); }
@Test public void testValidTimestamps () { Date now = new Date(); Context ctx = new Context(); ISOMsg m = new ISOMsg(); m.set(7, ISODate.getDateTime(now)); m.set(12, ISODate.getDateTime(now)); ctx.put(ContextConstants.REQUEST.toString(), m); cfg.put("mandatory", "7 12"); int action = cf.prepare(1L, ctx); assertEquals(PREPARED | NO_JOIN | READONLY, action); Result rc = ctx.getResult(); assertFalse(rc.hasInfo()); assertFalse(rc.hasWarnings()); assertFalse(rc.hasFailures()); }
/** * @param d date object to be formatted * @param timeZone for GMT for example, use TimeZone.getTimeZone("GMT") * and for Uruguay use TimeZone.getTimeZone("GMT-03:00") * @return date in MMddHHmmss format suitable for FIeld 7 */ public static String getDateTime (Date d, TimeZone timeZone) { return formatDate (d, "MMddHHmmss", timeZone); } /**
private void putTimestamp (Context ctx, ISOMsg m, String key, int fieldNumber, boolean mandatory, Set<String> validFields, Result rc) { if (m.hasField(fieldNumber)) { String s = m.getString(fieldNumber); validFields.add(Integer.toString(fieldNumber)); if (TIMESTAMP_PATTERN.matcher(s).matches()) ctx.put (key, ISODate.parseISODate(s)); else rc.fail(CMF.INVALID_FIELD, Caller.info(), "Invalid %s '%s'", key, s); } else if (mandatory) { rc.fail(CMF.MISSING_FIELD, Caller.info(), TRANSMISSION_TIMESTAMP.toString()); } }