http://www.roseindia.net/java/beginners/java-write-to-file.shtml shows how to save a file. I have a better way to do it - get the date that the program was first downloaded at, save that in the file, then get the current date when the program is opened. Then find how many days are in between and if that amount exceeds your "x" amount of days, disallow access to the program functions.
That way won't work at all. Nothing prevents the user from just discarding the file. To make it harder, you can save the data in a hard to find place, like a hidden folder, or the Windows registry, but this won't work against someone who's determined to circumvent your timer.
To make it more reliable, you can try to save the data on a server, by mapping the user's IP address to the time of installation. This can be circumvented using proxy servers however.
I thought about almost all other options mentioned here, but could not think a way to prevent user from changing the date. E.g. even if someone puts the installation date in a file and encrypt it and put it in secrete place and all, what if user manually changes the date before running the program? It would never expire.
Now, this 'synchronizing time from server' thing would work here
Any idea how to do it problematically? Thanks in advance.
Who will be using your application? For example, if your clients are primarily corporations, your implementation does not need to be very secure. Corporations generally cannot afford (or genuinely do not want to) to use unlicensed software.
If, on the other hand, you plan to offer your application "in the wild" and your application is so splendid that it is worth to be hacked, it will be hacked. It will happen. The best protection I can think about would be to set up a 7/24 server and have your users authenticate online against that server whenever using that application, but even this can be circumvented either by modifying the application, or by setting up a fake authentication server somewhere.
I believe the best approach is implementing a remainder that will not bother so much to make someone expend the effort to remove it. That way, even if your application is pirated, the users will know the author and some of them might actually pay for your app.