我尝试了不同的public ServiceCall<CreateDocumentResponse> createDocument(CreateDocumentRequest createRequest) {
Validator.notEmpty(createRequest.getEnvironmentId(), "environment_id cannot be empty");
Validator.notEmpty(createRequest.getCollectionId(), "collection_id cannot be empty");
RequestBuilder builder = RequestBuilder.post(String.format("/v1/environments/%s/collections/%s/documents", new Object[] { createRequest.getEnvironmentId(), createRequest.getCollectionId() }));
if (createRequest.getConfigurationId() != null) {
builder.query(new Object[] { "configuration_id", createRequest.getConfigurationId() });
}
Validator.notNull(createRequest.getFile(), "Document file cannot be null");
MediaType mediaType = (MediaType)supportedMediaTypes.get(createRequest.getMediaType());
Validator.notNull(mediaType, String.format("Media Type '%s' not supported", new Object[] { createRequest.getMediaType() }));
RequestBody file = InputStreamRequestBody.create(mediaType, createRequest.getFile());
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.setType(MultipartBody.FORM);
multipartBuilder.addFormDataPart("file", "filename", file);
multipartBuilder.addFormDataPart("metadata", createRequest.getMetadata().toString());
builder.body(multipartBuilder.build());
Request request = createVersionedRequest(builder);
return createServiceCall(request, ResponseConverterUtils.getObject(CreateDocumentResponse.class));
}
(ISODateTimeFormats
,dateHourMinuteSecond()
)和我自己的格式dateTimeNoMillis()
,以便在没有毫秒的情况下将时间戳解析为日期时间对象,但失败了。
对于每种格式,我都试图相应地修改字符串:
"yyyy-MM-dd'T'HH:mm:ss"
输出:
String value = "2011-01-24T09:45:00.000";
DateTimeFormatter formatter = ISODateTimeFormat.dateHourMinuteSecond();
value = value.substring(0, value.lastIndexOf('.'));
System.out.println("Formatted dateTime: "+formatter.parseDateTime(value));
答案 0 :(得分:3)
我在 jodatime 2.2 中进行了测试。
formatter.parseDateTime(value)
会返回DateTime
个对象。将此对象传递给System.out.println
时,它会调用此toString()
对象的DateTime
方法。看一下源代码,我发现它调用ISODateTimeFormat
:
// (from jodatime source)
/**
* Output the date time in ISO8601 format (yyyy-MM-ddTHH:mm:ss.SSSZZ).
*
* @return ISO8601 time formatted string.
*/
@ToString
public String toString() {
return ISODateTimeFormat.dateTime().print(this);
}
要获得所需的输出(无毫秒),您需要格式化DateTime
对象:
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
value = value.substring(0, value.lastIndexOf('.'));
DateTime datetime = formatter.parseDateTime(value);
// *** create the formatter with the "no-millis" format ***
DateTimeFormatter formatterNoMillis = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
System.out.println("Formatted dateTime: " + datetime.toString(formatterNoMillis));
输出将是:
Formatted dateTime: 2011-01-24T09:45:00
你也可以这样做:
System.out.println("Formatted dateTime: " + formatterNoMillis.print(datetime));
//the output will be the same