Friday, April 1, 2016

Java Date & Time Examples : How To Convert A String Into Date

Intro


This examples will demonstrates how to convert a string into date object. It is very quite simple, you can use SimpleDateFormat.parse() method to parse a string format into date object.

Examples


package com.freesamplecode.java.datetime;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ConvertStringToDateDemo {
 public static void main(String[] args){
  String dateString1 = "8-Jan-2016";
  String dateString2 = "08/01/2016";
  String dateString3 = "Jan 8, 2016";
  
  SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MMM-yyyy");
  SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MM/yyyy");
  SimpleDateFormat sdf3 = new SimpleDateFormat("MMM dd, yyyy");
  
  try {
   Date date1 = sdf1.parse(dateString1);
   System.out.println(date1);
   
   Date date2 = sdf2.parse(dateString2);
   System.out.println(date2);
   
   Date date3 = sdf3.parse(dateString3);
   System.out.println(date3);
   
   
  } catch (ParseException e) {
   e.printStackTrace();
  }
  
 }
}

Output


Fri Jan 08 00:00:00 ICT 2016
Fri Jan 08 00:00:00 ICT 2016
Fri Jan 08 00:00:00 ICT 2016

Screenshot


How To Convert A String Into Date In Java





Wednesday, March 30, 2016

Java Date & Time Examples : How To Convert Date To String

Intro


This examples will demonstrates how to convert a date object into string format. It is very quite simple, you can use SimpleDateFormat.format() method to format a date object into specify format.

Examples


package com.freesamplecode.java.datetime;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class ConvertDateToStringDemo {
 public static void main(String[] args){
  
  //get today
  Date today = Calendar.getInstance().getTime();
  
  //create a simple date format object to specify string format
  SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh.mm.ss");
  
  //display today in string format
  System.out.println("Today is : "+sdf.format(today));
 }
}

Output


Today is : 31/03/2016 05.58.21

Screenshot

How To Convert Date To String In Java


Java Examples : How To Get Home Directory

Intro


This examples will demonstrates how to display an user home directory. It is quite simple, we can use System.getProperty() method with "user.home" parameter.

Examples


package com.freesamplecode.java.basic;

public class GetUserHomeDirectoryDemo {
 public static void main(String[] args){
  String homeDirectory = System.getProperty("user.home");
  
  System.out.println("Home Directory : "+homeDirectory);
 }
}


Output


Home Directory : C:\Users\Dev

Screenshot

How To Get Home Directory In Java

Monday, March 28, 2016

Java Examples : How To Display All Environment Variables

Intro


This examples will demonstrates how to display all environment variables using Java program. To display it, you can use System.getEnv() method that will return a collections of environment variables in your system.


Examples


package com.freesamplecode.java.basic;

import java.util.Map;
import java.util.Set;

public class DisplayEnvVariableDemo {
 public static void main(String[] args){
  Map envVar = System.getenv();
  
  Set keySet = envVar.keySet();
  
  for(String key : keySet){
   String value = (String) envVar.get(key);
   
   System.out.println("[ " + key + " ] : "+value);
  }
 }
}

Output


[ USERPROFILE ] : C:\Users\Dev
[ ProgramData ] : C:\ProgramData
[ PATHEXT ] : .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.RB;.RBW;.PY
[ windows_tracing_logfile ] : C:\BVTBin\Tests\installpackage\csilogfile.log
[ ORACLE_HOME ] : D:\app\Dev\11g
[ JAVA_HOME ] : C:\Program Files (x86)\Java\jdk1.7.0_03
[ TFS_DIR ] : C:\Program Files\ThinkVantage Fingerprint Software\
[ ProgramFiles(x86) ] : C:\Program Files (x86)
[ windows_tracing_flags ] : 3
[ TEMP ] : C:\Users\Dev\AppData\Local\Temp
[ SystemDrive ] : C:
[ ProgramFiles ] : C:\Program Files
[ Path ] : C:\Perl64\site\bin;C:\Perl64\bin;C:\Ruby22-x64\bin;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;D:\app\Dev\11g\bin;C:\oraclexe\app\oracle\product\10.2.0\server\bin;C:\Program Files\Broadcom\Broadcom 802.11 Network Adapter\Driver;;;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;;;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Lenovo\Access Connections\;C:\Program Files (x86)\Lenovo\Password Manager\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;D:\Installer\eclipse-jee-indigo-SR2-win32\eclipse\plugins\org.apache.ant_1.8.2.v20120109-1030\bin;C:\Program Files (x86)\Java\jdk1.6.0\bin;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files\TortoiseHg\;C:\Program Files\nodejs\;C:\Users\Dev\AppData\Roaming\npm
[ HOMEDRIVE ] : C:
[ PROCESSOR_REVISION ] : 2a07
[ USERDOMAIN ] : MOBILE214
[ ALLUSERSPROFILE ] : C:\ProgramData
[ ProgramW6432 ] : C:\Program Files
[ PROCESSOR_IDENTIFIER ] : Intel64 Family 6 Model 42 Stepping 7, GenuineIntel
[ SESSIONNAME ] : Console
[ TMP ] : C:\Users\Dev\AppData\Local\Temp
[ pythonpath ] : C:\Python34;C:\Python34\Scripts;
[ VISUALSVN_SERVER ] : C:\Program Files (x86)\VisualSVN Server\
[ CommonProgramFiles ] : C:\Program Files\Common Files
[ ACPath ] : C:\Program Files (x86)\Lenovo\Access Connections\
[ =:: ] : ::\
[ LOGONSERVER ] : \\MOBILE214
[ PROCESSOR_ARCHITECTURE ] : AMD64
[ FP_NO_HOST_CHECK ] : NO
[ OS ] : Windows_NT
[ TVT ] : C:\Program Files (x86)\Lenovo
[ HOMEPATH ] : \Users\Dev
[ PROCESSOR_LEVEL ] : 6
[ CommonProgramW6432 ] : C:\Program Files\Common Files
[ ANT_HOME ] : D:\Installer\eclipse-jee-indigo-SR2-win32\eclipse\plugins\org.apache.ant_1.8.2.v20120109-1030
[ LOCALAPPDATA ] : C:\Users\Dev\AppData\Local
[ COMPUTERNAME ] : MOBILE214
[ windir ] : C:\Windows
[ SystemRoot ] : C:\Windows
[ NUMBER_OF_PROCESSORS ] : 4
[ USERNAME ] : Dev
[ PUBLIC ] : C:\Users\Public
[ PSModulePath ] : C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
[ CommonProgramFiles(x86) ] : C:\Program Files (x86)\Common Files
[ ComSpec ] : C:\Windows\system32\cmd.exe
[ APPDATA ] : C:\Users\Dev\AppData\Roaming


