Question

I need to display time zone in CET in my java application. And I am using following code to achieve this.

String OLD_FORMAT = "yyyyMMdd HH:mm:ss";
String NEW_FORMAT = "dd.MM.yyyy HH:mm:ss";
String date = "20140217 14:45:28";

SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);

TimeZone zone = TimeZone.getTimeZone("GMT+1");
sdf.setTimeZone(zone);

Date d = null;
d = sdf.parse(date);
sdf.applyPattern(NEW_FORMAT);
date = sdf.format(d);

and I am using the date object to print the date on UI.

OR

    TimeZone zone = TimeZone.getTimeZone("CET");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
    sdf.setTimeZone(zone);

But using the either of above piece of code i am getting GMT time which is one hour behind CET.

FOr example if I execute the code now, I will get 1:32:50 PM where as its 2:32:50 PM as per http://wwp.greenwichmeantime.com/time-zone/europe/european-union/central-european-time/

Any one any idea what might be going wrong here ?

UPDATE : I have found the issue. I made a silly mistake as I had to set the time first to GMT (the datetime i was getting was in GMT) and then change it to CET. Its working now. Thanks much everyone for the reply.

No correct solution

OTHER TIPS

Maybe you are passing the wrong date to the SimpleDateFormat instance. I've written a small to test your code and it seems to work:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

    public class Test {
        public static void main(String[] args) {
            TimeZone zone = TimeZone.getTimeZone("GMT+1");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
            sdf.setTimeZone(zone);

            TimeZone zone2 = TimeZone.getTimeZone("CET");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
            sdf2.setTimeZone(zone2);

            Calendar c = Calendar.getInstance();
            c.set(Calendar.HOUR_OF_DAY, 15);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.setTimeZone(TimeZone.getTimeZone("GMT+3"));

            System.out.println(sdf.format(c.getTime()));
            System.out.println(sdf2.format(c.getTime()));

        }
    }

java.util.Date does not have a TimeZone, it's essentially a long (milliseconds since epoch). If you want to keep the timezone, you must use java.util.Calendar or even better, use joda-time

The second piece of code should do the trick.

Note that CET in java actually means CET in winter and CEST in summer which is what you want I assume. GMT+1 would not actually switch to summer time so you'd be stuck in winter time if you use that.

If the outputted value is still wrong you are giving it the wrong date to format.

Perhaps you made the same timezone error when parsing the date?

Avoid 3-Letter Codes

Those three-letter time zone codes are neither standardized nor unique. And they get confusing with regards to Daylight Saving Time (DST). Instead use proper time zone names.

There are a few dozen such names for +01:00. Choose the one that represents your applicable rules for DST and other anomalies. My example code arbitrarily chose Paris time zone.

Confusing Question

I could not understand if your input string represented a date-time at UTC or already in a +01:00 time zone. My example code below has two variations, covering both cases.

Also, you would have found your question already asked and answered many times on StackOverflow if you searched.

Joda-Time

The bundled java.util.Date and Calendar classes are notoriously troublesome. Avoid them. Use either:

Example Code

String input = "20140217 14:45:28";

// Formatters
DateTimeFormatter formatterInput = DateTimeFormat.forPattern( "yyyyMMdd HH:mm:ss" );
DateTimeFormatter formatterOutput = DateTimeFormat.forPattern( "dd.MM.yyyy HH:mm:ss" );

// Use a proper time zone name rather than 3-letter codes.
DateTimeZone timeZoneParis = DateTimeZone.forID( "Europe/Paris" );

// If that input was meant to be in UTC, and then adjusted to +01:00.
DateTime dateTimeAsUtc = formatterInput.withZone( DateTimeZone.UTC ).parseDateTime( input );
DateTime dateTimeAdjustedToParis = dateTimeAsUtc.withZone( timeZoneParis );

// Or, if that input was already in +01:00.
DateTime dateTimeAsParis = formatterInput.withZone( timeZoneParis ).parseDateTime( input );

Dump to console…

System.out.println( "input: " + input );
System.out.println( "dateTimeAsUtc: " + dateTimeAsUtc );
System.out.println( "dateTimeAdjustedToParis: " + dateTimeAdjustedToParis );
System.out.println( "dateTimeAdjustedToParis thru formatter: " + formatterOutput.print( dateTimeAdjustedToParis ) );
System.out.println( "dateTimeAsParis: " + dateTimeAsParis );

When run…

input: 20140217 14:45:28
dateTimeAsUtc: 2014-02-17T14:45:28.000Z
dateTimeAdjustedToParis: 2014-02-17T15:45:28.000+01:00
dateTimeAdjustedToParis thru formatter: 17.02.2014 15:45:28
dateTimeAsParis: 2014-02-17T14:45:28.000+01:00

I use the following code to get the date and time of my country;

String TIME_SERVER = "time-a.nist.gov";   
NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();
Date time = new Date(returnTime);

Maybe it helps you, if it doesn't, just put a comment and i will delete my answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top