0

I need load one date, for example: "14-03-2015 14:00:00" in a java variable. But that date is in GMT +4

And I would like to convert it to my local time that is GMT +1

I have seen a lot of conversions to get the local time in one country. For example this code show the local date in Melbourne and in Madrid, but no is that i am searching for.

DateTime dt = new DateTime(org.joda.time.DateTimeZone.forID("Australia/Melbourne"));
DateTimeZone dtZone = DateTimeZone.forID("Europe/Madrid");
DateTime dtus = dt.withZone(dtZone);

Thanks a lot in advance ;)

Rom
  • 563
  • 5
  • 21
  • So, what *are* you searchivg for? What do you expect the output to be, and what is this code giving you instead? – RealSkeptic Mar 13 '15 at 17:24
  • It's not really clear what you *are* looking for then, as that code converts from one time zone to another. Is the problem that you need to *parse* it in that time zone? Please be more specific, otherwise it's going to be hard to help you. – Jon Skeet Mar 13 '15 at 17:24
  • http://www.joda.org/joda-time/apidocs/org/joda/time/DateTimeZone.html#forOffsetHours-int- – JB Nizet Mar 13 '15 at 17:25
  • http://stackoverflow.com/questions/13470830/how-to-change-timezone-for-a-java-util-calendar-date – Apurva Mar 13 '15 at 17:30
  • I need load a Date in GMT +4 in a java variable and convert it to my localtime that is GMT +1 – Rom Mar 13 '15 at 17:44

2 Answers2

0

You do not need to use Joda time, It is really easy to calculate with core Java library. It is not enough to calculate time differences, summer and winter time switches should be taken into consideration.

TimeZone#getRawOffset, TimeZone#getDSTSavings, TimeZone#inDaylightTime methods of Java should be used. Use TimeZone#getRawOffset to get time offset to UTC time. Give special attention to this method, because this do not return dayligth savings. Use TimeZone#getDSTSavings to get dayligth offset time in miliseconds. Finaly use TimeZone#inDaylightTime to check if the time in DST.

Here is the full code;

        TimeZone timeZoneMelbourne = TimeZone.getTimeZone("Australia/Melbourne");
        TimeZone timeZoneMadrid = TimeZone.getTimeZone("Europe/Madrid");

        System.out.println("timeZoneMelbourne.getRawOffset() -> " + timeZoneMelbourne.getRawOffset());
        System.out.println("timeZoneMadrid.getRawOffset() -> " + timeZoneMadrid.getRawOffset());
        System.out.println("timeZoneMelbourne.getDSTSavings() -> " + timeZoneMelbourne.getDSTSavings());
        System.out.println("timeZoneMadrid.getDSTSavings() -> " + timeZoneMadrid.getDSTSavings());

        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, 2015);
        c.set(Calendar.MONTH, 2);
        c.set(Calendar.DAY_OF_MONTH, 13);

        c.set(Calendar.HOUR_OF_DAY, 20);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);


        int timeDiff =  timeZoneMadrid.getRawOffset() - timeZoneMelbourne.getRawOffset();
        int madridDST = 0;
        int melbourneDST = 0;

        if(timeZoneMadrid.inDaylightTime(c.getTime()))
        {
            madridDST = timeZoneMadrid.getDSTSavings();
            System.out.println("timeZoneMadrid#inDaylightTime -> " + madridDST);
        }

        if(timeZoneMelbourne.inDaylightTime(c.getTime()))
        {
            melbourneDST += timeZoneMelbourne.getDSTSavings();
            System.out.println("timeZoneMelbourne#inDaylightTime -> " + melbourneDST);
        }

        timeDiff = timeDiff - melbourneDST + madridDST;

        System.out.println("total timeDiff -> " +timeDiff);


        System.out.println("timeZoneMelbourne -> " +c.getTime());

        c.add(Calendar.MILLISECOND,  timeDiff);

        System.out.println("timeZoneMadrid -> " +c.getTime());

The output of the code;

timeZoneMelbourne.getRawOffset() -> 36000000

timeZoneMadrid.getRawOffset() -> 3600000

timeZoneMelbourne.getDSTSavings() -> 3600000

timeZoneMadrid.getDSTSavings() -> 3600000

timeZoneMelbourne#inDaylightTime -> 3600000

timeDiff -> -36000000

timeZoneMelbourne -> Fri Mar 13 20:00:00 EET 2015

timeZoneMadrid -> Fri Mar 13 10:00:00 EET 2015

Check the correctness on this. There is 10 hours difference on 13rd of March.

Lets test the code for 13rd of June, which is summer time in Madrid. Furthermore, there is a switch between summer and winter time.

Here is the output of the code;

timeZoneMelbourne.getRawOffset() -> 36000000

timeZoneMadrid.getRawOffset() -> 3600000

timeZoneMelbourne.getDSTSavings() -> 3600000

timeZoneMadrid.getDSTSavings() -> 3600000

timeZoneMadrid#inDaylightTime -> 3600000

total timeDiff -> -28800000

timeZoneMelbourne -> Sat Jun 13 20:00:00 EEST 2015

timeZoneMadrid -> Sat Jun 13 12:00:00 EEST 2015

You can check the correctness of calculation on this

Furthermore, i totally agree @dimo414. It is best practice to use Joda time for date and time operations.

Here is the Joda code;

    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, 2015);
    c.set(Calendar.MONTH, 5);
    c.set(Calendar.DAY_OF_MONTH, 13);

    c.set(Calendar.HOUR_OF_DAY, 20);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);

    LocalDateTime dateTime = new LocalDateTime(c.getTime()); 
    DateTime srcDateTime =  dateTime.toDateTime(DateTimeZone.forID("Australia/Melbourne"));
    DateTime dstDateTime = srcDateTime.withZone(DateTimeZone.forID("Europe/Madrid"));
    Date madridTime =dstDateTime.toLocalDateTime().toDateTime().toDate();


    System.out.println("Melbourne Time -> "+srcDateTime.toDateTime());
    System.out.println("Madrid Time -> "+madridTime);

See also;

TimeZone#getRawOffset

TimeZone#getDSTSavings

TimeZone#inDaylightTime

erencan
  • 3,725
  • 5
  • 32
  • 50
  • 1
    Granted it's do-able without Joda, but the core Java date utilities are painfully broken (and are replaced in Java 8). Doing as much date-processing with the Joda library is a pretty generally accepted best-practice at this point. – dimo414 Mar 13 '15 at 18:28
0

A java.util.Date object (not JodaTime) is the same regardless of what the time zone is. Behind the scenes it's always GMT. You only need to worry about timezone when you're parsing (creating the Date object) or printing it.

Use a SimpleDateFormat with timezone set to +4 to parse and create your Date object. Use a different SimpleDateFormat with timezone set to +1 to format that same Date object for printing.

You can either call setTimeZone on SimpleDateFormat or construct it with a format string that includes the time zone. http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#setTimeZone(java.util.TimeZone)

Ted Bigham
  • 4,237
  • 1
  • 26
  • 31