We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Set Created & Last Saved Times of Build in Word Document Properties in UTC or Local Time Zones - Java

We’re setting the createdTime and lastSavedTime to the current time in UTC and would expect the UTC time to be saved in the word document. But it’s saved in the current timezone BUT marked as UTC. Here’s some example code:

final Document document = new Document();
document.getBuiltInDocumentProperties().clear();
final Date nowInUTC = Date.from(Instant.now());
document.getBuiltInDocumentProperties().setCreatedTime(nowInUTC);
document.getBuiltInDocumentProperties().setLastSavedTime(nowInUTC);
document.save("output.docx");
document.save("output.pdf");

See files.zip for the produced files. If you look in the pdf_properties.jpg, the time is given as June 2nd 2021, 11:15 local time. The document was created at June 2nd 2021, 09:15 local time however. The created and modified values in core.xml in the produced word document specify the time as June 2nd 2021, 09:15 in UTC time! Which automatically adds 2 hours (in my case). Am I doing something wrong or is this just a bug? Shouldn’t the core.xml specify the time as 07:15 UTC?

files.zip (47.1 KB)

@bmpi,

I think, you can workaround this problem by using the following Java code:

Document document = new Document();
document.getBuiltInDocumentProperties().clear();

Date localDate = Date.from(Instant.now());
OffsetDateTime nowInUTC = OffsetDateTime.now(ZoneOffset.UTC);
Date utcDate = new Date(localDate.getYear(), localDate.getMonth(), nowInUTC.getDayOfMonth(),
        nowInUTC.getHour(), nowInUTC.getMinute(), nowInUTC.getSecond());

document.getBuiltInDocumentProperties().setCreatedTime(utcDate);
document.getBuiltInDocumentProperties().setLastSavedTime(utcDate);

document.save("C:\\Temp\\UTC\\output2.docx");
document.save("C:\\Temp\\UTC\\output2.pdf");

Thanks, we’ll use something like that then. The Date-constructor is deprecated though that’s why we used a slightly different code snippet:

OffsetDateTime nowInUTC = OffsetDateTime.now(ZoneOffset.UTC);
final LocalDateTime dateTime = LocalDateTime.of(nowInUTC.getYear(), nowInUTC.getMonth(), nowInUTC.getDayOfMonth(), nowInUTC.getHour(), nowInUTC.getMinute(), nowInUTC.getSecond());
final Date utcDate = Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
1 Like