Skip to content

Commit

Permalink
Fix writing dates before 1000 year
Browse files Browse the repository at this point in the history
  • Loading branch information
albfernandez committed Aug 22, 2020
1 parent 5d725f5 commit ad89998
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/com/linuxense/javadbf/DBFWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ private void writeRecord(DataOutput dataOutput, Object[] objectArray) throws IOE
if (objectArray[j] != null) {
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime((Date) objectArray[j]);
dataOutput.write(String.valueOf(calendar.get(Calendar.YEAR)).getBytes(StandardCharsets.US_ASCII));
dataOutput.write(DBFUtils.textPadding(String.valueOf(calendar.get(Calendar.YEAR)),
StandardCharsets.US_ASCII, 4, DBFAlignment.RIGHT, (byte) '0'));
dataOutput.write(DBFUtils.textPadding(String.valueOf(calendar.get(Calendar.MONTH) + 1),
StandardCharsets.US_ASCII, 2, DBFAlignment.RIGHT, (byte) '0'));
dataOutput.write(DBFUtils.textPadding(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)),
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/com/linuxense/javadbf/bug_dates/BugDatesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.linuxense.javadbf.bug_dates;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Timestamp;
import java.util.GregorianCalendar;

import org.junit.Assert;
import org.junit.Test;

import com.linuxense.javadbf.DBFDataType;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFReader;
import com.linuxense.javadbf.DBFWriter;

public class BugDatesTest {

public BugDatesTest() {
super();
}

@Test
public void test1() throws Exception {
File dbfFile = File.createTempFile("dates", ".dbf");
try {
DBFWriter dbfWriter = new DBFWriter();
DBFField field = new DBFField();
field.setName("date"); // give a name to the field
field.setType(DBFDataType.DATE); // and set its type
dbfWriter.setFields(new DBFField[] {field});
GregorianCalendar calendar = new GregorianCalendar(2,1,1);
Timestamp date = new Timestamp(calendar.getTime().getTime());
dbfWriter.addRecord(new Object[] {date});

OutputStream os = new FileOutputStream(dbfFile);
dbfWriter.write(os);
dbfWriter.close();
os.close();


InputStream is = new FileInputStream(dbfFile);
DBFReader reader = new DBFReader(is);
Object[] o = reader.nextRecord();
is.close();
Object value = o[0];
System.out.println(value);
Assert.assertEquals(calendar.getTime(), value);
}
finally {
dbfFile.delete();
}
}

}

0 comments on commit ad89998

Please sign in to comment.