Java 8 new Date and Time API -2

In this previous post we already check about Date api now in this post will take a look on Time api example.

Time API Example: 

With the help of LocalTime we will get the hour, minute, second, nanosecond

package com.blogspot.ekumeedhelp;

import java.time.*;

public class JavaDateAPIExample2 {

	public static void main(String[] args) {

		LocalTime time = LocalTime.now();
		int hour = time.getHour();
		int minute = time.getMinute();
		int second = time.getSecond();
		int nanoSecond = time.getNano();
		System.out.printf("\n%d:%d:%d:%d", hour, minute, second, nanoSecond);
	}
}

Now let's assume if you want both Date and Time for this we have LocalDateTime class.

DateTime API example :

package com.blogspot.ekumeedhelp;

import java.time.*;

public class JavaDateAPIExample2 {

	public static void main(String[] args) {

	LocalDateTime currentDateTime = LocalDateTime.now();

 	System.out.printf(currentDateTime);
 
        LocalDateTime customeDate = LocalDateTime.of(2020,Month.APRIL,22,12,45); 
 
        System.out.printf(customeDate); 

	}
}


For more details please check out this video

Java 8 new Date and Time API -1

In  this post we will be learning new Date and Time API that is being introduced in Java 8. Prior to Java 8 we already have couple of class is available to handle Date and Time like Data, Calendar etc. but these are not  convenience to use and also have some performance issue.

 Now in Java 8 new Joda time api has been added to overcome all the issue related with Date and Time.

Which is available under Java.time package also know as Joda Time API.

Example - 1

To print local Date and Time.

import Java.time.*;
 public class Java8DateTime {
 public static void main(String[] args) {
 LocalDate date = LocalDate.now();
 System.out.println(date);
 LocalTime time=LocalTime.now();
 System.out.println(time);
 }
} 


Example - 2

To get the day month and year from local date.

import Java.time.*;
class DateAPI{
 public static void main(String[] args) {
 LocalDate date = LocalDate.now();

 System.out.println(date);

 int day   = date.getDayOfMonth();
 int month = date.getMonthValue();
 int year  = date.getYear();

 System.out.println(day+"..."+month+"..."+year);
 System.out.printf("\n%d-%d-%d",day,month,year);
 }
} 

Check out the video example 

Java program to check a given string is Palindrome

Java program to check a given string is Palindrome
In this post we will learn one of the interview programming question to check given string is a palindrome or not.

Lets assume input string is "MADAM" and we will write a program to check whether "MADAM" is a palindrome or not.
In order to check a given string is palindrome we first need to reverse the given string and compare both original and reverse string if both are same then we can say given string is a palindrome. 

package com.ekumeedhelp;

public class Palindrome {

public static void main(String[] args) {

if (isPalindrome("MADAM")) {
System.out.println("Given String is a palindrome");
} else {
System.out.println("Given String isn't a palindrome");
}
}

public static boolean isPalindrome(String original) {

String reversed = "";
int length = original.length();

for (int i = 0; i < length; i++)
reversed = reversed + original.charAt(length - i - 1);

if (original.equals(reversed))
return true;

return false;
}
}


Another way to solve the same problem. I would say this is most elegant way

package com.ekumeedhelp;

public class Palindrome {

public static void main(String[] args) {

if (isPalindrome("MADAM")) {
System.out.println("Given String is a palindrome");
} else {
System.out.println("Given String isn't a palindrome");
}
}

public static boolean isPalindrome(String s) {

int i = 0;
int j = s.length() - 1;
int k = (i + j) / 2;

for (int index = 1; index <= k; index++) {
if (s.charAt(i) == s.charAt(j)) {
i++;
j--;
} else
return false;
}
return true;
}
}

Test

Hello Java program

Hello Java program
In this post we will learn about Hello Java program and try to understand with example.

1
2
3
 public class Hello {

}

Like other programming language Java programs also have keywords. Each keywords has specific meaning and sometime used in some specific orders. keywords are case sensitive like public and Public and even PUBLIC all are totally different.

With the help of keywords and certain rule we can write statements which collectively form java program.

In the above program you can see public and class are two keywords in green and Hello is the name of the class. lets try to understand the meaning of both keywords.

public :- In java programming public is the access modifier is allow us to define the scope or you can also say how other part of the program access this code. For example  public bus that means it is available for everyone i.e scope is for everyone. So here our class is public means available for every other class.

