Sunday, July 27, 2008

Java: Date Format

Code Sample:

import java.text.SimpleDateFormat;

java.util.Date today = new java.util.Date();
SimpleDateFormat sdfOutput = new SimpleDateFormat ( "yyyyMMdd" );
String strToday = sdfOutput.format( today );

Date and Time Patterns as below:

Letter: G
Date or Time Component: Era designator
Presentation: Text
Examples: AD

Letter: y
Date or Time Component: Year
Presentation: Year
Examples: 1996; 96

Letter: M
Date or Time Component: Month in year
Presentation: Month
Examples: July; Jul; 07

Letter: w
Date or Time Component: Week in year
Presentation: Number
Examples: 27

Letter: W
Date or Time Component: Week in month
Presentation: Number
Examples: 2

Letter: D
Date or Time Component: Day in year
Presentation: Number
Examples: 189

Letter: d
Date or Time Component: Day in month
Presentation: Number
Examples: 10

Letter: F
Date or Time Component: Day of week in month
Presentation: Number
Examples: 2

Letter: E
Date or Time Component: Day in week
Presentation: Text
Examples: Tuesday; Tue

Letter: a
Date or Time Component: Am/pm marker
Presentation: Text
Examples: PM

Letter: H
Date or Time Component: Hour in day (0-23)
Presentation: Number
Examples: 0

Letter: k
Date or Time Component: Hour in day (1-24)
Presentation: Number
Examples: 24

Letter: K
Date or Time Component: Hour in am/pm (0-11)
Presentation: Number
Examples: 0

Letter: h
Date or Time Component: Hour in am/pm (1-12)
Presentation: Number
Examples: 12

Letter: m
Date or Time Component: Minute in hour
Presentation: Number
Examples: 30

Letter: s
Date or Time Component: Second in minute
Presentation: Number
Examples: 55

Letter: S
Date or Time Component: Millisecond
Presentation: Number
Examples: 978

Letter: z
Date or Time Component: Time zone
Presentation: General time zone
Examples: Pacific Standard Time; PST; GMT-08:00

Letter: Z
Date or Time Component: Time zone
Presentation: RFC 822 time zone
Examples: -0800

Examples
Date and Time Pattern: "yyyy.MM.dd G 'at' HH:mm:ss z"
Result: 2001.07.04 AD at 12:08:56 PDT

Date and Time Pattern: "yyyy.MM.dd G 'at' HH:mm:ss z"
Result: Wed, Jul 4, '01

Date and Time Pattern: "h:mm a"
Result: 12:08 PM

Date and Time Pattern: "hh 'o''clock' a, zzzz"
Result: 12 o'clock PM, Pacific Daylight Time

Date and Time Pattern: "K:mm a, z"
Result: 0:08 PM, PDT

Date and Time Pattern: "yyyyy.MMMMM.dd GGG hh:mm aaa"
Result: 02001.July.04 AD 12:08 PM

Date and Time Pattern: "EEE, d MMM yyyy HH:mm:ss Z"
Result: Wed, 4 Jul 2001 12:08:56 -0700

Date and Time Pattern: "yyMMddHHmmssZ"
Result: 010704120856-0700

Date and Time Pattern: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
Result: 2001-07-04T12:08:56.235-0700

Saturday, July 26, 2008

Java: Compare 2 dates

import java.util.Date;

public class CompareDate {
   public static void main(String[] args) {
      Date dt2 = new Date("7/26/2004");
      Date dt3 = new Date("7/25/2004");

      if (dt2.compareTo(dt3) > 0) {
         System.out.println("dt2>dt3");
      } else {
         System.out.println("dt3<=dt2");
      }
   }
}

Java: Check server response

Java Version 1.4

try
{
   java.net.Socket s = new Socket();
   s.connect(new InetSocketAddress("192.168.0.157", 7777), 1000);
   s.close();
   System.out.println("Success");
}catch (Exception ex){
   System.out.println("Error : " + ex.getMessage());
   // failed
   // do redirect
}

Java Version 1.3

try{
   java.net.Socket s = new Socket(host, port);
   s.close();
}catch (Exception ex){
   // failed
   // do redirect
}

Java: Cast ArrayList to Array

import java.util.ArrayList;
public class CastAryListToArray {
public static void main(String arg[]){
//How to cast ArrayList to Array
//The following code will store arraylist value into array:
ArrayList al = new ArrayList();
al.add("a");
al.add("b");
al.add("c");
String XAxisValue[] = new String[al.size()];
al.toArray(XAxisValue);

for(int i=0;i<XAxisValue.length;i++){
   System.out.println("XAxisValue["+i+"] = " + XAxisValue[i]);
}
}
}

Java: Call an Oracle Stored Procedure

/*

* This sample shows how to call a PL/SQL stored procedure using the SQL92

* syntax.

*/

import java.sql.*;
import java.io.*;

class PLSQLExample {
public static void main(String args[]) throws SQLException, IOException {
// Load the driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

// Connect to the database
// You can put a database name after the @ sign in the connection URL.
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.157:1521:mydb",
"scott", "tiger");

// Create a statement
Statement stmt = conn.createStatement();

// Create the stored function
stmt.execute("create or replace function RAISESAL (name CHAR, raise NUMBER)return
NUMBER is begin return raise + 100000; end;");

// Close the statement
stmt.close();

// Prepare to call the stored procedure RAISESAL.
// This sample uses the SQL92 syntax
CallableStatement cstmt = conn.prepareCall("{? = call RAISESAL (?, ?)}");

// Declare that the first ? is a return value of type Int
cstmt.registerOutParameter(1, Types.INTEGER);

// We want to raise LESLIE's salary by 20,000
cstmt.setString(2, "LESLIE");

// The name argument is the second ?
cstmt.setInt(3, 20000);

// The raise argument is the third ?
// Do the raise
cstmt.execute();

// Get the new salary back
int new_salary = cstmt.getInt(1);
System.out.println("The new salary is: " + new_salary);

// Close the statement
cstmt.close();

// Close the connection
conn.close();
}
}

Java: 2D Array Declaration

//2D Array declaration
int array1[][] = new int [2][2];

//2D Array assignment
array1[0][0] = 1;
array1[0][1] = 2;
array1[1][0] = 3;
array1[1][1] = 4;

//Print 2D Array
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
System.out.println(array1[i][j]);
}
}

Since 26 July 2008: