I had posted a blog to show how to convert, but now I bring another new way more easier.

Here is my GitHub project link.

package com.test.timezone;

import java.util.TimeZone;

public final class Utility {
	public static final TimeZone utcTZ = TimeZone.getTimeZone("UTC");

	public static long toLocalTime(long time, TimeZone to) {
		return convertTime(time, utcTZ, to);
	}

	public static long toUTC(long time, TimeZone from) {
		return convertTime(time, from, utcTZ);
	}

	public static long convertTime(long time, TimeZone from, TimeZone to) {
		return time + getTimeZoneOffset(time, from, to);
	}

	private static long getTimeZoneOffset(long time, TimeZone from, TimeZone to) {
		int fromOffset = from.getOffset(time);
		int toOffset = to.getOffset(time);
		int diff = 0;

		if (fromOffset >= 0){
			if (toOffset > 0){
				toOffset = -1*toOffset;
			} else {
				toOffset = Math.abs(toOffset);
			}
			diff = (fromOffset+toOffset)*-1;
		} else {
			if (toOffset <= 0){
				toOffset = -1*Math.abs(toOffset);
			}
			diff = (Math.abs(fromOffset)+toOffset);
		}
		return diff;
	}
}

package com.test.timezone;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class TestTimezone {

	public static void main(String[] args) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ss zzzz");
		Calendar date1 = new GregorianCalendar(2014,0,15,10,0,0);
		System.out.println(sdf.format(date1.getTime())+"\n");
		long utcTimeStamp = Utility.toUTC(date1.getTimeInMillis(), date1.getTimeZone());
		Calendar utcCal = Calendar.getInstance();
		utcCal.setTimeInMillis(utcTimeStamp);
		System.out.println("toUTC: "+sdf.format(utcCal.getTime())+"\n");

		System.out.println("---------------------------------------");
		Calendar date2 = new GregorianCalendar(2014,2,15,10,0,0);
		System.out.println(sdf.format(date2.getTime())+"\n");
		utcTimeStamp = Utility.toUTC(date2.getTimeInMillis(), date2.getTimeZone());
		utcCal.setTimeInMillis(utcTimeStamp);
		System.out.println("toUTC: "+sdf.format(utcCal.getTime())+"\n");

		System.out.println("---------------------------------------");
		Calendar date3 = new GregorianCalendar(2014,11,25,9,0,0);
		System.out.println(sdf.format(date3.getTime())+"\n");
		long uTime = Utility.toUTC(date3.getTimeInMillis(), date3.getTimeZone());
		System.out.println("utcTimeStamp: "+uTime+"\n");
		long lTime = Utility.toLocalTime(uTime, TimeZone.getTimeZone("EST"));
		Calendar locCal = Calendar.getInstance();
		locCal.setTimeInMillis(lTime);
		System.out.println("toLocal: "+sdf.format(locCal.getTime())+"\n");

		System.out.println("---------------------------------------");
		Calendar date4 = new GregorianCalendar(2014,6,4,9,0,0);
		System.out.println(sdf.format(date4.getTime())+"\n");
		uTime = Utility.toUTC(date4.getTimeInMillis(), date4.getTimeZone());
		System.out.println("utcTimeStamp: "+uTime+"\n");
		lTime = Utility.toLocalTime(uTime, TimeZone.getTimeZone("EST"));
		locCal = Calendar.getInstance();
		locCal.setTimeInMillis(lTime);
		System.out.println("toLocal: "+sdf.format(locCal.getTime())+"\n");
	}
}

We don’t re-implement wheel, so Java collection framework offers very easy way to iterate the elements. But iterator pattern is useful, you need to handle it. Everyday you use it but have you though how it works? read the example.

Iterator Design Pattern in Java  (this example forgot to implement Iterable interface. The Iterable interface has only one method:  iterator)

Java’s Iterators and Iterables

Also the beginner may get this exception,  ConcurrentModificationException because if you don’t understand iterator.

How to Avoid ConcurrentModificationException when using an Iterator

 

