One machine can do the work of fifty ordinary men. No machine can do the work of one extraordinary man

Friday, December 27, 2013

C# Part 1 : Hello World Program

This is a starting of new tutorial series for C# beginners. You don't need to have big experiences with any programming language and I am expecting very basic computer knowledge as an requirement.

I am conducting this sessions using 'Microsoft Visual Studio 2010' product. If you don't have licence version of that you can download free light version called 'Visual C# 2010 Express' from the Microsoft official web site.




Now we are going to write the "Hello World" program.

Step 1 : Open Visual Studio 2010 software


Step 2 : Then create a new project

File -> New -> Project


As the first program we are going to create a console C# application. Once you got the New Project window, follow the instructions in the above picture and press OK to create the project.

Then it will generate a simple program automatically for you. After that you can add following lines to the Main() method (which is the starting point of the program).

Console.WriteLine("Hello World");
Console.ReadLine();

Following picture describe the program properly.


1) Namespaces used by the program (Actually this small program required only 'System' namespace). Namespace is a collection of Classes, Delegates, Enums and Namespaces which we will go in depth in next tutorials.

2) Namespace of the current class

3) Declaration of the class (In this case the class name is 'Program')

4) Starting point of the program

5) Ending point of the program

6) 'WriteLine()' is the method inside the 'Console' class that we use to write something to the console. 'Console' class is in the 'System' namespace.

7) 'ReadLine()' is the method inside the 'Console' class that we use to read user inputs from the console (In this case we use this method to wait the program until we press any key).

Please put a comment if you have any doubts. Play around with this code and ask any question regarding the issues you faced. See you in the next tutorial.



Friday, March 29, 2013

Generate HTML Content in JAVA

This post is for generating HTML content in java. You can use 3rd party jar called rendersnake to do this kind of thing very easily. This has class called HTMLCanvas and you use hierarchical structure to build the HTML.

You can find more samples and information from
http://rendersnake.org/index.html
https://code.google.com/p/rendersnake/

This is a free opensource library and can find the license here : http://www.apache.org/licenses/LICENSE-2.0.html

Here is a sample code snippet

    HtmlCanvas htmlCanvas = new HtmlCanvas();

HtmlAttributes tableAttributes = new HtmlAttributes();
tableAttributes
.add("border", "1")
.add("bgcolor", "#E8E8E8")
.add("cellspacing", "0")
.add("cellpadding", "5")
.add("bordercolor", "#B0B0B0");

HtmlAttributes rowHeaderAttribute = new HtmlAttributes();
rowHeaderAttribute.add("bgcolor", "#C8C8C8");

HtmlAttributes taskRowHeaderAttribute = new HtmlAttributes();
taskRowHeaderAttribute.add("bgcolor", "#C8C8C8");
taskRowHeaderAttribute.add("width", "400");
taskRowHeaderAttribute.add("height", "50");

HtmlAttributes cellColor = new HtmlAttributes();
cellColor.add("bgcolor", "#009933");

htmlCanvas.html()
.body()
.b().content("Status : OK")
.br()
.table(tableAttributes).th(taskRowHeaderAttribute).content("Task")
.th(rowHeaderAttribute).content("User")
.th(rowHeaderAttribute).content("Host")
.th(rowHeaderAttribute).content("Status");

for(int i=0; i<10; i++) {
htmlCanvas.tr().td().content("Task " + i)
.td().content("User " + i)
.td().content("Host " + i)
.td(cellColor).content("Status " + 1)
._tr();
}
htmlCanvas._table().br().br()._body()._html();
   System.out.println("Result : " + htmlCanvas.toHtml());

Following is the result of above code.



You need to add the jar that downloaded from the above link and can do this kind of table very easily. If there is a exception occurred when you run the application call specifically "commons-lang3-3.1.jar" not found, you need to add that jar file as well. You can find this jar from http://commons.apache.org/proper/commons-lang/download_lang.cgi

Wednesday, March 27, 2013

Java DateTime Formatting

You can use following mechanism to get the date and time as a format you preferred. You need to use the Calendar class in the java.util package. It is possible to filter the date time using SimpleDateFormat class in the java.text package by passing the format to the constructor.

LetterDate or Time ComponentPresentationExamples
GEra designatorTextAD
yYearYear199696
MMonth in yearMonthJulyJul07
wWeek in yearNumber27
WWeek in monthNumber2
DDay in yearNumber189
dDay in monthNumber10
FDay of week in monthNumber2
EDay in weekTextTuesdayTue
aAm/pm markerTextPM
HHour in day (0-23)Number0
kHour in day (1-24)Number24
KHour in am/pm (0-11)Number0
hHour in am/pm (1-12)Number12
mMinute in hourNumber30
sSecond in minuteNumber55
SMillisecondNumber978
zTime zoneGeneral time zonePacific Standard TimePSTGMT-08:00
ZTime zoneRFC 822 time zone-0800



public String getTimeStamp(){
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();

SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_hhmmss");

String timeStamp = "";

try {
timeStamp = format.format(date);
} catch (Exception e1) {
   e1.printStackTrace();
}

return timeStamp;
}