简体   繁体   中英

SimpleDateFormat returns wrong date value during parse

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM