ChronoLocalDateTimetoEpochSecond() method in Java, example
ChronoLocalDateTime toEpochSecond() method in Java, examples Original text: https://www . geeksforgeeks . org/chronolocaldatetime-toepochssecond-method-in-Java-with-examples/ Time local date and timeThe to pochssecond() method is used to convert the time local date and time to since 1970-01-01T00 The number of seconds since :00:00Z. This method combines this ChronoLocalDateTime with the offset passed as argument to calculate the epoch seconds value, which is the number of seconds elapsed since 1970-01-01T00:00:00Z. Moments on the timeline after the epoch are positive, earlier ones are negative. Syntax: default long toEpochSecond(ZoneOffset offset) Parameters:This method accepts One parameter Offset, which is the area offset. Return value: This method returns a long, which is the number of seconds since the epoch 1970-01-01T00:00:00Z, which may be negative. The following program illustrates the toEpochSecond() method: Program 1: // Java program to demonstrate// ChronoLocalDateTime.toEpochSecond() methodimport java.time.*;import java.time.chrono.*;public class GFG { public static void main(String[] args) { // create ChronoLocalDateTime object ChronoLocalDateTime time LocalDateTime .parse(“2019-12-31T19:15:30”); // print ChronoLocalDateTime System.out.println(“ChronoLocalDateTime: ” “c1”>// create ZoneId ZoneOffset zone = ZoneOffset.of(“Z”); // print ZoneId System.out.println(“Passed ZoneOffset: ” class=”c1″>// print result System.out.println(“Epoch Second: ” toEpochSecond(zone)); }} Output: ChronoLocalDateTime: 2019-12-31T19:15:30Passed ZoneOffset: ZEpoch Second: 1577819730 Program 2: // Java program to demonstrate// ChronoLocalDateTime.toEpochSecond() methodimport java.time.*;import java.time.chrono.*; public class GFG { public static void main(String[] args) { // create ChronoLocalDateTime object ChronoLocalDateTime time = LocalDateTime.parse ( “2018-10-25T23:12:31.123”); //…
PrintWriter writes (int) method in Java, example
PrintWriter writes (int) method in Java, examples Original text: https://www . geesforgeks . org/print writer-writeint-method-in-Java-with-examples/ The write(int) method of the [PrintWriter] class in Java is used to write specified characters in the stream. The character is specified using the ASCII value of the character passed as an integer value. This integer value is used as a parameter. Syntax: public void write(int ascii) Parameters:This method accepts A mandatory parameter ascii which is the ascii value of the character to be written to the stream. Return value:This method does not return a value. The following method illustrates how the write(int) method works: Program 1: // Java program to demonstrate// PrintWriter write(int) methodimport java.io.*;class GFG { public static void main(String[] args ) { try { = new PrintWriter (System.out); // write the charter ‘0’ to this writer // using write ( ) method // This will put the string in the stream // till it is printed on the console writer.write(48); /span>.flush(); } catch (Exception e) { System.out.println(e); } }} Output: 0 Program 2: / / Java program to demonstrate// PrintWriter write(int) methodimport java.io.*;class GFG { public static void main(String[] args) { try { a PrintWriter instance PrintWriter writer span> new PrintWriter(System .out); =”c1″>// Write the character ‘A’ to this writer // using write() method //…
Pump getAdler() function in Java, example
The pump getAdler() function in Java, examples Original text: https://www . geeksforgeeks . org/dugger-getadler-function-in-Java-with-examples/ The BoostPump class‘s getAdler() function returns the Adler-32 value of the uncompressed data. Adler-32 is a widely used checksum algorithm in the zlib compression library. Function signature: public int getAdler() Syntax: i.getAdler(); Parameters: The function requires no parameters Return type:The function returns an integer value which is the Adler-32 value of the uncompressed data . Exception:The function does not throw any exception Example 1:Usage of getAdler() function // Java program to describe the use// of getAdler functionimport java.util.zip.*;import java.io.UnsupportedEncodingException;class GFG { public static void main(String args[] ) throws UnsupportedEncodingException, DataFormatException { // compress the data // deflater Deflater d = new Deflater(); // get the text String pattern = “GeeksforGeeks”, text = “”; // generate the text for ( int i = 0; i < 4; i++) text += pattern ; // set the input for deflator d.setInput(text.getBytes(“UTF-8”)); // finish d .finish(); // output bytes byte output[] = new byte [1024]; // compress the data int size = d.deflate(output); // end d.end(); // end of compression // use Inflater to get back the original data // Inflater Inflater i = new Inflater( );// set the input for inflator i.setInput(output); // output bytes byte inflater_output[] = new byte span>[1024]; // uncompress the data int org_size = i. inflate(inflater_output); // output of inflater and deflater System.out.println(“Compressed output of…
Timing unit getDuration() method in Java, example
Chronounit getDuration() method in Java, examples Original text: https://www . geesforgeks . org/chronounit-get duration-method-in-Java-with-examples/ Time unit enumeration‘s Get duration() method is used to return the time unit in the International Organization for Standardization calendar system. Estimated duration. Daylight hours vary due to daylight saving time, and months have different lengths. Syntax: public Duration getDuration() Parameters: This method accepts nothing. Return value: This method returns the estimated duration of the unit, which is not empty. The following program illustrates the timing unit. Get duration () method:Program 1: // Java program to demonstrate// ChronoUnit.getDuration() methodimport java.time.Duration; import java.time.temporal.ChronoUnit;public class GFG { public static void main(String[] args ) { // get ChronoUnit >ChronoUnit.valueOf(“NANOS”); // apply getDuration() Duration getDurationAttribute /span>.getDuration(); // print System.out .println( “s”>”Duration Estimated :” + getDurationAttribute ); }} Output: Duration Estimated :PT0.000000001S Program 2: // Java program to demonstrate// ChronoUnit.getDuration() method import java.time.Duration;import java.time.temporal.ChronoUnit;public class GFG { public static void main(String[] args) { // get ChronoUnit ChronoUnit chronoUnit span>.valueOf(” DAYS”); // apply getDuration() Duration getDurationAttribute = chronoUnit.getDuration(); // print System.out .println( “Duration Estimated:” + getDurationAttribute); }} Output: Duration Estimated :PT24H Reference: https://docs.Oracle.com/javase/10/docs/API/Java/time/temporal/chronounit.html # getDuration()
Property getProperty(key, defaultValue) method in Java, example
Property getProperty(key, defaultValue) method in Java, example Original text: https://www.geesforgeks.org/properties-getpropertykey-defaultvalue-method-in-Java-with -examples/ Property class‘s getProperty(key, defaultValue) method is used to obtain the value mapped to this key as a parameter Passed into this Properties object. This method will get the corresponding value for the key, if it exists, and return it. If there is no such mapping, then it returns the default value. Syntax: Public string getProperty(String key, String defaultValue) Parameters:This method accepts two parameters: Key: whose mapping is to be checked in the property object. Default: This is the default mapping for the key Returns:The The method returns the value corresponding to the key, if one exists. If there is no such mapping, then it returns the default value. The following program illustrates the getProperty(key, default value) method: Program 1: // Java program to demonstrate// getProperty(key, defaultValue) method.import java.util.*;public class GFG { // Main method public static void main(String[] args) { // Create a properties and add some values Properties properties = new Properties(); properties.put (“Pen”, 10); properties.put(“Book” , 500); properties.put(“Clothes”, 400); properties.put(“Mobile”, 5000); span> // Print Properties details System.out.println(“Properties: “ + properties.toString()); // Getting the value of Pen System.out .println(“Value of Pen: ” >.getProperty(“Pen”, 200)); / / Getting the value of Phone System.out.println(” Value of Phone: ” + properties . getProperty(“Phone”, 200)); }} Output: Properties: {Book=500 ,…
ArrayListtrimToSize() in Java, example
ArrayList trimToSize() in Java, example Original text: https://www . geesforgeks . org/ArrayList-trimtosize-Java-example/ The trimToSize() method of ArrayList in Java trims the capacity of an ArrayList instance to the current size of the list. This method is used to trim an array list instance to the number of elements it contains. Syntax: trimToSize() Parameters:Does not accept any parameters. Return value: No value is returned. It trims the capacity of an array list instance to the number of elements it contains. Errors and Exceptions: There are no errors or exceptions. The following program illustrates the trimTosize() method: // Java code to demonstrate the working of// trimTosize() method in ArrayList// for ArrayList functionsimport java.util.ArrayList;public class GFG { public static void main(String[] args ) { // creating an Empty Integer ArrayList ArrayList<Integer> arr = new ArrayList<Integer>(9); // using add(), add 5 values arr.add(2); arr.add(4); arr.add(5); arr.add (6); arr.add(11); // trims the size to the number of elements arr.trimToSize(); System.out .println(“The List elements are:”); // prints all the elements for (Integer number : arr) { System.out .println(“Number = ” + number); } }} Output : The List elements are:Number = 2Number = 4Number = 5Number = 6Number = 11
Duration truncation to (temporary time) method in Java, example
Duration truncation to (temporary time) method in Java, examples Original text: https://www . geeksforgeeks . org/duration-shoppettemporalunit-method in-Java-with-examples / Use the Truncate to (Tense) method of the Duration class in the java.time package , gets the value of this duration in the specified units. Syntax: public Duration truncatedTo(TemporalUnit unit) Parameters:This method accepts A parameter Units to which the duration value is to be converted. Return Value: This method returns a Duration that is truncated to the specified unit. Exception:This method throws: Abnormal date and time : If the unit is invalid. Unsupported dtime type exception:yoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyoyo Yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo yo. The following example illustrates the Duration.Truncate to () method: Example 1: // Java code to illustrate truncatedTo() methodimport java.time.Duration;import java.time.temporal.*;public class GFG { public static void main(String[] args) { // Duration using parse() method Duration duration = Duration.parse(“P2DT3H4M”); System.out.println(“Original duration: ” “>); // Truncate the duration to seconds // using truncatedTo() method span> System.out.println( duration .truncatedTo(ChronoUnit .SECONDS)); }} Output: Original duration: PT51H4MPT51H4M Example 2: // Java code to illustrate truncatedTo() methodimport java.time.Duration;import java.time.temporal.*;public class GFG { public static void…
InstantplusNanos() method in Java, example
Instant plusNanos() method in Java, examples Original text: https://www . geesforgeks . org/instant-plusnanos-method-in-Java-with-examples/ The Instant class‘s plusNanos() method adds the nanosecond value passed as argument to the instant and returns the result as an instant object . This returned instant message is immutable. Syntax: public Instant plusNanos(long nanosToAdd) Parameters:This method accepts A parameter nanostoad, nanoseconds need to be added. Returns: This method returns the moment, which is not null. Exception:If the result exceeds the maximum or minimum instant, this method throws the following exception: DateTimeException :. Arithmetic exception: If there is a numerical overflow. The following program illustrates the plusNanos() method : Program 1: // Java program to demonstrate// Instant.plusNanos() methodimport java.time.*;public class GFG { public static void main(String[] args) { // create a Instant object Instant instant = Instant.parse(“2018-10-28T19:34:50.63 Z”); // addition of 840000000 nanoseconds // means .84 seconds to this instant Instant returnedValue = instant.plusNanos(840000000); // print result System.out .println(“Returned Instant : ” + returnedValue); }} Output: Returned Instant: 2018-10-28T19:34:51.470Z Program 2: // Java program to demonstrate// Instant.plusNanos() methodimport java.time.*;public class GFG { public static void main( String[] args) { // create a Instant object Instant instant = Instant.now(); // current Instant System.out.println(“Current instant: ” > instant); // addition of 430000000 nanoseconds // means .43 seconds to this instant Instant returnedValue = instant.plusNanos(430000000); // print result System.out.println(“Returned Instant: ” >); }} Output:…
Rulebase clone() method in Java, example
Rules based collator-clone-method-in-Java-with-example/ The clone() method of the Java . text . rulesbasedcollator class is used to obtain a copy of this Collator object. Syntax: public Object clone() Parameters: This method does not accept any parameters. Return value:This method returns a copy of this Collator object. The following is an example illustrating the clone() method: Example 1: // Java program to demonstrate// clone() method import java.text.*;import java.util.*; import java.io.*;public class GFG { public static void main(String[] argv) { try { // Creating and initalizing new simple rule String simple = “; // Creating and initializing // new RuleBasedCollator Object RuleBasedCollator col = new RuleBasedCollator (simple); =”c1”>// getting copy of this object // using clone() mehtod Object obj = col.clone(); /span> System.out.println(“equivalent object :- ” class=”p”>} catch (ClassCastException e) { System.out.println(“Exception thrown : ” >+ e); } catch (ParseException e ) { “p”>.out.println(“Exception thrown : ” “>e); } }} Output: equivalent object :- java.text .RuleBasedCollator@7033e09a Example 2: // Java program to demonstrate// clone() methodimport java.text.*;import java.util.*;import java.io.*;public class GFG { public static void main(String[] argv) { try { // Creating and initalizing new simple rule String simple = “; // new RuleBasedCollator Object RuleBasedCollator col = new RuleBasedCollator(simple); of this object // using clone() mehtod Object obj = col.clone(); // display result System.out.println(“equivalent object :- ” “o”>+ obj); } catch (ClassCastException e) { span class=”p”>.out.println(“Exception thrown : ” =”n”>e); >catch (ParseException…
Arrays.toString() in Java, example
Arrays.toString() in Java, examples Original text: https://www . geesforgeks . org/arrays-tostring-in-Java-with-examples/ Today we will discuss the simplest way to print an array as a string in Java: the Arrays.toString() method. How to use Arrays.toString() method? Description:Returns a string representation of the contents of the specified array. The string representation consists of a list of array elements enclosed in square brackets (“[]”). Adjacent elements are separated by the characters “,” (comma followed by a space). If empty, returns “null”. In the case of an object array, if the array contains other arrays as elements, they will be converted to strings via the Object.toString() method inherited from the object, which describes their identity rather than their content. Variants: Public static string toString(boolean[] arr) Public static string toString(byte[] arr) Public static string toString(char[] arr) Public static string toString( double[] arr) Public static string toString(float[] arr) Public static string toString(int[] arr) li> Public static String(Long[] arr) Public static String to String(Object[] arr) Public static string toString(short[] arr) Parameters:arr – the array whose string representation is to be returned p> Returns:String representation of arr Usage:The Java code mentioned below is described with an example The usage of toString() method of Arrays class: // Java program…