1

I m facing a problem:I want to get current time of GMT TimeZone in long. I m using the following code as given below:

  TimeZone timeZoneGmt = TimeZone.getTimeZone("GMT");
  long gmtCurrentTime = getCurrentTimeInSpecificTimeZone(timeZoneGmt);

    public static long getCurrentTimeInSpecificTimeZone(TimeZone timeZone) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(timeZone);
    long finalValue = 0;
    SimpleDateFormat sdf = new SimpleDateFormat(
            "MMM dd yyyy hh:mm:ss:SSSaaa");

    sdf.setTimeZone(timeZone);

    Date finalDate = null;

    String date = sdf.format(cal.getTime());
    try {
        finalDate = sdf.parse(date);

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    finalValue = finalDate.getTime();
    return finalValue;
}

As given in, above method while formatting
String date = sdf.format(cal.getTime()); I m getting correct current time in GMT but as i do parsing by following code:

finalDate=sdf.parse(date);

Date got changed from current GMT time to 15:35:16 IST 2013 that is current time of my system.

I tried with Calendar as well in another way:

TimeZone timeZoneGmt=TimeZone.get("GMT"); 
Calendar calGmt = Calendar.getInstance(); 
calGmt.setTimeZone(timeZoneGmt); 
long finalGmtValue = 0; 
finalGmtValue = calGmt.getTimeInMillis(); 
System.out.println("Date......" + calGmt.getTime()); 

but still getting date as current time of my System Thu Jan 23 15:58:16 IST 2014 Not getting GMT current time.

1 Answer 1

7

You've misunderstood how Date works. A Date doesn't have a time zone - if you use Date.toString() you'll always see the default time zone. The long value in a Date is purely the number of milliseconds since the Unix epoch: it doesn't have any concept of time zone or calendar system.

If you want to represent a date and time in a particular time zone and calendar, use Calendar instead - but for getting "the current date and time as a long" you can just use System.currentTimeMillis(), which again does not have anything to do with the system time zone.

Additionally, even if you did want to do manipulation like this, you shouldn't be using string conversions. You're not conceptually performing any string conversions, so why introduce them?

If your aim is to display (as a string) the current date and time in a particular time zone, you should just use something like:

Date date = new Date(); // This will use the current time
SimpleDateFormat format = new SimpleDateFormat(...); // Pattern and locale
format.setTimeZone(zone); // The zone you want to display in

String formattedText = format.format(date);

When working with date and time APIs - particularly bad ones like the Java Calendar/Date API - it's very important that you understand exactly what each value in your system represents.

Sign up to request clarification or add additional context in comments.

8 Comments

I want current date of a particular time zone in long.That is the reason,i m parsing date String.
@user1862243: What do you expect that long value to be? Because as I say, the normal meaning of a long with respect to date/time values in Java is "milliseconds since the Unix epoch" which has no concept of a time zone. What are you planning on doing with that value?
@user1862243: Edit your attempts into your question rather than adding them as comments, but I think you've fundamentally misunderstood the data types you're using. Please include in your question what you're intending to do with the long afterwards.
@user1862243: As I said 5 minutes ago, edit your code into the question... but I don't think you've really read my answer thoroughly. I've explained why you got that result - Date.toString() always uses the current time zone. The time zone isn't part of the Date. I'm not sure how many other ways I can say it...
@user1862243: That still leaves the question of what you intend to do with the long - and indeed what bigger problem you're trying to solve in the first place. I strongly suspect you're making your life far more complicated than it needs to be. Have you fully read my answer and now understand it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.