Screenshot

How To Display All Environment Variables In Java


Sunday, March 27, 2016

Java Examples : How To Use Final Variable

Intro


This examples, will demonstrates how to use final variable in Java program. Final variable is used to declares a constants in Java program. Once you created, a final variable cannot change this value.

Examples


package com.freesamplecode.java.basic;

public class FinalVariableDemo {
 public static void main(String[] args){
  final int HOUR_OF_DAY = 24;
  
  System.out.println("Hours in one day are : "+HOUR_OF_DAY);
 }
}

Output


Hours in one day are : 24

Screenshot

How To Use Final Variable

Saturday, March 19, 2016

Java Loop Examples : How To Use Enhanced For Loop

Intro


This examples, will demonstrates how to use Enhanced For Loop.

Examples


package com.freesamplecode.java.basic;

import java.util.ArrayList;
import java.util.List;

public class EnhancedForLoopDemo {
    public static void main(String[] args){

        String[] fruitsArray = new String[]{"Apple","Banana","Cherry","Pinneapple","Strawberry"};
        //enhanced for loop an array of string
        System.out.println("My favorite fruits are : ");

        for(String fruit : fruitsArray){
           System.out.println(fruit);
        }

       System.out.println("\n");

       List<String> listOfEmployee = new ArrayList<String>();
       listOfEmployee.add("Nursalim");
       listOfEmployee.add("Nurul Hikmah");
       listOfEmployee.add("Iskiyati");

       //enhanced for loop a list
       System.out.println("List of employees : ");

       for(String employee : listOfEmployee){
          System.out.println(employee);

       }
   }
}

Output


My favorite fruits are : 
Apple
Banana
Cherry
Pinneapple
Strawberry


List of employees : 
Nursalim
Nurul Hikmah
Iskiyati

Screenshot

Enhanced For Loop Java

Friday, March 18, 2016

Java Break Examples : How To Use A Break Statement In For Loop

Intro


This examples, will demonstrates how to use a break statement in for loop statement. Break statement used to exit from looping state.

Examples


package com.freesamplecode.java.basic;

public class ForLoopBreakDemo {
 public static void main(String[] args){
  int[] anInt = {1, 2, 3, 4, 5};
  
  for(int i = 0; i < anInt.length; i++ ){
   if(i == 3){
    break;
   }else{
    System.out.println(anInt[i]);
   }
  }
 }
}


Output


1
2
3

Screenshot

How To Use A Break Statement In For Loop Using Java


Java Thread Example : How To Create A New Thread

Intro


This examples will demonstrates how to create a new thread. To create a thread, your java class must implements runnable interface.

Examples


package com.freesamplecode.java.thread;

public class CreateNewThreadDemo implements Runnable {
 public static void main(String[] args){
  
  //create new thread with names "Thread Demo";
  Thread t = new Thread(new CreateNewThreadDemo(), "Thread Demo");
  
  //start a thread
  t.start();
  
  for(int i = 0; i < 5; i++){
   System.out.println("Main thread : "+i);
   
   try {
    Thread.sleep(100);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  
  System.out.println("Main thread finished");
 }

 @Override
 public void run() {
  for(int i = 0; i < 5; i++){
   System.out.println("Child thread : "+i);
   
   try{
    Thread.sleep(50);
   }catch(InterruptedException ex){
    ex.printStackTrace();
   }
  }
  
  System.out.println("Child thread finished ");
  
 }
}

Output


Main thread : 0
Child thread : 0
Child thread : 1
Main thread : 1
Child thread : 2
Child thread : 3
Main thread : 2
Child thread : 4
Child thread finished 
Main thread : 3
Main thread : 4
Main thread finished


Screenshot

How To Create A New Thread In Java

Java Examples : How To Display Hello World

Intro


This examples, will demonstrates how to display "Hello World" string in monitor screen.

Examples


package com.freesamplecode.java.basic;

public class DisplayHelloWorldDemo {
 public static void main(String[] args){
  System.out.println("Hello World");
 }
}

Output


Hello World

Screenshot


How To Display Hello World In Java Program