Content about Java ERP Solution for Reviews Best ERP Software for sale and Free Software with Java OpenSource for Free Download
Showing posts with label Developer Basic Java. Show all posts
Showing posts with label Developer Basic Java. Show all posts

Java Date Format with SimpleDateFormat

Java Date Format with SimpleDateFormat
You can use  java.text.SimpleDateFormat to farmat Date
 For example in below

 Date date=new Date();  
 System.out.println("No format :"+date);  
 java.text.SimpleDateFormat df= new java.text.SimpleDateFormat();  
 System.out.println("No pattern format :"+df.format(date));  
 df.applyPattern("dd/mm/yyyy");  
 System.out.println("dd/mm/yyyy format :"+df.format(date));  
 df.applyPattern("dd/mm/yyyy HH:mm:ss");  
 System.out.println("dd/mm/yyyy HH:mm:ss format :"+df.format(date));  

Out put when run this code
No format :Thu Apr 05 16:29:18 ICT 2012
No pattern format :5/4/2555, 16:29 ?.
dd/mm/yyyy format :05/29/2555
dd/mm/yyyy HH:mm:ss format :05/29/2555 16:29:18

Java ArrayList Example how to use Array List for add content and loop result

Exapmple code for using java.utill.ArrayList

 package example.util;  
 import java.util.ArrayList;  
 import java.util.HashSet;  
 import java.util.Iterator;  
 import java.util.Vector;  
 public class ArrayListExample {  
   public void arrayListExample(){  
     //Create ArrayList Object  
     ArrayList arrayList=new ArrayList();  
     //Store data to ArrayList  
     arrayList.add("value1");  
     arrayList.add("value2");  
     arrayList.add("value3");  
     arrayList.add("value4");  
     arrayList.add("value5");  
     //Test Print all data with toString()  
     System.out.println("ArrayList Data befor "+arrayList);  
     //Remove data from ArrayList  
     String removeValue=arrayList.remove(2);      
     boolean remove1=arrayList.remove("value1");  
     boolean remove2=arrayList.remove("value3");  
     Vector v=new Vector();  
     v.add("value2");      
     boolean remove3=arrayList.removeAll(v);  
     //Removed data can return in VAL  
     System.out.println("Remove Value "+removeValue);  
     System.out.println("Remove1 "+remove1);  
     System.out.println("Remove2 "+remove2);  
     System.out.println("Remove3 "+remove3);  
     System.out.println("ArrayList Data after "+arrayList);  
     //Ged data from ArrayList  
     System.out.println("-- Example for Get Value from ArrayList --");  
     System.out.println("Get ArrayList value: "+arrayList.get(0));  
     System.out.println("Contains ArrayList: "+arrayList.contains("value1"));  
     System.out.println("Contains ArrayList: "+arrayList.contains("value3"));  
     //Loop data from Array List  
     System.out.println("-- Example for Iterate list from ArrayList --");  
     Iterator it=arrayList.iterator();  
     while(it.hasNext()){  
       String value=it.next();  
       System.out.println("List Iterated Value: "+value);  
     }  
     System.out.println("-- Example for Loop from ArrayList --");  
     for (String temp:arrayList){  
       System.out.println("For value ArrayList "+temp);  
     }  
   }  
   public static void main (String args[]){  
     //Test the code in main method  
     ArrayListExample arryList=new ArrayListExample();  
     arryList.arrayListExample();  
   }  
 }  

Test Result in below
ArrayList Data befor [value1, value2, value3, value4, value5]
Remove Value value3
Remove1 true
Remove2 false
Remove3 true

ArrayList Data after [value4, value5]
-- Example for Get Value from ArrayList --
Get ArrayList value: value4
Contains ArrayList: false
Contains ArrayList: false

-- Example for Iterate list from ArrayList --
List Iterated  Value: value4
List Iterated  Value: value5

