Project

General

Profile

« Previous | Next » 

Revision 52077

I really hope we are finally fine with date handling

View differences:

modules/dnet-oai-store-service/trunk/src/test/java/eu/dnetlib/data/oai/store/mongo/RecordInfoGeneratorTest.java
1 1
package eu.dnetlib.data.oai.store.mongo;
2 2

  
3 3
import java.io.IOException;
4
import java.text.ParseException;
4 5
import java.time.LocalDateTime;
5 6
import java.time.ZoneId;
6 7
import java.time.format.DateTimeFormatter;
......
57 58
		Date utilDate = Date.from(d.atZone(ZoneId.systemDefault()).toInstant());
58 59
	}
59 60

  
61
	@Test
62
	public void parseDatestamp1() throws ParseException {
63
		String theDate = "2018-04-07T04:23:31.8Z";
64
//		org.apache.commons.lang.time.DateUtils.parseDate(
65
//				theDate,
66
//				new String[]{ "yyyy-MM-dd'T'HH:mm:ss.SX" });
67
		gen.parseDate(theDate);
68
	}
69

  
70
	@Test
71
	public void parseDatestamp2() throws ParseException {
72
		String theDate = "2018-02-13T15:02:16.122Z";
73
		//		org.apache.commons.lang.time.DateUtils.parseDate(
74
		//				theDate,
75
		//				new String[]{ "yyyy-MM-dd'T'HH:mm:ss.SX" });
76
		gen.parseDate(theDate);
77
	}
78

  
79

  
80

  
60 81
	private Binary createCompressRecord(final String record) throws IOException {
61 82
		ByteArrayOutputStream os = new ByteArrayOutputStream();
62 83
		ZipOutputStream zos = new ZipOutputStream(os);
modules/dnet-oai-store-service/trunk/src/main/java/eu/dnetlib/data/oai/store/mongo/RecordInfoGenerator.java
3 3
import java.io.ByteArrayInputStream;
4 4
import java.io.IOException;
5 5
import java.io.StringReader;
6
import java.text.ParseException;
6 7
import java.time.LocalDateTime;
7 8
import java.time.ZoneId;
8 9
import java.time.format.DateTimeFormatter;
......
59 60
			//assuming we have a string in the form 2017-12-05T14:24:48.61Z to convert to Date
60 61
			log.debug("Why the hell there are string instead of Dates in datestamp???");
61 62
			try {
62
				LocalDateTime d = LocalDateTime.parse(datestampObject.toString(), dtf);
63
				Date utilDate = Date.from(d.atZone(ZoneId.systemDefault()).toInstant());
64
				record.setDatestamp(utilDate);
63
				record.setDatestamp(parseDate(datestampObject.toString()));
65 64
			}catch(Exception dateException){
66 65
				log.warn("Error setting date from a string datestamp, using current date instead: "+dateException.getMessage());
67 66
				record.setDatestamp(new Date());
......
85 84
			}
86 85
		}
87 86
		return record;
87
	}
88 88

  
89
	protected Date parseDate(final String date) throws ParseException {
90
		// date should be in the form: 2017-12-18 or 2017-12-18T12:00:04+00:00 or 2014-10-12T15:54:02.43Z or 2014-10-12T15:54:02.432Z or 2014-10-12T15:54:02.4Z
91
		try {
92
			LocalDateTime d = LocalDateTime.parse(date, dtf);
93
			return Date.from(d.atZone(ZoneId.systemDefault()).toInstant());
94
		}catch(Exception dateException){
95
			log.warn("Error parsing with DateTimeFormatter: "+dateException.getMessage());
96
			log.warn("Now trying with additional pattern with apache DateUtils...");
97
			return org.apache.commons.lang.time.DateUtils.parseDate(
98
					date,
99
					new String[]{ "yyyy-MM-dd", "yyyy-MM-dd'T'HH:mm:ssXXX", "yyyy-MM-dd'T'HH:mm:ss.SSSX","yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss.SX"});
100
		}
89 101
	}
90 102

  
91 103
	public  String decompressRecord(final byte[] input)  {
......
103 115
		} catch (IOException e) {
104 116
			throw new OaiPublisherRuntimeException(e);
105 117
		}
106

  
107

  
108 118
	}
109 119

  
110 120
	public MetadataExtractor getMetadataExtractor() {
modules/dnet-oai-store-service/trunk/pom.xml
64 64
			<version>[1.0.0,2.0.0)</version>
65 65
			<scope>test</scope>
66 66
		</dependency>
67
		<dependency>
68
			<groupId>commons-lang</groupId>
69
			<artifactId>commons-lang</artifactId>
70
			<version>2.6</version>
71
		</dependency>
67 72

  
68 73
	</dependencies>
69 74
</project>

Also available in: Unified diff