Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, February 20, 2010

Calendar of a month using Java

import java.util.*;
/*
* Printing the calendar of a month where the user
* Gives the input month , year
* By Shyamala
*/
class CalendarForMonth{
public static void main(String args[]){
//A string array for months

String months []={"Jan","Feb","March","April","May","June","July","Aug","Sept","Oct","Nov","Dec"};

//An integer for number of days in every month
int nDays[]={31,28,31,30,31,30,31,31,30,31,30,31};

//creating the instance of calendar
Calendar calendar = Calendar.getInstance();

Scanner in = new Scanner(System.in);
//Take the input from the user Month,Year

System.out.print("Enter month ,year :");
int mon = in.nextInt(); int yr = in.nextInt();
calendar.set(yr,mon-1,1); //setting date yr,mon,date consider first day

if(yr%4==0){
System.out.println("Leap year!!");
//change the number of days Feb month as it is leap year
nDays[1]=29;
}

System.out.println("Calendar For "+calendar.get(Calendar.YEAR)+" == "+months[calendar.get(Calendar.MONTH)]);

//printing the days
System.out.print("S\tM\tTu\tW\tTh\tF\tSa");
int day = calendar.get(Calendar.DAY_OF_WEEK);
//System.out.println(day);
System.out.println();

//gives you the starting day
int nline=0;
//for getting new lines

for(int i=1;i<=nDays[mon-1];i++){
System.out.print(i+"\t");
nline++;
if(nline%7==0)
System.out.println("\n");
}

}

}

Friday, June 5, 2009

Creating Jar file (Java executables) - Very simple

Step 1:
Create a Hello.java class .
public class Hello{
public static void main(String args[]){
System.out.println("welcome to Java’s chronicle");
}
}

Step-2 :
Compile the above java file.

Cmd > javac Hello.java

Step- 3:
Create a mainClass.txt with the following contents

Main-Class: Hello

Note :
Remember the above must be terminated with carriage return coz some of the OS expects CR to create Manifest file.

Step- 4:
Go to command prompt create jar file

Cmd > jar -cvmf mainClass.txt my1.jar Hello.class
success :
added manifest
adding: Hello.class(in = 411) (out= 282)(deflated 31%)

where :
c-create jar

v-verbose
m-mainclassfile

Step 5:
To list the contents of jar

Cmd > jar tf my1.jar

META-INF/
META-INF/MANIFEST.MF
Hello.class

Step 6:
To Execute jar

Cmd > java –jar my1.jar

I just tried explaining in a layman terminology ...Hope it is useful
Further any queries kindly get back to me......

Keep Rocking
Shyamala