-- Example for Loop from ArrayList --
For value ArrayList value4
For value ArrayList value5

Java HashSet Example Code for add and collect data from HashSet

Example Code for using java.util.HashSet

HashSet is a Class in package java.util we can store data and collect data from HashSet like Array
      

     //Create Instance for HashSet of String  
     HashSet hashSet=new HashSet();  
     //Add data to HashSet ...the same way for list  
     hashSet.add("aa");  
     hashSet.add("bb");  
     hashSet.add("ab");  
     //Check contain data in HashSet  
     System.out.println("HashSet contails "+hashSet.contains("aa"));  
     System.out.println("HashSet contails "+hashSet.contains("cc"));  
     //Loop data in hash Set  
     Iterator it=hashSet.iterator();  
     while(it.hasNext()){  
       System.out.println("HashSet Next Data "+it.next());  
     }  
Result when run program

HashSet contails true
HashSet contails false
HashSet Next Data aa
HashSet Next Data ab
HashSet Next Data bb

Java TreeSet Example to sorting data with java TreeSet

Java TreeSet is a Class int package java.util
We can use TreeSet to auto sorting data in the Tree for example in below

 Example to using java.util.TreeSet
   //Create instance for TreeSet of String  
     TreeSet treeSet=new TreeSet();  
     //Store data to TreeSet with mix order  
     treeSet.add("aa1");  
     treeSet.add("aa2");  
     treeSet.add("aa3");  
     treeSet.add("bb1");  
     treeSet.add("bb2");  
     //Test containing data .. the same function for Other Collection  
     System.out.println("TreeSet contails "+treeSet.contains("aa1"));  
     System.out.println("TreeSet contails "+treeSet.contains("bb1"));  
     //Loop data from TreeSet  
     Iterator it=treeSet.iterator();  
     while(it.hasNext()){  
       System.out.println("TreeSet Next Data "+it.next());  
     }  
Result when run program
 TreeSet contails true
TreeSet contails true
TreeSet Next Data aa1
TreeSet Next Data aa2
TreeSet Next Data aa3
TreeSet Next Data bb1
TreeSet Next Data bb2
Amazed !! data will sorting automatic yes

Java StringBuffer Example to append a lot of String with StringBuffer

When we need to append String with java String we can using a simple Code in below
String x="test1";
x=x+"test2" ;
With this code have some problem when we adding a lot of String it's not support !! surprise
We have the best solution to append String by using StringBuffer we can append a lot of data laugh
StringBuffer is a Class in paclage java.lang 
 Example Code for using StringBuffer
 //Create instance for StringBuffer  
     StringBuffer stringbuffer =new StringBuffer();  
     //Append String to StringBuffer  
     stringbuffer.append("str1");  
     stringbuffer.append("str2");  
     stringbuffer.append("str3");  
     /*  
     ..  
     .. You can append a lot of data you want  
     ..      
     */  
     //Transform it to String  
     String data=stringbuffer.toString();  
     System.out.println("StringBuffer data :"+data);  
            Out put when Run Program

StringBuffer data :str1str2str3

Java Hashmap Example example code to using Hashmap in Java

HashMap is a Collection Class in package java.util
HashMap store data with key and value for example in below

