You are currently browsing the monthly archive for February 2014.

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");
	}
}
Advertisements

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.