class :- To define a class we need the class keywords. In above example Hello is the class name and defined using class keyword and whatever you can write withing both left and right curly braces are called as class body.  


 

Java Tutorial for beginners

Java Tutorial for beginners
Hi Guys, Welcome to the Java tutorials for beginners.In this tutorials series you will learn core Java concept in depth. To join free Java class join my telegram channel and also subscribe my YouTube channel using below link. 


 What is Java?

  • Java is a programming language was developed by Sun Microsystems in 1995.
  • James Gosling is known as the father of Java.
  • Java is high level programming language.
  • Java is object oriented programming language.
  • Object-oriented programming (OOPs) is a methodology or rules
  • OOPs concepts are: [Class, Object, encapsulation, Abstraction, polymorphism, Inheritance]

Java JDK and eclipse installation and setup

In this post we you will learn how to install and setup Java 11 & 14 and eclipse software in 64 bit windows 10 machine.

To start programming in java you must have Java install in your machine.
Download JDK 14 or JDK 11 as per your your machine like windows or linux and check out the video and follow the step.


How to setup eclipse in windows 10 machine.

 


Stream in Java 8

Stream is an interface present in java.util.stream which is newly added package in Java 8. With the help of stream() method of Collection interface you can create stream object that has been added into collection in java 8 version. Java Stream is lazy loading and evaluates code when needed only.Check out this video to learn about java 8 stream


Package - java.util.function


Well you know predicate, function and consumer can only accept one input argument but sometimes we need a two-argument type functional interface in that situation we can use these predefined functional interfaces.


  1. BiPredicate
  2. BiFunction
  3. BiConsumer
BiPredicate: It is the same as a predicate interface but the only difference is it takes two input arguments to perform a conditional check.

1
2
3
 interface BiPredicate<T,U> {
public boolean test(T t, U u);
}


BiFunction: It is the same as a function predefine functional interface but the only difference is it takes two input arguments to perform some operation.

1
2
3
 interface BiFunction<T,U,R> {
public R apply(T t, U u);
}


BiConsumer: BiConsumer is exactly the same as Consumer except that it will take 2 input arguments

1
2
3
 interface BiConsumer<T,U> {
public void accept(T t, U u);
}





Java 8 : Supplier

What is a Supplier?

Supplier is a predefined functional interface like Consumerpresent in java.util.function package. It has one abstract method called get();

Supplier can be used to supply some value based on some operation, Supplier won't take any input and it will always supply objects.

1
2
3
interface Supplier<R> {
public R get();
}


Supplier example 1:


 1
2
3
4
5
6
7
8
9
10
11
12
package com.blogspot.ekumeedkiasha;

import java.util.function.Supplier;

public class SupplierExample1{

public static void main(String[] args) {
Supplier<Double> sup= () -> Math.random();
System.out.println(sup.get());
}

}



Property Predicate Function Consumer Supplier
What To take some Input and perform some conditional checks To take some Input and perform required Operation and return the result  To consume some Input and perform required Operation. It won’t return anything. To supply some Value base on our Requirement
Abstract method name  public boolean test (T t); public R apply (T t); public void accept (T t); public R get();
Default Methods and(), or(), negate() andThen(), compose() andThen() N/A
Static Method isEqual() identify() N/A N/A

Java 8 : Consumer

What is a Consumer?

Consumer is a predefined functional interface like Predicatepresent in java.util.function package. It has one abstract method called accept();

 Consumer can be used to consume object and perform a certain operation it takes one argument but won't return anything.


1
2
3
interface Consumer<T,R> {
public void accept(T t);
}


Consumer example 1: Take a single argument and print the value.

 1
2
3
4
5
6
7
8
9
10
11
12
package com.blogspot.ekumeedkiasha;

import java.util.function.Consumer;

public class ConsumerExample1 {

public static void main(String[] args) {
Consumer<String> con = s -> System.out.println(s);
con.accept("ekumeedhelp");
}

}


Consumer Chaining example:
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.blogspot.ekumeedkiasha;

import java.util.function.Consumer;

public class ConsumerExample1 {

public static void main(String[] args) {

Consumer<String> con = s -> System.out.println(s.toUpperCase());

Consumer<String> con1 = s -> System.out.println("(" + s + ")");

con.andThen(con1).accept("ekumeed");

}

}