Example to using HashMap in Java

 package example.util;  
 import java.util.*;  
 public class HashmapExample {  
   public void hashmapExample(){  
     //Create HashMap Object  
     HashMap<String,String> hashmap=new HashMap<String,String>();  
     //Store and Remove data from HashMap  
     hashmap.put("key1", "value1");  
     hashmap.put("key2", "value2");  
     hashmap.put("key3", "value3");  
     String key3=hashmap.remove("key3");  
     //Get Value from HashMap by using Key  
     System.out.println("-- Example for Get Value from HashMap --");  
     System.out.println("Get Hashmap value: "+hashmap.get("key1"));  
     System.out.println("Remove Hashmap value: "+key3);  
     System.out.println("Contains Key Hashmap: "+hashmap.containsKey("key1"));  
     System.out.println("Contains Key Hashmap: "+hashmap.containsKey("key3"));  
     System.out.println("Contains Value Hashmap: "+hashmap.containsValue("value1"));  
     System.out.println("Contains Value Hashmap: "+hashmap.containsValue("value3"));  
     //Loop data from HashMap by keySet and using key to get the Value  
     System.out.println("-- Example for Iterate list from Hash Map --");  
     Iterator<String> it=hashmap.keySet().iterator();  
     while(it.hasNext()){  
       String key=it.next();  
       String value=hashmap.get(key);  
       System.out.println("By Key :Key : "+key+"  Value: "+value);  
     }  
     //Loop data from HashMap by Value List all value no need to using Key  
     it=hashmap.values().iterator();  
     while(it.hasNext()){  
       System.out.println("Value "+it.next());  
     }             
   }  
   public static void main (String args[]){  
     //Test run program in main method  
     HashmapExample hashmap=new HashmapExample();  
     hashmap.hashmapExample();  
   }  
 }  
Output when Run Test Program

-- Example for Get Value from HashMap --
Get Hashmap value: value1
Remove Hashmap value: value3
Contains Key Hashmap: true
Contains Key Hashmap: false
Contains Value Hashmap: true
Contains Value Hashmap: false
-- Example for Iterate list from Hash Map --
By Key :Key : key2   Value: value2
By Key :Key : key1   Value: value1
Value value2
Value value1

Java Hashtable Example Code for using Hashtable store data and get data

You can use Hasetable to atore data with key and value for example
 key1= data1
 key2= data 2
 key3= data 3

you can use key collect data from Hashtable however you can collect list of key or value with keySet and valuesSet

Example Code to using java Hashtable
** you need to inport java.util package

 package example.util;  
 import java.util.*;  
 public class HashtableExample {  
   public void hashtableExample(){  
     //Create Hashtable Object  
     Hashtable<String,String> hashtable=new Hashtable<String,String>();  
     //Store and Remove data from Hashtable  
     hashtable.put("key1", "value1");  
     hashtable.put("key2", "value2");  
     hashtable.put("key3", "value3");  
     String key3=hashtable.remove("key3");  
     System.out.println("-- Example for Get Value from Hashtable --");  
     System.out.println("Get Hashtable value: "+hashtable.get("key1"));  
     System.out.println("Remove Hashtable value: "+key3);  
     System.out.println("Contains Key Hashtable: "+hashtable.containsKey("key1"));  
     System.out.println("Contains Key Hashtable: "+hashtable.containsKey("key3"));  
     System.out.println("Contains Value Hashtable: "+hashtable.containsValue("value1"));  
     System.out.println("Contains Value Hashtable: "+hashtable.containsValue("value3"));  
     System.out.println("-- Example for Iterate list from Hashtable --");  
     Iterator<String> it=hashtable.keySet().iterator();  
     while(it.hasNext()){  
       String key=it.next();  
       String value=hashtable.get(key);  
       System.out.println("By Key :Key : "+key+"  Value: "+value);  
     }  
     it=hashtable.values().iterator();  
     while(it.hasNext()){  
       System.out.println("Value "+it.next());  
     }  
   }  
   public static void main (String args[]){  
     HashtableExample hashtable=new HashtableExample();  
     hashtable.hashtableExample();  
   }  
 }  
Result when run test

-- Example for Get Value from Hashtable --
Get Hashtable value: value1
Remove Hashtable value: value3
Contains Key Hashtable: true
Contains Key Hashtable: false
Contains Value Hashtable: true
Contains Value Hashtable: false
-- Example for Iterate list from Hashtable --
By Key :Key : key2   Value: value2
By Key :Key : key1   Value: value1
Value value2
Value value1

Java synchronized block vs synchronized method

You can using synchronized  to lock multi Thread program with process thread by thread

