Apr 23, 2008

Using the Calendar class in J2ME for date and time

Parsing and displaying dates and times is often complicated because of formatting and locale issues. Java 2 Platform, Standard Edition (J2SE) provides several classes to simplify date and time handling -- classes such as java.util.Calendar, java.util.Date, java.util.TimeZone, and java.text.DateFormat. By comparison, the Mobile Information Device Profile (MIDP) defines only subsets of the Calendar, Date and TimeZone classes, and does not include any form of DateFormat. How, then, can your MIDP applications properly handle dates and times?

The answer lies in the javax.microedition.lcdui.DateField class, part of the MIDP high-level user interface API. DateField is an interactive user interface component that displays a date, time, or both. It also allows you to edit the date and time. DateField extends the Item class. This means that DateField components can be placed on Form objects. So the first step in using a DateField is to create a Form and place the DateField on the form:

    Form f = new Form( "A Form" );
f.append( df );

As with any Item, the DateField is displayed only when the form is made active by calling Display.setCurrent.

The DateField class defines two constructors:

    public DateField( String label, int mode );
public DateField( String label, int mode,
java.util.TimeZone zone );

To properly display dates and times, a DateField instance needs to know which time zone to use. The two-argument constructor uses the device's default time zone. The three-argument constructor lets you specify an explicit time zone if the default is inappropriate. Note that you can't change the displayed time zone without creating a new instance of DateField.

The first two arguments are identical in both constructors. The first argument is the label to display alongside the field -- use null if there is no label. The second argument is the input mode of the field. There are three possible modes, these are declared as constants in the DateField class:

    public static final int DATE = 1;
public static final int TIME = 2;
public static final int DATE_TIME = 3;

The input mode controls what the field displays: a date only, a time only, or a combined date and a time. You can change the input mode at any time by calling the setInputMode method.

When you create a new DateField instance, you do not have to set a date or time. The following code, for example, displays an uninitialized date:

    Display display = ....; // initialized elsewhere
Form f = new Form( "An Empty Date" );
DateField df = new DateField( "Date:",
DateField.DATE );
f.append( df );
display.setCurrent( f );

To initialize the field to a particular date or time, call setDate and pass in a java.util.Date object initialized to the correct value:

    Calendar c = Calendar.getInstance();
c.set( Calendar.MONTH, Calendar.OCTOBER );
c.set( Calendar.DAY_OF_MONTH, 18 );
c.set( Calendar.YEAR, 1996 );
c.set( Calendar.HOUR_OF_DAY, 16 );
c.set( Calendar.MINUTE, 39 );
c.set( Calendar.SECOND, 45 );
c.set( Calendar.MILLISECOND, 0 );

Date moment = c.getTime();
DateField df = new DateField( null,
DateField.DATE_TIME );
df.setTime( moment );

A Date object represents a moment in time (in coordinated universal time, or UTC, to be exact) as the number of milliseconds since midnight, January 1, 1970. Use a Calendar instance to create a Date instance, as shown above.

Note that a DateField in TIME input mode requires the date portion to be set to January 1, 1970. Two useful routines for clearing out the date portion of a Date and for combining two Date objects into a single object are as follows:

    // Return a Date with the time intact but the date
// set to January 1, 1970

public static Date clearDate( Date d ){
Calendar c = Calendar.getInstance();
c.setTime( d );
c.set( Calendar.MONTH, Calendar.JANUARY );
c.set( Calendar.DAY_OF_MONTH, 1 );
c.set( Calendar.YEAR, 1970 );
return c.getTime();
}

// Combine a date and time into a single
// Date instance

public static Date combineDateTime(
Date date, Date time ){
Calendar cd = Calendar.getInstance();
Calendar ct = Calendar.getInstance();

cd.setTime( date );
ct.setTime( time );

ct.set( Calendar.MONTH,
cd.get( Calendar.MONTH ) );
ct.set( Calendar.DAY_OF_MONTH,
cd.get( Calendar.DAY_OF_MONTH ) );
ct.set( Calendar.YEAR,
cd.get( Calendar.YEAR ) );

return ct.getTime();
}

Always do your date manipulation using the Calendar class, not using the raw milliseconds value stored in a Date object.

After a DateField is displayed, the system will allow the user to select the object and edit the date, time or both, depending on the input mode. Whenever you need to obtain the new date/time, call the getDate method:

DateField df = ....; Date editedDate = df.getDate();

Here is a simple MIDlet that lets you view and edit dates and times using all three input modes.

import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

/**
* Demonstration of time/date editing using the MIDP
* DateField class.
*/

