Archive for March, 2009

Handling Timezone in webclient Timer

March 24, 2009

This post is the continuation of my previous post “Showing Timer in webclient”. – i could not handled timezone in my previous code. In detail if you are running a server in one timezone, say US(GMT-08:00)  and you are accessing the server webclient in different machine which has different timezone, say, India(GMT+05:30). So already written code is failed to show the correct server time.
First analyze, what was the problem in previous code:

var time = <%=System.currentTimeMillis()%>
var date = new Date(time);

In the above code, am getting server time in milliseconds and set the same in javascript Date object.
var date = new Date(time) – while executing this line, server will get browser timezone and set that timezone into this date object. Due to this we are getting incorrect server time.

Solution to this problem is by setting year, month, day, hours, minutes & seconds separately to Date class.  Here Date class does not take browser timezone.

<%
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int date = c.get(Calendar.DATE);
int hrs = c.get(Calendar.HOUR);
int mins = c.get(Calendar.MINUTE);
int secs = c.get(Calendar.SECOND);
%>
var date = new Date(<%=year%> , <%=month%> , <%=date%> , <%=hrs%> , <%=mins%> , <%=secs%> );

For better understanding, once again i put the entire code here with the updated one.

Sample code with Timezone handling.

<div id=”showTimer“></div> <!– Used to show the Digital Timer –>

<%
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int date = c.get(Calendar.DATE);
int hrs = c.get(Calendar.HOUR);
int mins = c.get(Calendar.MINUTE);
int secs = c.get(Calendar.SECOND);
%>

<script>

var months = new Array(”Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”);
function showCurrentTime(){
var date = new Date(<%=year%> , <%=month%> , <%=date%> , <%=hrs%> , <%=mins%> , <%=secs%> );
var showdate = date.getDate() + ” ” + months[date.getMonth()] + ” ” +date.getFullYear() + “, ” ;
var hrs = date.getHours();
var mins = date.getMinutes();
var secs = date.getSeconds();
hrs = (hrs > 9) ? hrs : “0″+hrs;
mins = (mins > 9) ? mins : “0″+mins;
secs = (secs > 9) ? secs : “0″+secs;
var showtime = hrs + ” : ” + mins + ” : ” + secs;
time = time+1000;
document.getElementById(”showTimer“).innerHTML=showdate + showtime;
}
setInterval(”showCurrentTime()”, 1000);
</script>

Vector vs ArrayList

March 21, 2009

Almost all basic java programmers have this doubt in his mind which one is best to use either Vector or ArrayList, Hashtable or HashMap. I don’t want to deviate this topic by evaluating the time required for insertions/deletions/modifications of data in each class instances. Just want to convey a main difference that’s enough for developers to use these classes in a better way. In Java, both the classes are similar from an API perspective but there has to be some differences. Let’s see below.

Vector

  • Vector is thread safe, because vector objects are synchronized which means you can access vector objects from any number of threads at the same time without affecting its data.
  • Due to the above reason, In performance wise Vector is slightly slower than ArrayList
  • Double the size of its internal array when it runs out of space.

ArrayList

  • ArrayList is just opposite of Vector class. Its not thread safe, suppose if you are using ArrayList instances in multiple threads surely which leads to create some problems while accessing.
  • ArrayList comes from Collection Framework family. So its easy to developers to convert in to any other datastructure formats.
  • Increases its array size by 50 percent.

According to me, when you are going to write code without threads it would be better to use ArrayList instead of Vector. Because this will improve your code performance. Anyway based on your requirements you can choose either Vector or ArrayList.

If you want to use/access ArrayList objects concurrently without affecting its data, its possible. In Java, an API is available to achieve this.

List asyncList = Collections.synchronizedList(new ArrayList());

Like Vector vs ArrayList, you can see the differences of Hashtable vs HashMap

Leadership

March 6, 2009

I came across this article in economic times, really nice one to read about leadership. Below are the points highlighted for a true leadership.

A true leader

  • is a person who is ready to take responsibility consciously.
  • he is ready to handle life consciously and he is not constantly dependent on the past.
  • is a person who is able to respond spontaneously to situations.
  • he is fresh in his ideas and continuously keeps himself alive.

Quality of Leadership

It comes from one’s ability to take responsibility for a particular organisation, a situation or a particular group with tremendous awareness and maturity.

Source : Leadership is state, not a status.

Method to convert string array to integer array in Java

March 4, 2009

Thanks to my friend to ask this question, hence this blog after long gap.
In Java , almost all code logics are processing in terms of objects. However some one new to java is not aware of this initially. For basic java learners, this blog will be helpful for understanding the array logic. Also java does not have any api’s to do this array conversion.
Lets explain the method.

  • input param is string array.
  • allocate sufficient memory for integer array intarray to store the converted value. – int intarray[] = new int[sarray.length];
  • finally iterate the string array values one by one and convert each value into an integer type.

public int[] convertStringArraytoIntArray(String[] sarray) throws Exception {
if (sarray != null) {
int intarray[] = new int[sarray.length];
for (int i = 0; i < sarray.length; i++) { intarray[i] = Integer.parseInt(sarray[i]); } return intarray; } return null; }[/sourcecode] Note : like this you can convert string in to any available data types(float , double,...) of  which you want.