Dounload free Programs

Sunday, October 31, 2010

Date Parsing in Java

High , Friend this is small but irritating program when u can't manage it properly.U can create date object of any date in any input format changing the SipleDateFormat constructor parameter.

//Program code...
 
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateParse {

    public static void main(String ars[])
    {
    DateParse dp=new DateParse();
    Date mm=dp.paseDate("22-2-1988");
    System.out.println("date="+mm.toString());
   
    }
    public Date paseDate(String dateParam)
    {
        Date tt=new Date();
       
        SimpleDateFormat smdf=new SimpleDateFormat("dd-MM-yyyy");
        System.out.println("");
        try
        {
        tt=smdf.parse(dateParam);
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        return tt;
    }
}

Monday, October 25, 2010

New Code for Muliplication of two matrices

Here I have given code for multiplication of two matrices.You can check it on your machine and give me reply I alsocreated code for finding inverse of any matrix and will post if you requested it.
public class MultiplyTwoMatrix {
    public static void main(String ar[])
    {
        int matrix1[][],row,column,matrix2[][],a[],result[][],row2,column2;
        java.util.Scanner sc=new  java.util.Scanner(System.in);
    int count=0;
        do{
            if(count>0)
        System.out.println("enter the valid row & column (column1=row2) for multiplication");
            System.out.println("enter the row & column of first matrix");
        row=sc.nextInt();
        column=sc.nextInt();
        System.out.println("enter the  column of second matrix");
        row2=column;
        column2=sc.nextInt();
        count++;
    }while(!(column==row2));
       
       
        matrix1=new int[row][column];
        matrix2=new int[row2][column2];
        result=new int[row][column2];
        a=new int[column];
        int sum=0;
        System.out.println("Enter fist matrix"+row+"x"+column);
        for(int i=0;i<row;i++)
        {
            for(int l=0;l<column;l++)
                matrix1[i][l]=sc.nextInt();
               
        }
        System.out.println("Enter second matrix"+row2+"x"+column2);
        for(int i=0;i<row2;i++)
        {
            for(int l=0;l<column2;l++)
                matrix2[i][l]=sc.nextInt();
        }       
        for(int i=0;i<row;i++)
        {
            for(int l=0;l<column;l++)
                    a[l]=matrix1[i][l];
            for(int j=0;j<column2;j++)
            {
                sum=0;
                for(int l=0;l<column;l++)
                    sum+=a[l]*matrix2[l][j];
                result[i][j]=sum;
            }
        }
        //print output
        for(int i=0;i<row;i++)
        {
            for(int l=0;l<column2;l++)
                System.out.print("\t"+result[i][l]);
            System.out.println();
        }
       
       
    }

}

Wednesday, October 20, 2010

Oracle Help get date from system in required format and its utility functions

the most often problem face by oracle+java programmer is when he code for getting the records on date variable and date format is different from the sql required format . It was the most irritating problem face by programmer i am giving solution for that u can check the below query on oracle .  

select to_date ('12-1-10','DD/MM/YYYY') from dual;


you will get the
TO_DATE('
---------
12-JAN-10

as output Nice !!
Some times it 's require that u have to minus the days e.g. 4 days from your date variable then u can do directly without coding it in java,

select to_date ('12-1-10','DD/MM/YYYY')-4 from dual;

the output will be,
TO_DATE('
---------
08-JAN-10

Monday, October 18, 2010

Rounding the double value in java

 For rounding the double value You can use the following code . some time its require that you have to round the value of double literal .
e.g . 1) 12.34656 require to round to 12.35
        2) 165.454 to 165.5 etc .
For that case you can use following code you are require to only create object of RoundUp class with input parameter double value  and up to which decimal you have to round value.
e.g. for  fist 1) RoundUp ru=new RoundUp(12.34656,2);
                    BigDecimal roundedValue=ru.roundUP();
                    2)RoundUp ru=new RoundUp(165.454,1);
                    BigDecimal roundedValue=ru.roundUP();




 //code section
import java.math.BigDecimal;

public class RoundUp {
    private double dbl;
    private int roundUpTo;
    public RoundUp(double Value,int roundTodec)
    {
        dbl=Value;
        roundUpTo=roundTodec;
    }
      public BigDecimal roundUP()
        {
            BigDecimal bd=new BigDecimal(dbl);
            return bd.setScale(roundUpTo,BigDecimal.ROUND_UP);
        }

}

Java Program for finding row and colum No in Jxl.

Most off time jxl programmer face the problem of getting the value of Cell  by its name.You can use following class for getting row and column parameter. You have to create only new object of this class  with  namebox in xlsheet (Cell name)as input parameter and call the the get method of row and column  .
e.g.
JxlColumnRow jcr=new JxlColumnRow("B12");
Cell cs= sheetObject.getCell(jcr.getColumn(),jcr.geRow());

//Code Section
public class JxlColumnRow {
    private String[] letters = new String[] {"","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    private String nameBox="";
    public JxlColumnRow(String nameBx)
    {
        nameBox=nameBx;
    }
    public int getRow()
    {
        String ans = "";
        for(int i=1;i<nameBox.length()+1;i++)
        {
            if(isInteger(nameBox.substring(i-1,i)))
            {
                ans = nameBox.substring(i-1);
                break;
            }
        }
        return Integer.parseInt(ans)-1;
    }
       

    public int getColumn()
    {
        int ans = 0;
        String col = "";
        for(int i=0;i<nameBox.length();i++)
        {
            String temp = nameBox.substring(i,i+1);
            if(isInteger(temp))
            {
                col = nameBox.substring(0,i);
                break;
            }
        }
        for(int i=0;i<col.length();i++)
        {
            int count = col.length();
            String temp = col.substring(i,i+1);
            for(int x=0;x<letters.length;x++)
            {
                if(temp.equals(letters[x]))
                {
                    int tempCount = count-i;
                    ans += (Math.pow(26,tempCount-1)*x);
                    break;
                }
            }
        }
        return ans-1;
    }
   
    //Dirty method to determine if inputted string is an integer
    public boolean isInteger( String input )
    {
    try {
        Integer.parseInt( input );
        return true;
    }
    catch( Exception e ) {
        return false;
    }
    }
 

}

Java Help

Hello , Friends I am a Java  programmer .I am wishing to help you with giving  lot of  free example on small java   program on this blog. U will get lot of  essential code on this site .I am continuously improving my blog with more excellent program beneficial for you. I am wishing reply and suggestion from you and who know about java because no one is perfect in this word and  knowledge  of sea is not touchable to single  my humble request to all you that suggest you pinion.