public class DateFieldTest extends MIDlet {

private Display display;

// Define our Command objects

private Command exitCommand =
new Command( "Exit", Command.EXIT, 1 );
private Command okCommand =
new Command( "OK", Command.OK, 1 );
private Command cancelCommand =
new Command(
"Cancel", Command.CANCEL, 1 );

public DateFieldTest(){
}

protected void destroyApp( boolean unconditional )
throws MIDletStateChangeException {
exitMIDlet();
}

protected void pauseApp(){
}

protected void startApp()
throws MIDletStateChangeException {
if( display == null ){ // first time called...
initMIDlet();
}
}

private void initMIDlet(){
display = Display.getDisplay( this );
testList = new TestList();
display.setCurrent( testList );
}

public void exitMIDlet(){
notifyDestroyed();
}

// Return a Date with the time intact but the date
// set to January 1, 1970

public static Date clearDate( Date d ){
Calendar c = Calendar.getInstance();
c.setTime( d );
c.set( Calendar.MONTH, Calendar.JANUARY );
c.set( Calendar.DAY_OF_MONTH, 1 );
c.set( Calendar.YEAR, 1970 );
return c.getTime();
}

// Combine a date and time into a single
// Date instance

public static Date combineDateTime( Date date,
Date time ){
Calendar cd = Calendar.getInstance();
Calendar ct = Calendar.getInstance();

cd.setTime( date );
ct.setTime( time );

ct.set( Calendar.MONTH,
cd.get( Calendar.MONTH ) );
ct.set( Calendar.DAY_OF_MONTH,
cd.get( Calendar.DAY_OF_MONTH ) );
ct.set( Calendar.YEAR,
cd.get( Calendar.YEAR ) );

return ct.getTime();
}

// The list of tests we can perform, arranged
// in threes so that ( index % 3 ) == one
// of DATE, TIME or DATE_TIME

static final String[] testLabels = {
"Current date",
"Current time",
"Current date/time",
"Edit date",
"Edit time",
"Edit date/time",
};

private TestList testList;
private Date editDate;

//
// Displays the list of actions
//

class TestList extends List
implements CommandListener {
public TestList(){
super( "DateField Tests", IMPLICIT,
testLabels, null );
addCommand( exitCommand );
setCommandListener( this );
}

public void commandAction( Command c,
Displayable d ){
if( c == exitCommand ){
exitMIDlet();
} else if( c == List.SELECT_COMMAND ){

// Figure out which date to display
// and what the input mode is

int which = getSelectedIndex();
String label = getString( which );
int mode = ( which % 3 ) + 1;
boolean save = ( which > 2 );

display.setCurrent(
new Edit( save, label, mode ) );
}
}
}

//
// Edit a date, time or date/time, optionally
// saving the value.
//

class Edit extends Form
implements CommandListener {

public Edit( boolean save, String label,
int mode ){

super( label );
this.save = save;

Date d = editDate;

if( !save ){
d = new Date();
}

dateField = new DateField( null, mode );
append( dateField );

if( d != null ){
if( mode == DateField.TIME ){
d = clearDate( d );
}

dateField.setDate( d );
}

addCommand( okCommand );

if( save ){
addCommand( cancelCommand );
}

setCommandListener( this );
}

public void commandAction( Command c,
Displayable d ){
Alert alert = null;
Date date = dateField.getDate();

if(
save && date != null && c == okCommand ){
if( editDate != null ){
int mode = dateField.getInputMode();
if( mode == DateField.DATE ){
editDate = combineDateTime(
date, editDate );
} else if(
mode == DateField.TIME ){
editDate = combineDateTime(
editDate, date );
} else {
editDate = date;
}
} else {
editDate = date;
}

Calendar cal = Calendar.getInstance();
cal.setTime( editDate );

alert = new Alert( "New date/time" );
alert.setString(
"The saved date/time is now " + cal );
alert.setTimeout( Alert.FOREVER );
}

if( alert != null ){
display.setCurrent( alert, testList );
} else {
display.setCurrent( testList );
}
}

private DateField dateField;
private boolean save;
}
}

Apr 22, 2008

Searching pattern diagnosis

1. Getting the system clock info in j2me
system clock in j2me
http://forum.java.sun.com/thread.jspa?threadID=639491&messageID=3754711
http://www.velocityreviews.com/forums/t142811-j2me-date-amp-time-in-j2mewtk.html
http://moncom.net/moncomj2me.asp

Apr 1, 2008

Relativity of minds

While watching fours rain from Sehwag's bat, I thought of that conversation. It is apparent that Sehwag sees what others are incapable of seeing. Hitting fours is not an indulgence for him. It is his lifeline, an utterly natural course for conducting his business, just as singles are for many others.

Four years ago, Wisden Asia Cricket magazine ran a cover story on India's "Fab Five" - Sachin Tendulkar, Rahul Dravid, VVS Laxman, Sourav Ganguly and Sehwag. The feature had interviews with the five players, with each talking about one of the others. Ganguly made a fascinating revelation about Sehwag. "The best way to know how [Sehwag's] mind works is to sit next to him in the players' balcony when India are batting. Every few minutes he will clutch his head and yell, 'Chauka gaya' [missed out on a four] or 'Chakka gaya' [missed out on a six] ... That's how he thinks, in fours and sixes."

