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.

kittu
  • 265
  • 1
  • 5
  • 17

1 Answers1

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I want current date of a particular time zone in long.That is the reason,i m parsing date String. – kittu Jan 23 '14 at 10:24
  • @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? – Jon Skeet Jan 23 '14 at 10:28
  • @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. – Jon Skeet Jan 23 '14 at 10:29
  • @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... – Jon Skeet Jan 23 '14 at 10:36
  • Sorry i just added code in question.I got about Date as default time Zone.Thanks for reply. – kittu Jan 23 '14 at 10:42
  • 1
    @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? – Jon Skeet Jan 23 '14 at 10:43
  • Hey,i dont have much idea about date formatting.I already mentioned i got your point that Date use current time zone.There is nothing as such for setting timezones in Date. Answer to your question: what you intend to do with the long? I need to add Current Date of GMT time Zone(in long milliseconds) to another date of different time zone(in long milliseconds). – kittu Jan 23 '14 at 10:52
  • @kittu: It's not that "Date use current time zone" - it's that calling `Date.toString()` uses the current time zone. As for adding two dates together - that makes no sense. You *can't* add dates together - it just isn't a logical operation. What's "June 10th 2013 + February 1st 2014" for example? You should clarify your actual requirements in the question. – Jon Skeet Jan 23 '14 at 10:56