Posts

Showing posts with the label Java

Java – How to Convert Integer to Long

In this section, we will show you h ow to c onvert Integer to Long in Java.In Java, we can use Long.valueOf() to convert an Integer to a Long. Integer i = 7 ; //example Long l = Long . valueOf ( i .longValue()); This avoids the performance hit of converting to a String. The longValue() method in Integer is just a cast of the int value. The Long.valueOf() method gives the vm a chance to use a cached value. Main.java public class Main { public static void main ( String [] args) { Integer i = 7 ; Long l = Long . valueOf ( i .longValue()); System . out .println( l ); } } Console Output: 7 More topics, Moshi - Convert JSON Array String to List and List to JSON Array String Moshi - How to convert Java object to JSON and JSON to Java object Genson - Convert JSON Array String to List and List to JSON Array String Genson - How to convert Java object to JSON and JSON to Java object Convert JSON Array String to List and List to JSON Array String with Jackson Con

Java - How to run a task periodically

In this section, we will show you h ow to run a task periodically in Java. 1. ScheduledTask.java import java.util.TimerTask ; import java.util.Date ; // Create a class extends with TimerTask public class ScheduledTask extends TimerTask { // to display current time Date now ; // Add your task here public void run () { // initialize date now = new Date(); // Display current time System . out .println( "Time is :" + now ); } } 2. Run Scheduler Task A class to run above scheduler task. import java.util.Timer ; //Main class public class SchedulerMain { public static void main ( String args[]) throws InterruptedException { // Instantiate Timer Object Timer time = new Timer(); // Instantiate SheduledTask class ScheduledTask st = new ScheduledTask(); // Create Repetitively task for every 2 secs time .schedule( st , 0 , 2000 ); //for demo only. for ( int i = 0 ; i <= 5 ; i ++) {

How to convert Java object to JSON and JSON to Java object with Gson?

In this section, we will show you h ow to convert Java object to JSON and JSON to Java object with Gson. Note Google Gson is a simple Java-based library to serialize Java objects to JSON and vice versa. It is an open-source library developed by Google. Note JSON (JavaScript Object Notation) is a lightweight format that is used for data interchanging. JSON is built on two structures:  A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.  An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence. 1. Download Gson < dependency > < groupId >com.google.code.gson</ groupId > < artifactId >gson</ artifactId > < version >2.9.0</ version > </ dependency > 2. Java Objects to JSON A User POJO, later uses this for conversion. User.java public class User { private Long id ; private Str

How to convert Char to String in Java

In this section, we will show you h ow to convert Char to String in Java. Main.java public class Main { public static void main ( String [] args) { String str = "knowledgefactory.net" ; //convert a String into char char charK = str .charAt( 0 ); //k char charL = str .charAt( 4 ); //l char charC = str .charAt( 11 ); //c System . out .println( charK ); System . out .println( charL ); System . out .println( charC ); //convert char back to String String temp = Character . toString ( charK ); System . out .println( temp ); } } Console Output: k l c k How to calculate days between two dates in Java 8? How to read a file in Java with BufferedReader Java 8 Streams - Count Frequency Of Words In a List Java 8 Streams - Program To Break A List Into Batches Of Given Size Remove duplicates from an array in Java Java Program To Convert String To HashMap Java 8 Stream - Remove duplicates from a l

How to calculate days between two dates in Java 8?

In this section, we will show you h ow to calculate days between two dates in Java. 1. LocalDate Main.java import java.time.LocalDate ; import java.time.temporal.ChronoUnit ; public class Main { public static void main ( String [] args) { LocalDate from = LocalDate . now (); LocalDate to1 = from .plusDays( 10 ); long result1 = ChronoUnit . DAYS .between( from , to1 ); System . out .println( result1 ); // 10 LocalDate to2 = from .minusDays( 10 ); long result2 = ChronoUnit . DAYS .between( from , to2 ); System . out .println( result2 ); // -10 } } Console Output: 10 -10 2. LocalDateTime Main.java import java.time.LocalDateTime ; import java.time.temporal.ChronoUnit ; public class Main { public static void main ( String [] args) { LocalDateTime from = LocalDateTime . now (); LocalDateTime to1 = from .plusDays( 10 ); long result1 = ChronoUnit . DAYS .between( from , to1 ); System . ou

How to read a file in Java with BufferedReader?

Image
In this section, we will show you how to use java.io.BufferedReader to read content from a file. 1. Files.newBufferedReader (Java 8) In Java 8, there is a new method  Files.newBufferedReader(Paths.get("file"))  to return a  BufferedReader myFile.txt Main.java import java.io.BufferedReader ; import java.io.IOException ; import java.nio.file.Files ; import java.nio.file.Paths ; public class Main { public static void main ( String [] args) { StringBuilder stringBuilder = new StringBuilder(); try ( BufferedReader bufferedReader = Files . newBufferedReader ( Paths . get ( "C: \\ Users \\ 91807 \\ Desktop \\ myFile.txt" ))) { // read line by line String line ; while (( line = bufferedReader .readLine()) != null ) { stringBuilder .append( line ).append( " \n " ); } } catch ( IOException e) { System . err .format( "IOException: %s%n" ,

Java - How to Count the Number of Occurrences of Substring in a String

In this section, we will write a Java program to Count the Number of Occurrences of Substring in a String. Java program to Count the Number of Occurrences of Substring in a String In the below program, we have countOccurrencesOf(String str, String sub) a generic method, here we simply pass input string and substring as arguments and method return number of occurrences of the substring. /** * * Java program to count number of occurrence of substring in given string. * @author knowledgefactory.net * */ public class Main { public static void main ( String [] args) { int count = countOccurrencesOf ( "javaknowledgefactoryjava" , "java" ); System . out .println( "Count number of occurrences of substring 'java' " + " in string 'javaknowledgefactoryjava' : " + count ); int count1 = countOccurrencesOf ( "javaknowledgefactoryjavajava" , "java" ); System .