Reputation: 1801
Hi i am having a problem with date. The date returned by Java and PHP does not matches my system date.
Here is my system settings date and time
Date = 05-13-2013 10:58 AM
Time Zone Info = (UTC+05:00) Islamabad, Karachi
Java
DateFormat df = new SimpleDateFormat("MM-dd-yyyy hh:mm a");
System.out.println(df.format(new Date()));
TimeZone zone = TimeZone.getDefault();
System.out.println(zone.getDisplayName());
System.out.println(zone.getID());
result:
05-13-2013 12:58 AM
Central Standard Time
America/Mexico_City
PHP
echo ini_get('date.timezone')." ======= ".date_default_timezone_get()."<hr>";
echo date("m-d-Y h:i:s A", mktime());
result
UTC ============== UTC
05-13-2013 06:02:21 AM
I tried every thing but unable to fix this issue, Some people have suggested to set Time Zone in php.ini to fix this issue, but i want programming languge should pick the system timezone and date & time.
I need solution for JAVA Is it possible for Java code to pick the date and time(Timezone) of the system, instead of some configuration settings. (As i want my code should be transparent, it should run on any server anywhere in the world with any time zone, that must be of the system)
If above is not possible where timezone settings can be configured (Please note i am running standalone application and as well as an application on JBOSS, which schedule tasks).
EDIT:
What i have tried for successful result running a java Class which print date.
Standalone
javac MyCurrentDate.java
java -Duser.timezone="GMT+5" MyCurrentDate
print it correctly
Jboss i have added -Duser.timezone param in run.bat
set JAVA_OPTS=%JAVA_OPTS% -Xms512m -Xmx1124m -XX:PermSize=256m -Duser.timezone="GMT+5"
but again i am looking for global solution (Every instance of java code should return correct date as of system date)
Few people have recommended Timezone Updater Tool. But i belive it only updates the list of timezone for JDK and JRE, and there is no such option to change current timezone. http://www.oracle.com/technetwork/java/javase/tzupdater-readme-136440.html
Upvotes: 1
Views: 918
Reputation: 101
Which operating system are you using? I have seen similar issues with locales on Windows, where the Local System Account had a different locale than the domain users.
Thereby, processes running as services had a different locale than processes started by a domain user.
My solution was to log on the server console with mstsc /console
and change the timezone for the server.
Edit: I have to say that I'm a Java developer so my primary work is not server maintenance, and the issue happened a while back. My experience was on a Windows 2003 server, as I recall, which had been installed by the company's IT management department.
When I logged in as a user through remote desktop, I could change the timezone to the correct one, but the applications server which run as a service did still use the old (wrong) timezone even after restart.
I had to log in through the console as mentioned in the original post (probably as a local administrator) before I got the correct timezone set for the application server.
Upvotes: 1