Home » Java programming language

Java TimeZone hasSameRules() Method with Example

TimeZone Class hasSameRules() method: Here, we are going to learn about the hasSameRules() method of TimeZone Class with its syntax and example.
Submitted by Preeti Jain, on March 13, 2020

TimeZone Class hasSameRules() method

  • hasSameRules() method is available in java.util package.
  • hasSameRules() method is used to check whether this time zone has the same rule and offset as the given time zone (tz) or not.
  • hasSameRules() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • hasSameRules() method does not throw an exception at the time of checking rule and offset.

Syntax:

    public boolean hasSameRules(TimeZone tz);

Parameter(s):

  • TimeZone tz – represents the other time zone object to compare with this time zone.

Return value:

The return type of the method is boolean, it returns true when this time zone and the given time zone (tz) has same rule and offset otherwise it returns false.

Example:

// Java program to demonstrate the example 
// of boolean hasSameRules(TimeZone tz)
// method of TimeZone 

import java.util.*;

public class HasSameRulesOfTimeZone {
    public static void main(String args[]) {
        // Instantiates two TimeZone object
        TimeZone tz1 = TimeZone.getTimeZone("Africa/Asmera");
        TimeZone tz2 = TimeZone.getDefault();

        // Display tz1 , tz1
        System.out.println("tz1: " + tz1);
        System.out.println("tz2: " + tz2);

        // By using hasSameRules() method is to
        // check whether two timezone has same 
        // rules and offset or not
        boolean status = tz1.hasSameRules(tz2);

        System.out.print("tz1.hasSameRules(tz2): ");
        System.out.println(status);
    }
}

Output

tz1: sun.util.calendar.ZoneInfo[id="Africa/Asmera",offset=10800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]
tz2: sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
tz1.hasSameRules(tz2): false





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.