Posts

From LocalDate to LocalDateTime

How to convert LocalDate to LocalDateTime?

First we need to understand what we have and what we want to get.

From LocalDate we have: Instant + System default time zone.
What we want to have is: Instant + System default time zone + Local Time

LocalDateTime.of() methods gives us so many options to create LocalDateTime. On of the option is as given below

LocalDate localDate = LocalDate.now()

LocalDateTime ldt= LocalDateTime.of(localDate, LocalTime.MIN);

Here LocalTime.Min will give us the Day's start time i.e. 00:00:00
To get LocalDateTime with current time we can use following:

LocalDateTime localDateTimeNow = LocalDateTime.of(localDate, LocalTime.now());

Example:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35importjava.time.Instant;importjava.time.LocalDate;importjava.time.LocalDateTime;importjava.time.LocalTime;importjava.time.ZoneId;importjava.util.Date;publicclassLocalDateToLocalDateTime{publicstaticvoidmain(String[] args){/…

Using Lambda Expressions to sort a collection in Java 8

Below is an example to show how to use Lambda expression to sort a collection.
Prior to Java 8, it was necessary to implement the java.util.Comparator interface with an anonymous (or named) class when sorting a collection: Java SE 1.2Collections.sort( personList,newComparator<Person>(){publicint compare(Person p1,Person p2){return p1.getFirstName().compareTo(p2.getFirstName());}}); Starting with Java 8, the anonymous class can be replaced with a lambda expression. Note that the types for the parameters p1 and p2 can be left out, as the compiler will infer them automatically: Collections.sort( personList,

Scheduling a Job using spring

Requirement:

We have to send email on a regular interval by checking a condition automatically.

Solution:
We can use Queartz Job scheduler which is an open source job scheduling library and can be attached with any java application.

Alternatively we can use Spring's Task scheduler using TaskScheduler and TaskExecutor.

Pros:

This approach is good for the situation where you are already using Spring in code. Its quick and easy to implement.You do not have many jobs to run.

To implement the spring way we should use @EnableScheduling annotation in our AppConfig.java
The above annotation enables Spring's scheduled task execution capability.

Below is an example which gets executed every hours and prints the current time.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27package blogspot.thinkwithjava;importjava.text.SimpleDateFormat;importjava.util.Date;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.scheduling.annotation.S…

Remove duplicate character from a given string

This is one of the tricky question asked in the interviews. Not only to check logical ability of candidates but to check their understanding about algorithms and complexities.

So here is the Problem statement:  How to remove duplicate characters from String in Java? For example, if given String is "aaaaaa" then output should be "a", because rest of  the "a" are duplicates. Similarly, if the input is "abcd" then output should also be "abcd"  because there is no duplicate character in this String. Algorithm:
 This algorithm goes through each character of String to check if its a duplicate of already found character. It skip the duplicate character by inserting 0, which is later used to filter those characters and update the non-duplicate character.  Time Complexity of this solution is O(n^2), excluded to UniqueString() method, which creates String from character array. 
This method will work even if String contains more than one duplica…

String pool differences in java 6 and java 7

While reading a blog here, I found a quite interesting improvements in String Pool

String pool is relocated to Java heap space from PermGen space.The default size of String pool is increased to 600013 entries from 1009 in Java 6.The -XX:StringTableSize JVM option is provided to specify the size of String pool. 

What is Phishing

About Phishing Phising is an online attempt to trick a user by pretending to be an official login page or an official email from an organization that you would have an account with, such as a bank or an email provider, in order to obtain a user’s login and account information. In the case of a phishing login page, the login page may look identical to the login page you would normally go to, but the website does not belong to the organization you have an account with (the URL web address of the website should reflect this). In the case of a phishing email, the email may look like an email you would get from the organization you have an account with and get emails from, but the link in the email that it directs you to takes you to the above phishing login page, rather than a legitimate login page for that organization.

To prevent your account information from being obtained in a phishing scheme, only log in to legitimate pages of the websites you have an account with. For example, &quo…

How to convert date into datetime in java

Java 8 has adopted Jodatime in Jave SE8. Joda time is now introduced in java.time package.

Joda time was de facto standard date and time library prior to Java SE8.

 Following are the ways to convert the java util dates with java.time.* and vice-versa.

The idle way (for all these conversions) is to convert to Instant. This can be converted to LocalDateTime by telling the system which timezone to use. This needs to be the system default locale, otherwise the time will change.

Convert java.util.Date to java.time.LocalDateTime


Date ts = new Date();
Instant instant = Instant.ofEpochMilli(ts.getTime());

LocalDateTime res = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());


Convert java.util.Date to java.time.LocalDate

Date date = new Date();
Instant instant = Instant.ofEpochMilli(date.getTime());
LocalDate res = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate();

Convert java.util.Date to java.time.LocalTime

Date time = new Date();
Instant instant = Instant.ofEpochM…