如何从COBOL的COMP-3字段中读取Java中的日期? [英] How To Read a Date in Java from a COMP-3 field in COBOL?
问题描述
我正在尝试使用 JRecord 读取COBOL数据文件,具有Header记录和Detail记录,因此我将SPLIT_01_LEVEL和CopyBook File格式解析为FMT_OPEN_COBOL.平面文件中的日期字段很少是COMP-3字段,我无法理解如何将其转换为Java日期字段.
I am trying to read a COBOL data file using JRecord, in that I have a Header record and Detail record, so I parsed with SPLIT_01_LEVEL and CopyBook File format as FMT_OPEN_COBOL. I have few date fields in the flat file as COMP-3 fields, and I am unable to understand on how to convert them into Java Date fields.
ICobolIOBuilder iob = CobolIoProvider.getInstance()
.newIOBuilder(copybookName)
.setCopybookFileFormat(Convert.FMT_OPEN_COBOL)
.setSplitCopybook(CopybookLoader.SPLIT_01_LEVEL);
//I fetched fields as below
line.getFieldValue(field).asString();
CopyBook的字段为
The CopyBook have fields as
MPOH-ENTRY-DATE PIC S9(7) COMP-3.
MPOH-STATUS-DATE PIC S9(7) COMP-3.
MPOH-APPROVED-DATE PIC S9(7) COMP-3.
MPOH-ORDER-DATE PIC S9(7) COMP-3.
当我如上所述进行解析时,输出为
When I parsed as above the output is
MPOH-ENTRY-DATE : 11261a1
MPOH-STATUS-DATE : 11261a1
MPOH-APPROVED-DATE : 11261a1
MPOH-ORDER-DATE : 11266140
请帮助我将这些字段转换为Java Date字段.
Please help me in converting these fields to Java Date fields.
推荐答案
最大的问题是,已经有 EBCDIC到ascii的转换.
The big problem is there has been an EBCDIC to ascii conversion.
JRecord构建器的创建
The JRecord builder creation
ICobolIOBuilder iob = CobolIoProvider.getInstance()
.newIOBuilder(copybookName)
.setCopybookFileFormat(Convert.FMT_OPEN_COBOL)
.setSplitCopybook(CopybookLoader.SPLIT_01_LEVEL);
不包含 setFont ,因此在Unix/Linux/Windows PC上,这表示文件是ASCII.如果您在Window/Linux/Unix上运行,并且文件是在大型机上创建的,那不好吗?数据是否真的来自
does not include the setFont, so on a Unix / Linux / Windows PC, this indicates the file is ASCII. Which is not good if you are running on Window / Linux / Unix and the file was created on the mainframe, Also is the Data really comming from GNUCobol ???.
数据看起来已经通过EBCDIC-> Ascii转换了???或可能移位1个字节.如果确实是GNU_Cobol,则可能需要其他格式之一,例如FMT_OPEN_COBOL_MVS
The data looks to has been through a EBCDIC -> Ascii conversion ??? or possibly shifted by 1 byte. If it really is GNU_Cobol you may need one of the other formats e.g. FMT_OPEN_COBOL_MVS
以下所有4个数字都是无效的comp-3 数字:
All 4 of the following numbers are not valid comp-3 numbers:
MPOH-ENTRY-DATE : 11261a1
MPOH-STATUS-DATE : 11261a1
MPOH-APPROVED-DATE : 11261a1
MPOH-ORDER-DATE : 11266140
MPOH-ORDER-DATE现在为x'11 26 61 40',而EBCDIC原件可能是 x'11 50 81 7c'即
The MPOH-ORDER-DATE is now x'11 26 61 40' while the EBCDIC original may of been x'11 50 81 7c' i.e.
CYY = 115 (or 2015)
MM = 08
DD = 17
所以您需要
So you need to
- 二进制传输以获取原始EBCDIC文件.如果它是大型机上的RECFM = VB文件,请先将其转换为RECFM = FB.
-
将setFont("cp037")添加到IOBuilder步骤(如果您使用的是美国ebcdic.不同国家/地区的EBCDIC有所不同,例如德国的cp273).
- A binary transfer to Get the raw EBCDIC file. If it is a RECFM=VB file on the mainframe, convert it to RECFM=FB first.
Add setFont("cp037") to the IOBuilder step (if you are using US ebcdic. There are different EBCDIC's for different countries e.g. cp273 for germany) .
ICobolIOBuilder iob = CobolIoProvider.getInstance()
.newIOBuilder(copybookName)
.setCopybookFileFormat(Convert.FMT_MAINFRAME)
.setSplitCopybook(CopybookLoader.SPLIT_01_LEVEL)
.setFont("cp037");
对于日期而言,日期看起来应该是CYYMMDD格式,其中C = 0为1900,C = 1为2000
For what it worth, the Dates look to be in a CYYMMDD format where C=0 is 1900 and C=1 is 2000
如果我不正确,请提供原始数据和本抄写本
If I am not correct, please provide the raw data and the Copybook
另一种选择是在字帖替换中出现1个字节的错误
The other alternative is a 1 byte error in the copybook displacement
例如
MPOH-ENTRY-DATE : 1?11261
MPOH-STATUS-DATE : 1?11261
MPOH-APPROVED-DATE : 1?11261
MPOH-ORDER-DATE : 112661
但是它看起来不像日期
Convert.FMT_MAINFRAME和Convert.FMT_OPEN_COBOL之间没有太大区别.但是这些是不同的:
There not a big difference between Convert.FMT_MAINFRAME and Convert.FMT_OPEN_COBOL. But these are the differences:
- GNU Cobol具有1、2、4、8个字节的二进制整数,而大型机具有2、4、8个
- GNU-Cobol中的Comp-5(在intel硬件上)几乎没有结束(大型机是big-endian).
- 小数点后的数字不同
- 不同的浮点(comp-1,comp-2).
您会在以下字段中看到不同之处:
The following fields are where you would seee a difference:
03 f1 pic s9(3).
03 f2 pic s99 comp.
03 f3 pic s9(4) comp-5
03 f4 comp-1.
03 f5 comp-2.
这篇关于如何从COBOL的COMP-3字段中读取Java中的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!