Wednesday, 2 January 2019

How to use getOffset method of java.util.TimeZone


Click here to watch on Youtube: 
https://www.youtube.com/watch?v=NWPI6Ruzdb0&list=UUhwKlOVR041tngjerWxVccw

TimeZoneDemo1.java

import java.util.TimeZone;

public class TimeZoneDemo1
{
    public static void main(String[] args)
    {
        TimeZone timezone = TimeZone.getTimeZone("Europe/Paris");

        /*
         * Parameters:
         *
         * era - the era of the given date.
         * year - the year in the given date.
         * month - the month in the given date. Month is 0-based. e.g., 0 for January.
         * day - the day-in-month of the given date.
         * dayOfWeek - the day-of-week of the given date.
         * milliseconds - the milliseconds in day in standard local time.
         *
         * Returns:
         *
         * the offset in milliseconds to add to GMT to get local time.
         */

        int offset = timezone.getOffset(1, 2017, 3, 4, 5, 300);
        System.out.println("offset = " + offset);
    }
}

Output

offset = 7200000

TimeZoneDemo2.java

import java.util.Calendar;
import java.util.TimeZone;

public class TimeZoneDemo2
{
    public static void main(String[] args)
    {
        TimeZone timezone = TimeZone.getTimeZone("Europe/Paris");

        /*
         * Parameters:
         *
         * date - the date represented in milliseconds since January
         * 1, 1970 00:00:00 GMT
         *
         * Returns:
         *
         * the amount of time in milliseconds to add to UTC to get
         * local time.
         */

        int offset = timezone.getOffset(Calendar.ZONE_OFFSET);
        System.out.println("offset = " + offset);
    }
}

Output

offset = 3600000

Click the below link to download the code:
https://sites.google.com/site/javaspringram2019/java_spring_2019/TimeZoneDemo_getOffset.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java_Spring_2019/tree/master/Java_2019/TimeZoneDemo_getOffset

Bitbucket Link:
https://bitbucket.org/ramram43210/java_spring_2019/src/80d90bcf5505788c0f100e330fb6b354d3132ddb/Java_2019/TimeZoneDemo_getOffset/?at=master

See also:
  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Spring Tutorial
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Cooking Tutorial
  • No comments:

    Post a Comment