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.