Monday 29 February 2016

Calling RestApi Web Services From Java Program

A JAVA Program to call RestApi Services :

package RestApiClientTest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class NetClientGet {
public static void main(String[] args) {
 try {
 URL url = new URL("http:/localhost:8983/rest/calculatesum"); // rest api url
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
 } catch (MalformedURLException e) {
e.printStackTrace();
 } catch (IOException e) {
e.printStackTrace();
 }
}

You need to include below jars in your class path:
1. jackson-databind
2. jackson-annotations
3. jackson-core

Maven details for the jars :
  <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.1.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.1.1</version>
</dependency>

Thursday 11 February 2016

Date Formatting in Java Example

Date Formatting in Java Example :
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatting {
public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        Date date=new Date();
        String str_date=df.format(date);
        System.out.println("dd-MM-yyyy: "+str_date);
        df.applyPattern("yyyy-MM-dd");
        System.out.println("yyyy-MM-dd: "+df.format(date));      
     
        System.out.println("*****************");        
        SimpleDateFormat df2 = new SimpleDateFormat("dd-MM-yyyy");
        String str="10-02-2016";
        //Date date=new Date();
        Date date1=null;
try {
date1 = df2.parse(str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
        df2.applyPattern("yyyy-MM-dd");
        System.out.println("yyyy-MM-dd: "+df2.format(date1));
}

}



String to Date :
private Date convertStringToDate(String inputString) throws ParseException
{
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date date = formatter.parse(inputString);

return date;
}

PGSQL date column :

@Temporal(TemporalType.DATE) @Column(name="patient_dob", nullable=false) private Date patientDob;