Thursday, January 27, 2011

Timestampdemo

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
*
* @author R.Amirtharaj
*/
public class TimestampDemo {
public static void main(String arg[])
{
try {
long ct = System.currentTimeMillis();
Timestamp ts = new Timestamp(ct);
System.out.println("starttime =" + getStartTimeoftheDay(ts));
// New timestamp
Timestamp nts = getStartTimeoftheDay(ts);
// New Timestamp + 10 mins
Timestamp n10ts = getNextTimestamp(nts, 10);
System.out.println("n10ts =" + n10ts);
Timestamp n10lessts = getPrevTimestamp(nts, 10);
System.out.println("n10lessts =" + n10lessts);
} catch (Exception ex) {
ex.printStackTrace();
}

}


public static Timestamp getStartTimeoftheDay(Timestamp ts)
{
String fts = null;
Timestamp fts1 = null;
try {
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
fts = formatter.format(ts);
fts1 = new Timestamp(formatter.parse(fts).getTime());
//System.out.println("fts = "+fts);
} catch (Exception ex) {
}
return fts1;
}


public static Timestamp getPrevTimestamp(Timestamp cts, int prevTime)
{
long ols = cts.getTime()-(prevTime*60*1000);
Timestamp ots = new Timestamp(ols);
return ots;
}

public static Timestamp getNextTimestamp(Timestamp cts, int prevTime)
{
long ols = cts.getTime()+(prevTime*60*1000);
Timestamp ots = new Timestamp(ols);
return ots;
}

// For Geeting Oracle Dateformat
public static String getOracleDateFormat(Timestamp ts)
{
String fts = null;
try {
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
fts = formatter.format(ts);
//fts = new Timestamp(formattedcurTime);
//System.out.println("fts = "+fts);
} catch (Exception ex) {
ex.printStackTrace();
}
return fts;
}

public static Timestamp getRounded15Mins(Timestamp ts)
{
long ct = ts.getTime();
long diff = ct % (1000*60*15);
long nct = ct- diff;
Timestamp fts = new Timestamp(nct);
return fts;
}

public static Date getDateString(String str) {
Date fts = null;
try {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
fts = formatter.parse(str);
} catch (Exception ex) {
ex.printStackTrace();
}
return fts;
}

public static Date addHoures(String str, int nofHrs)
    {
        Date fts = null;
        try {
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            fts = formatter.parse(str);
            long ols = fts.getTime()+(nofHrs*60*60*1000);
            fts = new Date(ols);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return fts;
    }


}

Wednesday, January 19, 2011

Bulk insert queries

SQL:

Insert into tablename (attribute,attribute1) values(1,0),(2,0),(3,0)

ORACLE:

INSERT ALL
INTO tablename (attribute,attribute1) VALUES (1,0)
INTO tablename (attribute,attribute1) VALUES (2,0)
INTO tablename (attribute,attribute1) VALUES (3,0) select count(*) from dual;

Note : In Oracle U can Bulk Insert Maximum 1000 Columns otherwise it cause Exception.

Monday, January 10, 2011

Stretch Image in JLabel

JLabel myImageLabel = JLabel(new ImageIcon("Image Path"))
{
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(((ImageIcon)getIcon()).getImage(), 0, 0, getWidth(), getHeight(), null);
}
};

Monday, January 3, 2011

Date Differences


import java.util.Calendar;

public class DateDifferent{
public static void main(String[] args){
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
calendar1.set(2007, 01, 10);
calendar2.set(2007, 07, 01);
long milliseconds1 = calendar1.getTimeInMillis();
long milliseconds2 = calendar2.getTimeInMillis();
long diff = milliseconds2 - milliseconds1;
long diffSeconds = diff / 1000;
long diffMinutes = diff / (60 * 1000);
long diffHours = diff / (60 * 60 * 1000);
long diffDays = diff / (24 * 60 * 60 * 1000);
System.out.println("\nThe Date Different Example");
System.out.println("Time in milliseconds: " + diff
+
" milliseconds.");
System.out.println("Time in seconds: " + diffSeconds
+
" seconds.");
System.out.println("Time in minutes: " + diffMinutes
+
" minutes.");
System.out.println("Time in hours: " + diffHours
+
" hours.");
System.out.println("Time in days: " + diffDays
+
" days.");
}
}