Frage

I am using the following function that returns a formatted string date:

private static String formatDate(String format) {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat(format);

    String strDate = sdf.format(date);
    return strDate;
}

This function returns for example if i call it using the following format:

System.out.println(formatDate("MMYYHHMMSS"));

it prints: 04130904803

When I use the above date to create an ISO message eg.

// Create ISO Message
    ISOMsg isoMsg = new ISOMsg();
    isoMsg.setPackager(packager);
    isoMsg.setMTI("0200");
            isoMsg.set(2, "100002");
    isoMsg.set(3, "201234");
    isoMsg.set(7, "04130904803");
    isoMsg.set(11, "123456");
    isoMsg.set(44, "A5DFGR");
    isoMsg.set(105, "ABCDEFGHIJ 1234567890");


    // Get and print the output result
    byte[] data = isoMsg.pack();

            unpackISO(new String(data));

I get the following error: =>Error: error packing field 7 But when I remove the preceding zero of date in field 7, I works fine. What date format should i use in java to format the date? Help!

War es hilfreich?

Lösung 3

You use ISODate.getDateTime(Date d) or ISODate.getDateTime(Date, TimeZone).

See http://jpos.org/doc/javadoc/org/jpos/iso/ISODate.html

Andere Tipps

I'm guessing that you want MMYYHHMMSS to mean "month year hour minute second". So using the pattern specifiers documented for SimpleDateFormat, that should be "MMddHHmmss". (As noted in the comment below, the second part is meant to be day, not year.)

As an aside, I also strongly suggest that you don't use the String(byte[]) constructor: specify the encoding as another argument, either using String(byte[], String) or String(byte[], Charset).

The error seems to be caused by the length of the String given. The format you specified should be 10 characters long, but the String generated is 11 characters long. This is likely why removing the leading zero fixes the issue.

The reason you're getting a String one character too long is likely due to the reason Jon Skeet provided; the patter specifiers for SimpleDateFormat.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top