Feb 14, 2008

Michelangelo di Lodovico Buonarroti Simoni: My icon in arts


Michelangelo di Lodovico Buonarroti Simoni
(March 6, 1475February 18, 1564), commonly known as Michelangelo, was an Italian Renaissance painter, sculptor, architect, poet and engineer. Despite making few forays beyond the arts, his versatility in the disciplines he
took up was of such a high order that he is often considered a contender for the title of the archetypal Renaissance man, along with his rival and fellow Italian Leonardo da Vinci. Michelangelo, who was often arrogant with others and constantly dissatisfied with himself, saw art as originating from inner insp- iration and from culture. In contradiction to the ideas of his rival, Leonardo da Vinci, Michelangelo saw nature as an enemy that had to be overcome. The figures that he created are forceful and dynamic; each in its own space apart from the outside world. For Michel -angelo, the job of the sculptor was to free the forms that were already inside the stone. He believed that every stone had a sculpture within it, and that the work of sculpting was simply a matter of chipping away all that was not a part of the statue.

Though he devoted himself only to sculpture, Michel -angelo never stopped his daily practice of drawing. In his personal life, Michelangelo was abstemious. He told his appr -entice, Ascanio Condivi: "However rich I mayhave been, I have always lived like a poor man." Condivi said he was indifferent to food and drink, eating "more out of necessity than of pleasure" and that he "often slept in his clothes and ... boots." These habits may have made him unpopular; his biographer Paolo Giovio says "His nature was so rough and uncouth that his domestic habits were incredibly squalid, and deprived posterity of any pupils who might have followed him." He may not have minded, since he was by nature a solitary and melancholy person; he had a reputation for being bizzarro e fantastico because he "withdrew himself from the company of men."

Fundamental to Michelangelo's art is his love of male beauty, which attracted him both aesthetically and emotionally. In part, this was an expression of the Renaissance idealization of masculinity. But in Miche -angelo's art there is clearly a sensual response to this aesthetic.[12] Such feelings caused him great anguish, and he expressed the struggle between Platonic ideals and carnal desire in his sculpture, drawing and his poetry, too, for among his other accomplishments Michelangelo was also a great Italian lyric poet of the 16th century.

List of Michelangelo's work
http://en.wikipedia.org/wiki/List_of_works_by_Michelangelo

Dec 24, 2007

Chak de

Song that envigorates, inspires and stimulates



Shah Rukh Khan rocks

Aug 17, 2007

I d i o s y n c r a c y
--------------------
I was just visiting websites of university teachers. I am mailing them with a hope that some one shows interest on me to give me a funding for my MS or PhD program. I found the following thing on the page http://www.utdallas.edu/~besp/ Dr. Sergey Bereg of University of Texas Dallas. It grabbed my mind. I can't control copying and pasting it. Idiosyncracy of human mind!So many idiosyncracies we have bestowed with.

"There are 10 kinds of people in the world:
those who count in binary and those who don't."

Aoccdrnig to a rscheearch at Cmabrigde Uinervtisy, it deosn't mttaer
in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is
taht the frist and lsat ltteer be at the rghit pclae. The rset can be
a total mses and you can sitll raed it wouthit porbelm. Tihs is
bcuseae the huamn mnid deos not raed ervey lteter by istlef, but the
wrod as a wlohe.

Aug 13, 2007

S e r e n d i p i t y

2 Days back i returned from office . After having my repast, i went to the Flexiload shop for refilling our mobiles. I had a 100 taka note which is bit torn and taped to conceal its defect.
I thought it would be sheer cheating if i give the money to the shopkeeper in a tricky way.
I decided to show the note fully stretched and ask the shopkeeper if he agreed to take it. 3 shop
passed in turn, unfortunately no one agreed to take it. In the mean time rain started pouring.
I had found no one convinced to take the note atleast for today so that i could exchange the note for next day. Every one was unmoved. Even i went to the barbar shop who knows me since my childhood. He showed me his treasury wide open and i saw few ten taka notes. He even told to me
he had no problem giving me 500 taka but had not enough money to give me even though he trusts me.
I found no way except go back to home drenching in the rain to bring back fresh note. I did it
and returned home bedraggled.
I have Learned:
----------------
1. You have to cheat people in order to hew your own way.
2. Their is no value for trust.
3. You can not convince people mere on telling the truth.

I FOUND:
-----------
1. Every action has an equal and opposite reaction. This law is not true only for physical object.
It is also true for actions that are intangible. I should get opposite reaction and equal of what i have done. May be some time later i will receive a sweetest return for what i have done.

Notebooks

illuminate.google.com notebooklm.google.com