DurationtoHoursPart() method in Java, example
Duration toHoursPart() method in Java, examples Original text: https://www . geesforgeks . org/duration-to ourspart-method-in-Java-with-examples/ java.time package The toHoursPart() method of the Duration class is used to divide the day by Hours Gets the hour value for this duration. Syntax: public long toHoursPart() Parameters: This method does not accept any parameters. Return value: This method returns a long value, which is the number of hours in the duration divided by the number of hours in a day. The following example illustrates the duration . to ourspart() method: Example 1: // Java code to illustrate toHoursPart() methodimport java .time.Duration;public class GFG { public static void main(String[] args) { // Duration using parse() method Duration duration = Duration.parse(“P2DT3H4M”); System.out .println(“Duration: ” span> // Get the number of hours // using toHoursPart() method System.out.println(duration.toHoursPart()); }} Output: Duration: PT51H4M3 Example 2: // Java code to illustrate toHoursPart() methodimport java.time.Duration;public class GFG { public static void main(String [] args) { // Duration using ofHours() method Duration duration = Duration. ofHours(10); System.out. println(“Duration: ” o”>+ duration); // Get the number of hours // using toHoursPart() method System.out.println(duration.toHoursPart()); }} Output: Duration: PT10H10 Reference:https://docs . Oracle . com/javase/9/docs/API/Java/time/duration . html # to ourspart–
Character set contains () method in Java, example
Charset contains the () method in Java, examples Original text: https://www . geesforgeks . org/charset-contains-method-in-Java-with-examples/ contains() The method is a built-in method of java.nio.charset that is used to check whether the given character set is in another within the given character set. A character set X contains a character set Y if every character that is representable in Y is also representable in X. Each character set contains itself. However, this method computes an approximation of the inclusion relationship. This means that the function will return true if the given character set is within another character set. However, if it returns false, then the given character set is not necessarily included in this character set. Syntax : public abstract boolean contains(Charset second) Parameter: This function accepts a single mandatory parameter seconds, specifying the character set to be checked. Return value: This function returns a Boolean value. Returns true if it contains the given character set, false otherwise. The following is the implementation of the above functions: Program 1: // Java program to demonstrate// the above functionimport java.nio.charset.Charset;import java.util.Iterator;import java.util.Map;public class GFG { public static void main(String[] args) { // First charset Charset first = Charset .forName(“ISO-2022-CN” ); // Second charset Charset second = Charset.forName(“UTF-8”); System.out .println(first span>.contains(second…
LocalTimewithSecond() method in Java, example
LocalTime withSecond() method in Java, examples Original text: https://www . geesforgeks . org/local time-with second-method-in-Java-with-examples/ The withSecond() method of a LocalTime class is used to obtain a copy of this LocalTime with the seconds changed to as argument The seconds passed to this method. The remaining value of this local time will remain unchanged. This instance is immutable and is not affected by calls to this method. Syntax: public LocalTime withSecond(int second) Parameters:This method accepts A single parameter seconds, representing the minutes and seconds set in the result, from 0 to 59. Return Value: This method returns a Local Time Instance based on which the seconds are requested. Exception:If the second value is invalid, this method throws an exceptionDateTimeException The following program illustrates withSecond() method: Program 1: // Java program to demonstrate// LocalTime.withSecond() methodimport java.time.*;public class GFG { public static void main(String[] args) { // create a LocalTime object LocalTime time = LocalTime.parse(“19:34:50.63 “); // print time System .out.println(“Old LocalTime: ” span> time); // Get a new LocalDateTime with seconds 4 LocalTime newtime = time.withSecond(4); // print result System.out.println(“New LocalDateTime: ” “>+ newtime); } } Output: Old LocalTime: 19:34:50.630New LocalDateTime: 19:34: 04.630 Procedure 2: // Java program to demonstrate// LocalTime.withSecond() methodimport java.time.*;public class GFG { public static void main(String[] args) { // create…
Array asList() method in Java, example
Array asList() method in Java, examples Original text: https://www . geesforgeks . org/arrays-as list-method-in-Java-with-examples/ The asList() method of the java.util.Arrays class is used to return a fixed-size list backed by the specified array. This method, combined with Collection.toArray(), acts as a bridge between array-based and collection-based APIs. The returned list is serializable and implements random access. Tip:This runs in O(1) time. Syntax: public static List asList(T… a) Parameters: This method takes array a and needs to be converted to List. Here… is called varargs which is an array of parameters and works like an object array parameter. Special Note: In the case of primitive data types (int, float, etc.), the type of the array must be Wrapper Class (Integer, Float, etc.), that is You cannot pass int a[], but you can pass Integer a[]. If you pass an int a[], this function will return a List instead of a List, since “autoboxing” does not happen in this case and the int a[] itself is identified as a object, and returns a List of int arrays instead of a List of integers, which will give errors in various Collection functions. Return value: This method returns a list view of the specified…
Durationplus(long, TemporalUnit) method in Java, example
Duration plus (long, TemporalUnit) method in Java, example Original text: https://www.geesforgeks.org/duration-plus long-tempalalunit-method-in-Java- with-examples/ java.time package‘s duration classplus(long, TemporalUnit) Method is used to obtain an immutable copy of this duration and add the specified duration, passed as argument. The duration to be added is determined by converting the quantity in the unit passed as argument to a quantity. Syntax: public Duration plus(long amountToAdd, TemporalUnit unit) Parameters:This method accepts two parameters: Add to the amount, which means the amount to be added. It can be positive or negative, but it cannot be empty. Unit , that is, the time unit to be added. It cannot be empty. Return value: This method returns a Duration that is an immutable copy of the existing duration with the duration’s argument amount added. Exception:This method throws: Arithmetic exception : If there is a numerical overflow . Unsupported imperialtypexception: if the unit is not supported. The following example illustrates the Duration.plus() method: Example 1: // Java code to illustrate plus() methodimport java.time.Duration;import java.time.temporal.*;public class GFG { public static void main(String[] args) { // Duration 1 using parse() method Duration duration1 = Duration.parse(“P2DT3H4M”); // Get the duration added / / using plus() method System.out.println( “>duration1 .plus(5, ChronoUnit.HOURS)); }} Output: PT56H4M Example 2: // Java code to illustrate plus()…
LogRecordsetParameters() method in Java, example
LogRecord setParameters() method in Java, examples Original text: https://www . geesforgeks . org/log record-set parameters-method-in-Java-with-examples/ The SetParameters() method of Java . util . logging . log record is used to set the parameters of the log message. These parameters are the parameters to be inserted into the message logged by this log. Syntax: public void setParameters(Object[] parameters) Parameters: This method accepts Parameters as parameters, which are log message parameters in the form of objects []. Return: This method does not return anything. The following program illustrates the SetParameter() method:Program 1: // Java program to illustrate setParameters() methodimport java.util.logging. Level;import java.util.logging.LogRecord;public class GFG { public static void main(String[] args) { // Create LogRecord object LogRecord logRecord = new LogRecord( Level.parse(“800”), “s”>”Hi Logger”); // set empty object array logRecord.setParameters(new Object[] {}); System.out .println( “Object Array length: ” + logRecord.getParameters().length); }} Output: Object Array length: 0 Program 2: // Java program to illustrate setParameters () methodimport java.lang.reflect.Method ;import java.util.logging.Level;import java.util.logging.LogRecord;public class GFG { public static void main(String[] args) { // Create LogRecord object LogRecord logRecord = new LogRecord( Level.parse(“800”), “Hi Logger”); // get parameter object array // from string class method Method method= String.class.getDeclaredMethods() [4]; System.out.println(“Method : ” “>+ method.getName()); Object[] objArr = method.getParameters(); // set empty object array logRecord.setParameters(objArr); // get array Object[] array = logRecord.getParameters (); for (int i = 0; i < array.length; i…
Decimal format symbol getGroupingSeparator() method in Java, example
Decimal format symbols getGroupingSeparator() method in Java, example Original text: https://www . geeksforgeeks . org/decimal format symbols-getgroup ingseparator-method-in-Java-with -examples/ The getGroupingSeparator() method of java.text. The Extract Format Symbol class in Java is used to obtain the characters used to represent the thousands of separators for a locale for that data format symbol. This method returns the thousands separator character for the locale. Syntax: public char getGroupingSeparator() Parameters: This method does not accept any parameters. Return value: This method returns the thousands separator of the data format symbol. Exceptions:This method does not throw any exceptions. Program: // Java program to demonstrate// the above methodimport java.text.* ;import java.util.* ;public class DecimalFormatSymbolsDemo { public static void main(String[] args) { DecimalFormatSymbols dfs new DecimalFormatSymbols(); System.out.println(“Character used for” + ” thousands separator: ” >.getGroupingSeparator()); }} Output: Character used for thousands separator:, Reference:https://docs . Oracle . com/javase/9/docs/API/Java/text/decimalformatsymbols . html # getgroup ingseparator–
Decimal style toString() method in Java, example
Decimal style toString() method in Java, example Original text: https://www . geesforgeks . org/decimal style-tostring-method-in-Java-with-example/ The toString() method of the Java . time . format . DecimalStyle class in java is used to obtain the string value of this decimal style . This method returns a string representing the string value. Syntax: public String toString() Parameters: This method does not accept any parameters. Return Value: This method returns a String, which is the decimal-style string value. Exceptions:This method does not throw any exceptions. Program: // Java program to demonstrate// the above methodimport java.time.format .*;import java.util.*;public class DecimalStyleDemo { public static void main(String[] args) { DecimalStyle ds1 span> DecimalStyle.STANDARD; DecimalStyle ds2 = DecimalStyle.of( new Locale(“JAPANESE”)); System.out.println( “String value of DS 1: ” /span>.toString()); System.out. println(“String value of DS 2: ” span class=”o”>+ ds2.toString()); }} Output: String value of DS 1 : DecimalStyle[0+-.]String value of DS 2: DecimalStyle[0+-.] Reference: https://docs . Oracle . com/javase/10/docs/API/Java/time /format/decimal style . html # toString()
CompoundName clone() method in Java, example
CompoundName clone() method in Java, examples Original text: https://www . geesforgeks . org/compound name-clone-method-in-Java-with-examples/ The clone() method of the javax.naming.CompoundName class is used to return a copy of this compound name object. If you make any changes to the components of this original composite name, it will not affect the new copy of the composite name object, and vice versa. Clones use the same syntax as this compound name. Syntax: public Object clone() Parameters: This method accepts nothing. Return value:This method returns a non-empty copy of the compound name. The following program illustrates the CompoundName.clone() method:Program 1: // Java program to demonstrate// CompoundName.clone() import java.util.Enumeration;import java.util.Properties;import javax.naming.CompoundName;import javax.naming.InvalidNameException;public class GFG { public static void main(String[] args) throws InvalidNameException { // need properties for CompoundName Properties props = new Properties(); props.put(“jndi.syntax.separator”, “|” ); props.put(“jndi.syntax.direction”, “left_to_right”); // create CompoundName object CompoundName CompoundName class=”k”>new CompoundName( |y”, props); // create clone object CompoundName cloneCompound = (CompoundName)CompoundName .clone(); // print cloned CompoundName System.out.println( “Clone CompoundName: ” + cloneCompound); // print members System.out.println(“Members:”); span> Enumeration<String> enumeration = cloneCompound.getAll(); while (enumeration.hasMoreElements()) System.out.print( span>.nextElement() + ” “); }} Output: Clone CompoundName: x|yMembers: x y Program 2: // Java program to demonstrate/ / CompoundName.clone() methodimport java.util.Enumeration;import java.util.Properties;import javax.naming.CompoundName;import javax.naming.InvalidNameException;public class GFG { public static void main(String[] args) throws InvalidNameException { // need properties for CompoundName Properties props = new Properties(); props.put(“jndi.syntax.separator”, “:”); props.put(” jndi.syntax.direction”, ”left_to_right”) ; // create CompoundName…
The most sophisticated Logger() method in Java, example
The most refined Logger() method in Java, examples Original text: https://www . geeksforgeeks . org/logger-finest-in-Java-method-in-with-examples / A Recorder class uses the Fineest() method to record the most granular messages. This method is used to pass the most granular type of log to all registered output handler objects. Minimum Message:Minimum provides highly detailed tracking information. There are two types of min() methods depending on the number of arguments passed. Minimum (string message): This method is used to record the smallest message. If the logger is allowed to log the finest level of messages, then the given message is forwarded to all registered output handler objects. Syntax: public void finest( String msg) Parameters: This method accepts a single parameter string, which is the string message. Return value:This method does not return anything. The following program illustrates the most sophisticated (string message) method:Program 1: // Java program to demonstrate// Logger.finest(String msg) methodimport java.io.IOException;import java.util.logging.*; public class GFG { public static void main(String[] args) throws SecurityException, IOException { // Create a Logger Logger logger > Logger.getLogger( GFG.class.getName()); // Create a file handler object // and set formatter to simple formatter FileHandler handler = new FileHandler (“logs.txt”); handler.setFormatter(new SimpleFormatter()); // Add file handler as // handler of logs logger.addHandler(handler ); // Set Logger level() logger.setLevel(Level .FINEST); // Call finest method logger.finest(“Set…