Heim  >  Artikel  >  Java  >  Java-Zeitzone

Java-Zeitzone

PHPz
PHPzOriginal
2024-08-30 15:53:26614Durchsuche

It is a direct subclass of java.lang.Object class present in java.util package that helps to represent the time zone offset of a particular region where the program is running, or the timezone being defined as argument and also helps to save other changes such as daylight savings; it implements Serializable and cloneable interfaces that make it compatible for network transfer as well as for cloning the setting of one-time zone object into another.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Methods of TimeZone Class in Java

This class uses the below 5 parameters to store time zone information:

  • public static final int SHORT = 1;
  • public static final int LONG= 1:
  • private static final int ONE_MINUTE = 60*1000;
  • private static final int ONE_HOUR = 60*ONE_MINUTE;
  • private static final int ONE_DAY = 24*ONE_HOUR;

Below are the methods available in this class:

1. public abstract int getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds)

This method helps to retrieve the offset of the timezone in milliseconds that can be added to GMT to get the local time for a particular date specified in the arguments. Here 0 in month argument means January.

Code:

package Try;
import java.util.TimeZone;
publicclass Office
{
publicstaticvoid main(String[] args)
{
TimeZone obj     = TimeZone.getTimeZone("Africa/Bangui");
System.out.println("Offset value at 2020,5,6 date is  = " + obj.getOffset(1, 2020, 2, 5, 6, 2000));
}
}

Output:

Java-Zeitzone

2. abstract public void setRawOffset(int offsetMillis)

This method is used to set the time zone base to GMT that is used to get the local time by adding it to the UTC.

3. public abstract int getRawOffset()

It is used to get the amount of milliseconds independent of the daylight saving time and added to UTC to retrieve the standard time zone.

Code:

import java.util.TimeZone;
publicclass Office
{
publicstaticvoid main(String[] args)
{
TimeZone obj     = TimeZone.getTimeZone("Pacific/Pago_Pago");
System.out.println("RawOffset in the start is = " + obj.getRawOffset());
obj.setRawOffset(7000000);
System.out.println("RawOffset "+ "set to 7000000");
System.out.println("RawOffset after changes is = "          + obj.getRawOffset());

}
}

Output:

Java-Zeitzone

4. public boolean observesDaylightTime()

This method is used to check if the timezone is currently in daylight saving time or any transition is scheduled for future schedule changes and send true, in that case otherwise false.

Code:

import java.util.*;
publicclass Office
{
publicstaticvoid main(String[] args)
{
TimeZone obj     = TimeZone.getTimeZone("Europe/Rome");
System.out.println(obj.getID()+" is in Daylight or will be in future transitions = " + obj.observesDaylightTime());
}
}

Output:

Java-Zeitzone

5. static String[] getAvailableIDs()

It is used to get the array of all the supported and available IDs under that timezone in string format.

Code:

import java.util.*;
publicclass Office
{
publicstaticvoid main(String[] args)
{
String[] idArray= TimeZone.getAvailableIDs();
System.out.println("Available IDs are \n");
for(String a :idArray){
System.out.print(a);
System.out.print("\n");
}
}
}

Output:

Java-Zeitzone

6. public String getDisplayName(boolean daylight, int style, Locale locale)

This method is used to get the name of the time zone used. In case the daylight argument is true, the daylight saving time zone is returned; otherwise, the standard time zone is returned. Style argument specifies if LONG name is required or SHORT name and in the locale specified. In case the style given is invalid, IllegalArgumentException is thrown, and NullPointerException is thrown in case locale argument is not specified.

7. public static TimeZone getDefault()

This method helps us to retrieve the default value for the Time Zone for a particular region.

8. public StringgetID()

It is used to retrieve the ID of this time zone.

Example program explaining above three methods:

Code:

import java.util.TimeZone;
publicclass Office
{
publicstaticvoid main(String[] args)
{
TimeZone obj1    = TimeZone.getDefault();
System.out.println("Display Name of default Offset is = " + obj1.getDisplayName(false,0));
System.out.println("Id of Default Offset = " + obj1.getID());
}
}

Output:

Java-Zeitzone

9. public abstract boolean inDaylightTime(Date date)

This method is used to query if the particular date given in the argument is in Daylight saving Time in that particular timezone and returns true otherwise false.

Code:

import java.util.*;
publicclass Office
{
publicstaticvoid main(String[] args)
{
TimeZone obj     = TimeZone.getTimeZone("Europe/Rome");
Date dt = new Date();
System.out.println(dt.toString()+" is in Daylight Savings or not = " + obj.inDaylightTime(dt));
}
}

Output:

Java-Zeitzone

10. public abstract boolean useDaylightTime()

This method is used to verify if the given time zone supports Daylight Savings Time schedule changes; if yes, it returns true otherwise false.

Code:

import java.util.*;
publicclass Office
{
publicstaticvoid main(String[] args)
{
TimeZone obj     = TimeZone.getTimeZone("Europe/Rome");
System.out.println(obj.getID()+" supports Daylight Savings or not = \n" + obj.useDaylightTime());
}
}

Output:

Java-Zeitzone

11. public Boolean has SameRules(TimeZone tz1)

This method helps to analyze if the calling time Zone object has the same rules as the time zone object passed in its argument and returns true otherwise false.

Code:

import java.util.*;
publicclass Office
{
publicstaticvoid main(String[] args)
{
TimeZone obj     = TimeZone.getTimeZone("Africa/Bangui");
TimeZone obj1     = TimeZone.getTimeZone("Europe/Rome");
System.out.println(obj.getID() + " has Same Rules as of " +obj.getID() + " = " +obj.hasSameRules(obj));
System.out.println(obj.getID() + " has Same Rules as of " +obj1.getID() + " = " +obj.hasSameRules(obj));
}
}

Output:

Java-Zeitzone

Conclusion

It is a serializable and cloneable class present in java.util package and is a direct sub class of java.lang.object class which helps to represent the time zone offset of a particular region in the 2 static final and int fields long and short and support various operations using the supported methods in them.

Das obige ist der detaillierte Inhalt vonJava-Zeitzone. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:Java-Countdown-TimerNächster Artikel:Java-Countdown-Timer