Java Calendar returning wrong time

Posted on 2009-04-22
Last Modified: 2012-05-06
I have an app running on two Centos machine. The app running on both servers is identical
when I run the following code, on one machine I get correct time but on the second server, I get 5 hrs early time.

Corret : 22/04/2009 10:22:23
Wrong : 22/04/2009 05:22:23

linux date command and locale command outputs for both servers are same. Can someone tell me where else I need to check?

Calendar cal = Calendar.getInstance();

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

        return sdf.format(cal.getTime());

Open in new window

Question by:bilgehanyildirim
    LVL 92

    Accepted Solution

    sounds like the default tz is different on the two boxes, check that

    LVL 92

    Expert Comment

    and check the TZ env var

    echo $TZ

    LVL 92

    Expert Comment

    >         SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

    and change that to

            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss z zzz");

    that way we can see the tz being used


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
    The viewer will learn how to implement Singleton Design Pattern in Java.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now