Thursday, July 10, 2008

java.util.Calendar package

/*This class shows the different different formta of date.
*dd-mm-yyyy
*dd-MM-yyyy-mm-ss
*The java.util.Calendar class is used to represent the date and time.
The year, month, day, hour, minute, second, and milliseconds can all
be set or obtained from a Calendar object. The default Calendar object
has the current time in it. There are also methods for making data calculations.
*/
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DataFormats
{

public static final String DATE_FORMAT_1 = "yyyy-MM-dd";
public static final String DATE_FORMAT_2 = "yyyy-MM-dd-mm-ss";
public static void main(String[] args)
{
String ddMMYyyy=getDDMMYYYY();
String ddMMYyyymmss=getDDMMYYYYmmss();
System.out.println("Date in dd-MM-yyyy Format = "+getDDMMYYYY());
System.out.println("Date in dd-MM-yyyy-mm-ss Format = "+getDDMMYYYYmmss());
}
//Method for date in dd-MM-yyyy format
public static String getDDMMYYYY()
{
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_1);
return sdf.format(cal.getTime());
}
//Method for date in dd-MM-yyyy-mm-ss format
public static String getDDMMYYYYmmss()
{
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_2);
return sdf.format(cal.getTime());
}

}

Wednesday, July 2, 2008

Hitting a URL and get page content into a string

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class UrlHit {

public static void main(String args[])
{
try {

URL cricurl = new URL("http://codeforjava.blogspot.com/2008/06/method-for-copying-only-files-from-one.html");
URLConnection criccon = cricurl.openConnection();
criccon.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(cricurl.openStream()));
String s="";
String strTemp="";


while((strTemp=br.readLine()) != null)
{
strTemp = br.readLine();
s=s.concat(strTemp);
}
s=s.trim();


System.out.println("Got url content - "+s);




} catch (Exception e)
{
e.printStackTrace();
}

}

}

Tuesday, June 24, 2008

Method for copying only files from one location to another location

public static boolean copyAllFiles(File src,File dst) throws IOException{
boolean flag = false;
try
{
if (src.isDirectory())
{
if (!dst.exists())
{
dst.mkdir();
}
File[] srcArr = src.listFiles();
ArrayList flist = new ArrayList();
for(int i=0;i {
if(!srcArr[i].isDirectory())
{
String fileName = srcArr[i].getAbsolutePath();
String[] fileArr = fileName.split("\\\\");
flist.add(fileArr[fileArr.length-1]);
}
}
for (int i=0; i{
copyAllFiles(new File(src, flist.get(i).toString()), new File(dst, flist.get(i).toString()));
}
flag = true;
}
else
{
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
in.close();
out.close();
flag = true;
}
}
catch(Exception ex)
{ flag = false; }
return flag;
}

java jdbc connectivity with sql server 2000

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connUrl="jdbc:sqlserver://localhost:1433;database=testDB";
Connection con=DriverManager.getConnection(connUrl,dbUser,dbPass);
Statement stmtVMS = con.createStatement();