Java SimpleDateFormat Date and time formats are specified by date and time pattern strings. Within format strings, unquoted letters from ‘A’ to ‘Z’ and from ‘a’ to ‘z’ are treated as format letters representing the components of a date or time string.
Category Archives: Java
Difference between @Component, @Repository & @Service annotations in Spring
@Component – generic stereotype for any Spring-managed component (auto scan component)
@Repository – stereotype for persistence layer (DAO component)
@Service – stereotype for business layer (service component)
@Controller – stereotype for presentation layer (spring-mvc)
Get Current Date and Time in a Specific Timezone (Java)
1 2 3 4 5 6 7 8 9 10 11 12 |
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("America/New_York")); DateFormat df = new SimpleDateFormat("EEE yyyy-MM-dd HH:mm:ss"); df.setTimeZone(cal.getTimeZone()); System.out.println("The date and time in the specific timezone: " + cal.getTimeZone().getID()); System.out.println(df.format(cal.getTime())); System.out.println("\nThe date and time in the system time zone: " + Calendar.getInstance().getTimeZone().getID()); df.setTimeZone(Calendar.getInstance().getTimeZone()); System.out.println(df.format(Calendar.getInstance().getTime())); |
Java HTTP client crashes on HTTPS connections
OS: CentOS 6.5
JAVA: 1.7.0_91
One use case in the project: due to the remote client server system upgrade, HTTPClient runs into such exception like javax.net.ssl.SSLException: java.security.ProviderException: java.security.KeyException:
Install Java 8 (JDK 8u66) on CentOS
Download latest 64 bit Java SE Development Kit 8 release
1 2 3 4 5 6 |
# mkdir /opt/shared # cd /opt/shared # wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u66-b17/jdk-8u66-linux-x64.tar.gz" # tar xzf jdk-8u66-linux-x64.tar.gz |
How HashSet implemented in Java
In Java, HashSet uses HashMap to finish: add, get, iterator etc. The HashMap also can help an element inside HashSet is unique.
How HashMap works in Java
HashMap works on the principle of Hashing. There are several terms: key, hash code, hash function, bucket, entry in HashMap
Queue example in Java
Java provides us Queue interface, where we can keep and handle elements before processing. Except the methods that Collection provides, it also supports some basic operations in order to simulate the classic queue structure. Each of these operations exists in two forms:
1. if a method fails, an exception is thrown. This form includes add(), remove() and element() methods.
2. if a method fails, a special value is returned (null or false). This form contains offer(), poll() and peek() operations.
Continue reading
Lock in Java
In the previous post (Synchronized vs ReentrantLock in Java) , we have discussed the difference between synchronized and ReentrantLock. The keyword synchronized can be widely used in many multi-threaded environments. However, there are some drawbacks when using synchronized. The use of keyword synchronized provides access to the implicit monitor lock associated with every object, but forces all lock acquisition and release to occur in a block-structured way: when multiple locks are acquired they must be released in the opposite order, and all locks must be released in the same lexical scope in which they were acquired. In short, when the keyword synchronized locks a block, and a thread is in the block, other threads have to wait and will be blocked until that thread exits the block. It will slow down the performance. In some situations, it may leads to a deadlock. Continue reading
Asynchronous method invocation in Spring Framework
Since Spring 3.0, annotation support for both task scheduling and asynchronous method execution has been added. The post will give a demo of @Async annotation and its uses. Continue reading