In this post, we will learn to convert int to String in java with example
Even this is a simple post, But it is difficult to remember all syntax. We can easily convert int to String using valueOf() method
Syntax
String.valueOf(i);
Example
public class ConvertInttoString {
public static void main(String[] args) {
int i = 99;
String stringValue = String.valueOf(i);
System.out.println("Converted String " + stringValue);
}
}
In this tutorial, we will learn to ignore error in ansible playbook.
While Executing play book if any error occurred in a task. Remaining tasks will get ignored
Some Cases we want to ignore the error. Ansible provide option to ignore error on Exception handling
below is the Syntax to ignore error in ansible playbook
Syntax
ignore_errors: yes
Sample playbook
---
- name: Network Getting Started First Playbook
gather_facts: false
hosts: all
tasks:
- name: Make a directory
shell: "mkdir sample"
- name: Make another directory with same name
shell: "mkdir sample"
ignore_errors: yes
- name: Make another directory with another name
shell: "mkdir demo"
ignore_errors: yes
In the above example. we are trying to make a duplicate directory which will occur an error. So we used ignore_errors command to ignore the error and to proceed further
we can see the <span class="token punctuation">..</span>.ignoring
In this post, we will learn to compare two dates in java example
There are multiple ways to compare two dates in java.
In the First example we will learn to compare dates using compareTo() method
While using compareTo() method we are having 3 chances of output. Below are the possible output
0 – When both the dates are equal
1 – When date 1 is greater than date 2
-1 – When date 1 is lesser than date 2
Syntax
date1.compareTo(date2)
Example
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateCompareExample {
public static void main(String[] args) {
try {
String date1 = "22/02/2020";
String date2 = "21/02/2020";
DateFormat format = new SimpleDateFormat("DD/MM/yyyy");
Date dateObj1 = format.parse(date1);
Date dateObj2 = format.parse(date2);
// If Both the date are same comparTo Method will return 0
if (dateObj1.compareTo(dateObj2) == 0) {
System.out.println("Both the dates are Equal !!");
} // If date1 greater than date 2 it will return 1
else if (dateObj1.compareTo(dateObj2) > 0) {
System.out.println("Date 1 is greater than date 2");
} // If date1 lesser than date 2 it will return -1
else {
System.out.println("Date 1 is lesser than date 2");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Date 1 is greater than date 2
Using equals(),after(),before()
In the above example, we learned using compareTo() method. But we can compare dates using predefined functions also.
Below example is another way to compare two dates in java
equals() – return true if both the dates are equal
after() – return true if date 1 greater than date 2
before() – return true if date 1 lesser than date 2
Example
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateCompareExample {
public static void main(String[] args) {
try {
String date1 = "20/02/2020";
String date2 = "21/02/2020";
DateFormat format = new SimpleDateFormat("DD/MM/yyyy");
Date dateObj1 = format.parse(date1);
Date dateObj2 = format.parse(date2);
// If Both the date are same comparTo Method will return 0
if (dateObj1.equals(dateObj2)) {
System.out.println("Both the dates are Equal !!");
} // If date1 greater than date 2 it will return 1
else if (dateObj1.after(dateObj2)) {
System.out.println("Date 1 is after than date 2");
} // If date1 lesser than date 2 it will return -1
else if (dateObj1.before(dateObj2)) {
System.out.println("Date 1 is before than date 2");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
In this tutorial, we will learn to convert String to date in java with example.While we working with java this is one of the most scenario
We can easily convert String to Data using SimpleDateFormat.java. But before that we should know the date patterns which mentioned below
Letter
Component
Presentation
Examples
y
Year
Year
1996; 96
M
Month in year
Month
July; Jul; 07
d
Day in month
Number
10
a
Am/pm marker
Text
PM
H
Hour in day (0-23)
Number
0
k
Hour in day (1-24)
Number
24
K
Hour in am/pm (0-11)
Number
0
h
Hour in am/pm (1-12)
Number
12
m
Minute in hour
Number
30
s
Second in minute
Number
55
S
Millisecond
Number
978
In this example we will convert 20/02/2020 to Date class
Syntax
DateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/YYYY");
simpleDateFormat.parse(dateString);
Example
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatExample {
public static void main(String[] args) {
try {
String dateString = "20/02/2020";
DateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/YYYY");
// Below line will convert from string to date
Date parse = simpleDateFormat.parse(dateString);
// The output will be like Sun Dec 29 00:00:00 IST 2019
System.out.println(" Output :: "+parse);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
In this tutorial, we will learn to ignore the warning message in ansible logs
While working with ansible playbooks, we will get some annoying warning message which makes us uncomfortable.
Below is the useful code snippet to get rid of ansible warning logs
args:
warn: false
Example
---
- name: Network Getting Started First Playbook
gather_facts: false
hosts: all
tasks:
- name: Download java file from github using curl
shell: curl https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/AddDaysToDate.java --output sample.java
args:
warn: false
Output
PLAY [Network Getting Started First Playbook] ********************************************************************************************************
TASK [Download java file from github using curl] *****************************************************************************************************
PLAY RECAP *******************************************************************************************************************************************
127.0.01 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Previous Output
PLAY [Network Getting Started First Playbook] ********************************************************************************************************
TASK [Download java file from github using curl] *****************************************************************************************************
[WARNING]: Consider using the get_url or uri module rather than running 'curl'. If you need to use command because get_url or uri is insufficient
you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
PLAY RECAP *******************************************************************************************************************************************
127.0.01 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
In this post we will learn to add 3 days to the current date in java While we working with dates we will come up with adding or subtracting from the current date
In Java there are lot of method for Date. So it is little bit difficult to remember all the things.
In this example we used Calendar.java and Date.java from java.util package In below example we added 3 days
Syntax
object.add(Calendar.DAY_OF_YEAR, 1)
Example
import java.util.Calendar;
import java.util.Date;
public class AddDaysToDate {
public static void main(String[] args) {
try {
Calendar calender = Calendar.getInstance();
Date beforeAdding = calender.getTime();
System.out.println("Printing the value before adding date ::: " + beforeAdding);
// Adding the 3 days below
calender.add(Calendar.DAY_OF_YEAR, 3);
Date afterAdding = calender.getTime();
System.out.println("Printing the value after adding date ::: " + afterAdding);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output
Printing the value before adding date ::: Thu Feb 13 17:00:21 IST 2020
Printing the value after adding date ::: Sun Feb 16 17:00:21 IST 2020
In this tutorial , We will learn about case when statement in pyspark with example
Syntax
The case when statement in pyspark should start with the keyword <case> . We need to specify the conditions under the keyword <when> .
The output should give under the keyword <then> . Also this will follow up with keyword <else> in case of condition failure.
The keyword <end> for ending up the case statement .
expr("case when {condition} then {Result} else {Result} end")
Libraries
We required the following libraries to be added before executing our code .
import findspark
from pyspark.sql import Row
from pyspark import SparkContext , SparkConf
from pyspark.sql.functions import expr
Sample program
This program helps us to understand the usage of case when statement. Before that we need a dataframe inorder to apply case statements .
Here df is the dataframe, which maintains the name,class,marks,grade details of 3 members.
findspark.init()
sc = SparkContext.getOrCreate()
df=sc.parallelize([Row(name='Gokul',Class=10,marks=480,grade='A'),Row(name='Usha',Class=12,marks=450,grade='A'),Row(name='Rajesh',Class=12,marks=430,grade='B')]).toDF()
print("Printing the df")
df.show()
df1=df.withColumn("Level",expr("case when grade='A' then 1 else 0 end"))
print("Printing the df1")
df1.show()
print("Printing the df2")
df2=df.withColumn("status",expr("case when grade='A' then 'yes' else 'no' end"))
df2.show()
Result
df is the source dataframe which we created earlier .
df1 and df2 are dataframes created by applying the case statements.
In this example, we learn sorting operations in java using streams
In Java 8 it is easy to sort records from an arraylist
Streams API providing powerful features to sort the records from a list without modifying it
This will returns a stream consisting of the sorted elements
Syntax
Stream sorted()
list.stream().sorted({operations});
Ascending Example
import java.util.ArrayList;
import java.util.List;
public class AscendingSortingStreams {
public static void main(String[] args) {
List list = new ArrayList();
list.add("Ball");
list.add("Apple");
list.add("Cat"); // printitng values before sorting
System.out.println("Values before sorting ");
list.stream().forEach(x -> System.out.println(x));
System.out.println("*** Values After sorting ***");
list.stream().sorted().forEach(x -> System.out.println(x));
}
}
Output
Values before sorting
Ball
Apple
Cat
*** Values After sorting ***
Apple
Ball
Cat
Descending Example
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class DescendingSortingStreams {
public static void main(String[] args) {
List list = new ArrayList();
list.add("Ball");
list.add("Apple");
list.add("Cat");
// printitng values before sorting
System.out.println("Values before sorting ");
list.stream().forEach(x -> System.out.println(x));
System.out.println("*** Values After sorting ***");
// Comparator.reverseOrder() will sort the records in descending order
list.stream().sorted(Comparator.reverseOrder()).forEach(x -> System.out.println(x));
}
}
Output
Values before sorting
Ball
Apple
Cat
*** Values After sorting ***
Cat
Ball
Apple