Example code for using synchronized block and synchronized  method

synchronized  method

public synchronized void testMethod(String obj){
    // you method logic  
}

synchronized block
public void testMethod(String obj){
    // your method logic
    synchronized(this){
        // your method logic
        // your method logic
    }
    // your method logic
    // your method logic
    synchronized(Test.class){
        // your method logic
        // your method logic
    }
    // your method logic
}

Java enum valueof example code for valueof method in java enum

enum  is the common base class of all Java language enumeration types. including descriptions of the implicitly declared methods synthesized by the compiler
valueof method returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type

Method syntax

public static > T valueOf(Class enumType, String name)

Method input parameter

    enumType - the Class object of the enum type from which to return a constant
    name - the name of the constant to return

Method return

the enum constant of the specified enum type with the specified name

Example Code for java enum valueof

 enum Car {  
   City(800000), Civic(1200000),Jaz(850000);   
   int price;  
   Car(int p) {  
    price = p;  
   }  
   int showPrice() {  
    return price;  
   }  
 }  
 public class EnumTest {  
   public static void main(String args[]) {  
    System.out.println("My Car List:");  
    for(Car m : Car.values()) {  
     System.out.println(m + " Price " + m.showPrice());  
    }  
    Car city;  
    city = Car.valueOf("City");  
    System.out.println("Test Selected Car : " + city);                 
   }  
 }  


Runnint Result

My Car List:
City Price 800000
Civic Price 1200000
Jaz Price 850000
Test Selected Car : City

Java hashmap get value by key example code for java hashmap

we can call method get to Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Method syntax

public V get(Object key)

Method input parameter

    key - the key whose associated value is to be returned

Method return

the value to which the specified key is mapped, or null if this map contains no mapping for the key

Example Code for java enum valueof

        HashMap hm=new HashMap();
      hm.put("key1", "data1");
      hm.put("key2", "data2");
      String data=hm.get("key1");
      System.out.println(data);

File in Java how to Read and Write File in Java

You can use class in package java.io.* to manage file with java
for example below

Example code for read file line by line with Java

  FileInputStream fs = new FileInputStream("filename.txt"); 
  DataInputStream ds = new DataInputStream(fs);
  BufferedReader br = new BufferedReader(new InputStreamReader(ds));
  String tmp; 
  while ((tmp = br.readLine()) != null)   {
      System.out.println (tmp);
  } 
  ds.close();


Example code for Write file with java
  FileWriter fw = new FileWriter("file_name.txt");
  BufferedWriter out = new BufferedWriter(fw);
  out.write("Test Wrrite1");
  out.write("Test Wrrite2");
  out.write("Test Wrrite3");  
  out.close();


Java Class is based for Java OOP

Class is the blueprint to create Object  Class is learning the basics of OOP that everyone must know.

for example

Object of  Class Car is  My car .

Class structure

prefix eg.(public,protected) class ClassName {
  
}

public class MyClass{
 //implement some things
}

Java Date Format with SimpleDateFormat

Today I will present how to Format Date with java.text.* package
Class java.text.SimpleDateFormat  for example below

   Date date=new Date();
        System.out.println("No format :"+date);
        java.text.SimpleDateFormat df= new java.text.SimpleDateFormat();
        System.out.println("No pattern format :"+df.format(date));
        df.applyPattern("dd/mm/yyyy");
        System.out.println("dd/mm/yyyy format :"+df.format(date));
        df.applyPattern("dd/mm/yyyy HH:mm:ss");
        System.out.println("dd/mm/yyyy HH:mm:ss format :"+df.format(date));



Output for Java SimpleDateFormat

No format :Thu Apr 05 16:29:18 ICT 2012
No pattern format :5/4/2555, 16:29 น.
dd/mm/yyyy format :05/29/2555
dd/mm/yyyy HH:mm:ss format :05/29/2555 16:29:18