With social network growth tech company try to collect big data to find what people want and people’s behavior. Then they can sell you goods exactly you need or not. Technology is from human! This year tech industry are more interested  in cars and things. The Internet Of Things (IOT) is new hot topic now. Continue to read and I will bring you to the future.

1. Wiki: Internet of Things

2. The Internet of Things outlook for 2014: Everything connected and communicating

Now do you know what’s Internet Of Things? maybe…but watch those videos, and would that happens?

1. Life Simplified with Connected Devices

2. Machines Made to Know You, by Touch, Voice, Even by Heart

At the end, this is for fun not real IOT. :)

Gamification – The Future is Fun – Your whole life can be a game!  link

Recently I did code review and found someone to implement clone() method. But he just instantiated new object and copied field one-by-one. It’s not good way ever. So I search to find the correct way and refactor the code. Share at here.

1. Which is better option: Cloning or Copy constructors? 

2. Cloneable interface is broken in java

3. If cloneable object has hierarchy, then clone() method for working the hierarchy of classes, link.

Oracle announced Java SE 8 will be released at March 2014. I always watch the lambda because I am very interested in Big data. Usually I use Python and very like the Python’s map, reduce, filter and lambda functional programming. Now I start to learn Java 8 Lambda and will become next big thing for Java.

1. First read the Java official tutorial:
Method References to understand the Java method as function of object type and how to refer to it.
Lambda Expressions to understand the anonymous class and functional interface. Then how Java 8 can be used instead anonymous class.

2. Some useful links.

3. I feel Python is more easier to learn. If you are interested it, see it
Python: Lambda Functions

4. Look at how groovy doing, From Groovy to Java 8

This post I show you how to grab Microsoft server’s SSL certificate and import into JSE keystore. I have customer only used Microsoft IIS and bought CA certificate from Comodo. They don’t know Java Security and they don’t have CSR in hand, so they have to export it as PFX (Personal Exchange file). When I got *.PFX, then I do

STEP 1. Export your private key and SSL Certificate from Microsoft IIS

but JSE keystore can’t be imported from outside, then
STEP 2. Import private key and certificate into Java Key Store

Reference:
1. Dealing with java keystores

2. OpenSsl/Keytool Cheat Sheet

3. A Step-by-Step Guide to Advanced Certificate Management

4. How to back up a server certificate in Internet Information Services 5.0

Part-1 setup on JBoss server with CAS war

1. Download CAS (Central Authentication Service) from here.
You may like to read their document.

2. Unzip and place CAR web application on C:\jboss-4.2.2.GA\server\default\deploy\cas.war. This is example path.

3. I assume you use JBoss login-config.xml to setup for your LoginContext, so you have the name of application-policy like I configure it as “MyLoginRealm”.

4. Now you can go to ${jboss_server}\cas.war\WEB-INF\deployerConfigContext.xml, and modify

<property name=”authenticationHandlers”>

<bean>
<property name=”realm”><value>MyLoginRealm</value></property>
</bean>

5. Create your host server’s keystore and crt.

Creating the keystore and private key:
a. keytool -genkey -alias jbosskey -keypass changeit -keyalg RSA -validity 3650 -keystore MyServer.keystore

b. keytool -list -keystore MyServer.keystore

Generating and storing the certificate:
c. keytool -export -alias jbosskey -keypass changeit -file MyServer.crt -validity 3650 -keystore MyServer.keystore

d. keytool -import -alias jbosscert -keypass changeit -file MyServer.crt -keystore MyServer.keystore

e. keytool -list -keystore MyServer.keystore

6. Copy MyServer.keystore and MyServer.crt into ${jboss_server}\conf

7. Add

SET JAVA_OPTS=%JAVA_OPTS%  -Djavax.net.ssl.trustStore=${jboss_server}\conf\MyServer.keystore into your JVM option. (usually it is at run.bat)

8. Change server.xml at ${jboss_server}\deploy\jboss-web.deployer. Uncomment the 8443 security port like this

