Wednesday, July 31, 2013

Java Date Time Format

String str = "12/9/2010 4:39:38 PM";
Formatter formatter = new SimpleDateFormat("M/dd/yyyy H:m:s a");
Date date =(Date)formatter.parse(str);
Formatter formatterOutput = new SimpleDateFormat("MM/dd/yyyy HH:m:ss a");
String s = formatterOutput.format(date);


Format formatter;

// The year
formatter = new SimpleDateFormat("yy");    // 02
formatter = new SimpleDateFormat("yyyy");  // 2002
ime
// The month
formatter = new SimpleDateFormat("M");     // 1
formatter = new SimpleDateFormat("MM");    // 01
formatter = new SimpleDateFormat("MMM");   // Jan
formatter = new SimpleDateFormat("MMMM");  // January

// The day
formatter = new SimpleDateFormat("d");     // 9
formatter = new SimpleDateFormat("dd");    // 09

// The day in week
formatter = new SimpleDateFormat("E");     // Wed
formatter = new SimpleDateFormat("EEEE");  // Wednesday

// Get today's date
Date date = new Date();

// Some examples
formatter = new SimpleDateFormat("MM/dd/yy");
String s = formatter.format(date);
// 01/09/02

formatter = new SimpleDateFormat("dd-MMM-yy");
s = formatter.format(date);
// 29-Jan-02

// Examples with date and time; see also
// Formatting the Time Using a Custom Format
formatter = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
s = formatter.format(date);
// 2002.01.29.08.36.33

formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
s = formatter.format(date);
// Tue, 09 Jan 2002 22:14:02 -0500

Java Age Calculate

 public static String getAge(String dateOfBirth) {
       int age;
       int age_m;
       int age_d;
      // String dob = "1984-09-20";
       String dob = dateOfBirth;

            //TAKE SUBSTRINGS OF THE DOB SO SPLIT OUT YEAR, MONTH AND DAY
            //INTO SEPERATE VARIABLES
            int yearDOB = Integer.parseInt(dob.substring(0, 4));
            int monthDOB = Integer.parseInt(dob.substring(5, 7));
            int dayDOB = Integer.parseInt(dob.substring(8, 10));

            //CALCULATE THE CURRENT YEAR, MONTH AND DAY
            //INTO SEPERATE VARIABLES
            DateFormat dateFormat = new SimpleDateFormat("yyyy");
            java.util.Date date = new java.util.Date();
            int thisYear = Integer.parseInt(dateFormat.format(date));

            dateFormat = new SimpleDateFormat("MM");
            date = new java.util.Date();
            int thisMonth = Integer.parseInt(dateFormat.format(date));

            dateFormat = new SimpleDateFormat("dd");
            date = new java.util.Date();
            int thisDay = Integer.parseInt(dateFormat.format(date));

            //CREATE AN AGE VARIABLE TO HOLD THE CALCULATED AGE
            //TO START WILL – SET THE AGE EQUEL TO THE CURRENT YEAR MINUS THE YEAR
            //OF THE DOB
            age = thisYear - yearDOB;

            //IF THE CURRENT MONTH IS LESS THAN THE DOB MONTH
            //THEN REDUCE THE DOB BY 1 AS THEY HAVE NOT HAD THEIR
            //BIRTHDAY YET THIS YEAR
            if(thisMonth < monthDOB){
                age_m = monthDOB-thisMonth;
            }else{
            age_m = thisMonth-monthDOB;
            }
            if(thisMonth == monthDOB){
                age_m = 0;
            }
            //IF THE MONTH IN THE DOB IS EQUEL TO THE CURRENT MONTH
            //THEN CHECK THE DAY TO FIND OUT IF THEY HAVE HAD THEIR
            //BIRTHDAY YET. IF THE CURRENT DAY IS LESS THAN THE DAY OF THE DOB
            //THEN REDUCE THE DOB BY 1 AS THEY HAVE NOT HAD THEIR
            //BIRTHDAY YET THIS YEAR
            if(thisDay < dayDOB){
            age_d = dayDOB - thisDay;
            }else{
            age_d =  thisDay - dayDOB;  
            }

            if(thisDay == dayDOB){
            age_d = 0;
            }

            //THE AGE VARIBALE WILL NOW CONTAIN THE CORRECT AGE
            //DERIVED FROMTHE GIVEN DOB
            System.out.println(age);
            String p = age + " Year " + age_m + " Month " + age_d + " Days ";
            return p;
    }

Monday, July 29, 2013

Java Get Date Time

 /**
  * @author Nazmus Sakib
 */

  //  Print Today's Date time

public String todays_datetime()
    {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                java.util.Date date = new java.util.Date();
                // System.out.println(dateFormat.format(date));
                return (dateFormat.format(date));

        }

Output is : 2013-07-30 10:55:05


// Print Next Month
public String next_month_year()
    {
            Calendar cal = GregorianCalendar.getInstance();
            SimpleDateFormat df = new SimpleDateFormat("MMMM yyyy");

            Date currentMonth = new Date();
            cal.setTime(currentMonth);

            // current month
            String currentMonthAsSting = df.format(cal.getTime());

            // Add next month
            cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)+1);
            //cal.add(Calendar.MONTH, 1);
            String nextMonthAsString = df.format(cal.getTime());

            System.out.println(nextMonthAsString);     
                return (nextMonthAsString);

        }

Java HTML Alert


/**
 *   Java HTML Alert Box
 * @author Nazmus Sakib
 */



private String msg_access = "<HTML> Access Denied <br>"
                                    + "Already Count This Figure In Salary</HTML>";


  JOptionPane.showMessageDialog(null,msg_access, "Already Count", OptionPane.ERROR_MESSAGE);

Friday, July 26, 2013

Java to Oracle Database Connection with net beans IDE

package Utility;

/**
 *
 * @author Sakib
 * Datacraft LTD
 */

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.*;
public class OracleJDBC {
  
    public static void main(String[] argv) {

        System.out.println("** ------ Oracle JDBC Connection Testing ------ **");

        try {

            Class.forName("oracle.jdbc.driver.OracleDriver");

        } catch (ClassNotFoundException e) {

            System.out.println("Where is your Oracle JDBC Driver?");
            e.printStackTrace();
            return;

        }

        System.out.println("Oracle driver registered");
        Connection conn=null;

        try {

            conn = DriverManager.getConnection( "jdbc:oracle:thin:@127.0.0.1:1521:xe", "sakib","Abc999");

            Statement stmt= conn.createStatement();
            ResultSet r=stmt.executeQuery("Select * from employees");
            while(r.next()){
            String str= r.getString("FIRSTNAME");
            System.out.println (str);
           
            }

        } catch (SQLException e) {

            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return;

        }
    }
}

Thursday, July 25, 2013

Java to MySQL Database Connection with net beans IDE

Java to MySQL Database Connection with net beans IDE


package Utility;
/**
 *
 * @author Nazmus Sakib
 */

import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;


public class javaconnect {
   
    Connection conn = null;
    public static Connection ConnecrDb(){
       
        try {
             Class.forName("com.mysql.jdbc.Driver");
             Connection conn = (Connection)DriverManager.getConnection("jdbc:mysql://localhost/dcl_payrole","root","123456");
             return conn;
        }catch (Exception e) {
           
            JOptionPane.showMessageDialog(null, e);
            return null;
            
        }
      
    }
   
}