Showing posts with label Code. Show all posts
Showing posts with label Code. Show all posts

Friday, November 16, 2012

Java Script function to Convert your local Date to UTC format Date

There might be a case that you might need to save date on UTC format while the user input date might be regional date time format. The following function converts the date on the UI layers itself  from the input reginonal date to UTC Date.

function convertToUTCFormat(date){
var year = "" + date.getUTCFullYear();
var month = "" + (date.getUTCMonth() + 1);if (month.length == 1) { month = "0" + month; } //months range is [0-11]
var day = "" + date.getUTCDate(); if (day.length == 1) { day = "0" + day; }
var hour = "" + date.getUTCHours();if (hour.length == 1) { hour = "0" + hour; }
var minute = "" + date.getUTCMinutes(); if (minute.length == 1)  minute = "0" + minute; } var second = "" + date.getUTCSeconds(); if (second.length == 1) { second = "0" + second; }

return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second+":000"; }

Read More...

Friday, January 22, 2010

Converting Clob Datatype to String and ViceVersa .

If you have been using Hibernate 3.x.x or greater you can easily convert clob type to string type and vice versa, i.e String data type to Clob type.

Following piece of codes demonstrate the process of doing the conversion.


.........
.........

Clob textClobValue;
String textValue;
String newTextValue;
Clob newTextClobValue;
InputStream textStream;
Paragraph paragraph // my domain Class that contains a clob datatypes and used to update the database connection with hibernate connection (Hibernate version >=3.0)
...........
...........

textClobValue=paragraph.getText();
textStream = textClobValue.getAsciiStream(); //return inputstream
textValue=convertStreamToString(textStream);
newTextValue=textValue.replaceAll("Some text","new Text"); //Replacing some text from old text.

newTextClobValue=Hibernate.createClob(newTextValue);
//System.out.println("Updated="+newParaContentValue);
paragraph.setText(newTextClobValue);
..........
..........
..........



public static String convertStreamToString(InputStream is) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;

while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
return sb.toString();
}




Read More...

Tuesday, November 3, 2009

Converting Groovy Array type to Java Array Type

Following is the code snipped in groovy that demonstrate the conversion Groovy Array type to Java Array Type.
Code for Groovy

def param=[]
param+="paramater 1"
param+="paramater 2"
param+="paramater 3"
param+="paramater 4"
def stringArgForJava = (String[])params


Test.callJavaClass(stringArgForJava) //static method


The sample java class is as follow;

class Test
{
public static void callJavaClass(String[] arg)
{
for(int i=0;i {
System.out.println("params "+i ":" +params[i]);
}

}
}








Read More...

Tuesday, October 13, 2009

Read a variable defined on the config file from a controller in a grail application



Let's say, you have the config.groovy file as follow.


...........
..........

environments {
production {
myVariable1="I am variable1 on RPOD"
myVariable1="I am variable1 on RPOD"
}



development {
myVariable1="I am variable1 on DEV"
myVariable1="I am variable1 on RPOD"
}

log4j {
appender.stdout = "org.apache.log4j.ConsoleAppender"
.........
........

Now the above varaible can be easily access through any of the controller as follow.


def config = ConfigurationHolder.config
def myVaraible1 = config.myVaraible1
def myVaraible2 = config.myVaraible2



Read More...

Wednesday, July 8, 2009

Reading the status code of HTTP Request !

When you make an HTTP request to any server url throuh your client software, lets say IE, HTTP Status codes are returned by the server to the client software to determine the outcome of your request. There are five different classes of status code range. They are
Status codes 100-101
Status codes 200-206
Status codes 300-307
Status codes 400-417
Status codes 500-505

Following is a simple program written in groovy that checks and returns the status code of any url.

import java.net.URL;
def url
def message =""
boolean fail=true
def urlString="http://192.168.0.1/test/test.html"
println checkURL(urlString)

def checkURL(urlString){
int status_code
def tempmessage=""
URL URLserver = new URL("urlString");
try{
URLConnection connection = (HttpURLConnection)URLserver.openConnection();

status_code = connection.getResponseCode();


switch (status_code)
{
case 200:
tempmessage=" Connection Successful"
break;
case 401:
tempmessage="UnAuthorised"
break;
case 405 :
tempmessage= " Resource Not Found"
break;
case 408:
tempmessage="Request Time-out"
break;
case 500:
tempmessage="Internal Server Error"
break;
case 502:
tempmessage="Bad Gateway"
break;
case 503:
tempmessage="Service Temporarily Unavailable"
break;
default :
tempmessage= connection.getResponseMessage()
break;

}
}
catch (Exception exc){
tempmessage=exc

}

return "\n "+urlString+" :"+status_code+":"+tempmessage;

}




Visit http://www.w3.org/Protocols/HTTP/HTRESP.html for detail about the status codes.



Read More...

Pages

 ©mytechtoday.com 2006-2010

 ©Mytechtoday

TOP