<Connector port=”8443″ protocol=”HTTP/1.1″ SSLEnabled=”true”
maxThreads=”150″ scheme=”https” secure=”true”
clientAuth=”false” sslProtocol=”TLS”
keystoreFile=”conf/MyServer.keystore”
keystorePass=”changeit”/>

Part-2 setup on your web apllicarion with CAS client
Now go to ${your_web_app}\WEB-INF\web.xml, and we need to add this into web.xml

<!-- CAS: Java Client 3.1.3-->
<filter>
<filter-name>CAS Single Sign Out Filter</filter-name>
<filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
</filter>

<filter>
<filter-name>CAS Authentication Filter</filter-name>
<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
<init-param>
<param-name>casServerLoginUrl</param-name>
<param-value>https://hostname:8443/cas/login</param-value>
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>https://hostname:8443</param-value>
</init-param>
<init-param>
<param-name>renew</param-name>
<param-value>false</param-value>
</init-param>
</filter>

<filter>
<filter-name>CAS Validation Filter</filter-name>
<filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
<init-param>
<param-name>casServerUrlPrefix</param-name>
<param-value>https://hostname:8443/cas/</param-value>
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>https://hostname:8443</param-value>
</init-param>
<init-param>
<param-name>redirectAfterValidation</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<filter>
<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
<filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
</filter>

<filter>
<filter-name>CAS Assertion Thread Local Filter</filter-name>
<filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>CAS Single Sign Out Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>CAS Authentication Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>CAS Validation Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>CAS Assertion Thread Local Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
</listener>

Test drive
If you reques this, http://bestsite:8080, it will redirect to CAS login page. You will see like this,
https://bestsite:8443/cas/login?service=https%3A%2F%2Fbestsite%3A8080%2F. That means you are good now.

Recently I have project about Single Sign On. I want to put all of my research here because I got them from internet and love to share with you.

1. SSL Converter

2. The Most Common Java Keytool Keystore Commands

3. The Most Common OpenSSL Commands

4. Installing an SSL Certificate in Windows Server 2008 (IIS 7.0)

5. How to use SSL Certificates with Exchange 2007

6. How to Create A Self Signed Certificate

7. Portecle is a user friendly GUI application for creating, managing and examining keystores, keys, certificates, certificate requests, certificate revocation lists and more

8. OpenSSL how-to

I like web front-end using Java script, AJAX more than Flash/Flex. Although RIA technology is improved , I still love to direct code in HTML, CSS, and Java script. They allow me control the basic foundation stuff, so I can avoid lock-in framework.
I start to use GWT (Google Web Toolkit) from 1.5 and it helps me quick demo UI layout and idea. I like to write a road map to explain how I learn.

Book:

Manning- GWT in action

Prentice Hall- Google Web Toolkit application

Apress- Accelerated GWT, Building Enterprise Google Web Toolkit Applications

Useful link:

1. GWT website

2. Born to code

3. GWTSite

4. GWT Widget Library

RESTFul and GWT

1. Restlet ported to GWT !

2. RESTful Client-Side tools for GWT

3. Build a dynamic organization tree using GWT and RESTful Web services

To start up customer java class or jar file when Java EE server starts. The solution usually is

1. Via WAR, servlet ; set the = 1 tag for servlet to provide intial invocation while the WAR is deployed.
Like this J2ee Start a class when application server starts

2. Depends on Java EE server setup.
For example, using weblogic startup and shutdown mechanism for EAR, In the weblogic-application.xml ; placed the following

//config xml

<startup>
<startup-class>MyStartup</startup-class>
<startup-uri>MyArchive.jar</startup-uri>
</startup>
<shutdown>
<shutdown-class>MyShutdown</shutdown-class>
<shutdown-uri>MyArchive.jar</shutdown-uri>
</shutdown>

I used second approach, so everytime I redeployed the EAR application the shutdown class main method was executed and then startup class invoked.

3. Using JBoss MBean service

Custom JMX MBeans in JBoss

ExampleHelloWorldService

How can an MBean depend on a SessionBean?

Writing JBoss MBean Services

Follow

Get every new post delivered to your Inbox.