PDFbox Document Propperties will not set - metadata

In Tutorialspoint is learn about PDFbox.
PDFbox Tutorialpoint
I put an method in my programm to update the metadata from an existing PDF-File.
The Document Properties will not set and i spent many hours to find out why.
When the modification date ".setModificationDate()" will be wrong or coment out, the meatadata of documet properties will not changed.
I dont want to put a static date as the tutorial, i want to insert variables.
//Setting the created date of the document
Calendar date = new GregorianCalendar();
date.set(2015, 11, 5);
//Setting the modified date of the document
date.set(2016, 6, 5);
// Save the metadata to file
pdfDocument.setModificationDate(date);
The data of the ".setModificationDate()" method are want a "Calendar" type.
I do it on different ways and nothing will be work.
Try first
Calendar transfomedDate = new GregorianCalendar();
transfomedDate = convertStringDateToCalendar(StringDate);
Calendar modifiedDate = new GregorianCalendar();
modifiedDate.set(transfomedDate.get(Calendar.YEAR),
transfomedDate.get(Calendar.MONTH),
transfomedDate.get(Calendar.DAY_OF_MONTH),
transfomedDate.get(Calendar.HOUR_OF_DAY),
transfomedDate.get(Calendar.MINUTE),
transfomedDate.get(Calendar.SECOND));
pdfDocument.setModificationDate(modifiedDate);
This Methos will be work correctly on creating Date but not on mofdificationDate?
Then i will trying to put the parameters of the modification in "int" variables and use it on this way.
Calendar transfomedDate = new GregorianCalendar();
transfomedDate = convertStringDateToCalendar(StringDate);
int year = transfomedDate.get(Calendar.YEAR);
int month = transfomedDate.get(Calendar.MONTH);
int day = transfomedDate.get(Calendar.DAY_OF_MONTH)
Calendar modifiedDate = new GregorianCalendar();
// modifiedDate.set(2017, 10, 11);
modifiedDate.set(year, month, day);
pdfDocument.setModificationDate(modifiedDate);
It will not work to.
I dont knwo why PDFbox will not aipdate the metadate when the modficationDate will be commet out or be wrong. I can be ignore the it are wrong?
I dont know what i can do now?
Thanks
Mike

Related

Add day to date with momentjs

I am trying to add a day to my date:
let createdDate = moment(new Date()).utc().format();
let expirationDate = moment(createdDate).add(1, 'd');
console.log(expirationDate);
However, this keeps returning an obscure object {_i: "2017-12-20T21:06:21+00:00", _f: "YYYY-MM-DDTHH:mm:ss Z", _l: undefined, _isUTC: false, _a: Array(7), …}
fiddle:
http://jsfiddle.net/rLjQx/4982/
Does anyone know what I might be doing wrong?
You are logging a moment object. As the Internal Properties guide states:
To print out the value of a Moment, use .format(), .toString() or .toISOString().
let createdDate = moment(new Date()).utc().format();
let expirationDate = moment(createdDate).add(1, 'd');
console.log(expirationDate.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
Please note that you can get the current date using moment() (no need to use new Date()) or moment.utc().
I will go with this one, simple works for me and I don't think you need other function to only add day in moment.
var yourPreviousDate = new Date();
var yourExpectedDate = moment(yourPreviousDate).add(1, 'd')._d;
The add method modifies the moment object. So when you log it, you're not getting an obscure object, you're getting the moment object you're working with. Are you expecting a formatted date? Then use format or some other method.
I agree with other answers just providing shortcut and different ways
You can do the format at the same time
moment().add(1,'d').format('YYYY-MM-DD');
or you can just format any date or date object
moment(result.StartDate).format('YYYY-MM-DD');

How to return a normal javascript date object?

Is it possible to return a javascript date object from this? I have an text input field with the calendar and want to return a standard date object from it's value... Is this possible?
Is this what you are looking for?
var date = '2016-06-12'; // Can be document.getElementById('myField').value that points to your date field.
var date_parts = date.split('-');
var date_obj = new Date(date_parts[0],date_parts[1],date_parts[2]);
console.log(date_obj);
You can also simply use
new Date(document.getElementById('myField').value)
and see if it works. The date function is smart enough to parse based on browser's locale. This should work for time as well. Eg. new Date('2016-06-12 04:15:30')

Change times of a reservation (timestamp) using Listbox in GWT

I want to implement a function (with GWT) to change the start- and end-date/time of a reservation!
The start- & end-date of the reservation is saved as a timestamp.
The reservations are saved in a mySQL DB.
The user can choose the day in a datepicker:
final DatePicker chooseDay = new DatePicker();
..
Date startDate = reservation.getStartDate();
..
chooseDay.setValue(startDate);
..
2 ListBoxes:
final ListBox startTime = new ListBox();
final ListBox endTime = new ListBox();
..
startTime.addItem("08:00");
startTime.addItem("08:30");
startTime.addItem("09:00"); (til 18:00)
...
endTime.addItem("08:00");
endTime.addItem("08:30");
endTime.addItem("09:00"); (til 18:00)
I now have the problem, that i don't know how to change between the formats. Reservations are saved as a timestamp, but how can i change just day and hour/minute?
I am a beginner and it would be really nice if you can help me. Thank you :)
Use can use JsDate on the client side:
JsDate jsDate = JsDate.create(startDate.getTime());
int hour = jsDate.getHours();
int mins = jsDate.getMinutes();

How do I convert a Date to an int?

I have this
DateFormat dateFormat = new SimpleDateFormat("MM");
Date date = new Date();
I know the value as 10 but have got no idea how to print it out. How do I convert it to an int? If there even is a way?
date.getMonth()
it is deprecated - you should be using Calendar, but if you don't want to change, that'll do it. It is 0 indexed, so remember to change the resulting value appropriately.
Javadoc

How to modify the date?

I cannot get the add function in Blackberry Java.
// Date
private static DateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");
private static Calendar cal = Calendar.getInstance();
public static final String date = dateFormat.format(cal.getTime())
.toString();
The cal variable don't have add function because I want to reduce 1 day from current date.
The source stated that used cal.add(Calendar.DATE, -1);.
The following solution works when using Java SE. I haven't verified using BlackBerry Java ME yet. But, given that I am only using functions that exist in both the SE version of Calendar and the BlackBerry version of Calendar, hence I have a good feeling about the accuracy of this solution. Append these lines to your code:
long curTime = cal.getTimeInMillis();
curTime -= 1000*60*60*24;
cal.setTimeInMillis(curTime);
System.out.println(dateFormat.format(cal.getTime()).toString());
Try this:
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(cal.getTime() - DateTimeUtilities.ONEDAY);
Updated, based on ecb0628's answer and paulkayuk's comment.
Check Calendar, getTimeInMilis(), setTimeInMillis(long millis), and DateTimeUtilities.ONEDAY.