Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting date in java on Centos OS is not correct?

Greetings all i have a j2ee app using spring framework and while debugging on the online server (centos os) when trying to get the date in the app by printing the value of new Date(); object it retrieves the time in GMT-6 although when getting the server time through the terminal command date it's retrieved in GMT time, the real time, so i am wondering where does the GMT-6 came from, why such strange behaver occurs ?

like image 709
Mahmoud Saleh Avatar asked Apr 30 '26 00:04

Mahmoud Saleh


1 Answers

This sounds like a locale issue to me. More specifically, it's not an issue, it's just Java's way of handling different timezone/regional settings.

Using code from this article:

Properties p = System.getProperties();
Enumeration keys = p.keys();
while (keys.hasMoreElements()) {
  String key = (String)keys.nextElement();
  String value = (String)p.get(key);
  System.out.println(key + ": " + value);
}

you can see how Java interprets your environment.

If you change CentOS' timezone/regional settings, Java should pick up that change and display time accordingly.

But that sounds like an overkill and customizing date format based on locale might be what you need.

Different applications may output the same date differently.

like image 156
darioo Avatar answered May 01 '26 14:05

darioo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!