Web Development Tools Class

Source: Internet
Author: User

1. Date Tool class

Import Java.text.SimpleDateFormat;

Import Java.util.Date;

public class Dateutil {

public static String formatdate (Date date,string format) {
String result= "";
SimpleDateFormat sdf=new SimpleDateFormat (format);
if (date!=null) {
Result=sdf.format (date);
}
return result;
}


public static Date formatString (String str,string format) throws exception{
SimpleDateFormat sdf=new SimpleDateFormat (format);
return Sdf.parse (str);
}
}

2. Result set Jsonarray Tool class

Import Java.sql.ResultSet;

Import Java.sql.ResultSetMetaData;
Import Java.util.Date;

Import Net.sf.json.JSONArray;
Import Net.sf.json.JSONObject;

public class Jsonutil {

public static Jsonarray Formatrstojsonarray (ResultSet rs) throws exception{
ResultSetMetaData Md=rs.getmetadata ();
int Num=md.getcolumncount ();
Jsonarray array=new Jsonarray ();
while (Rs.next ()) {
Jsonobject mapofcolvalues=new jsonobject ();
for (int i=1;i<=num;i++) {
Object O=rs.getobject (i);
if (o instanceof Date) {
Mapofcolvalues.put (Md.getcolumnname (i), Dateutil.formatdate ((Date) O, "yyyy-mm-dd"));
}else{
Mapofcolvalues.put (Md.getcolumnname (i), rs.getobject (i));
}
}
Array.add (mapofcolvalues);
}
return array;
}
}

3. Output JSON string tool class

Import Java.io.PrintWriter;

Import Javax.servlet.http.HttpServletResponse;

Import Net.sf.json.JSONObject;

public class Responseutil {

public static void Write (HttpServletResponse response,object o) throws exception{
Response.setcontenttype ("Text/html;charset=utf-8");
PrintWriter Out=response.getwriter ();
Out.println (O.tostring ());
Out.flush ();
Out.close ();
}
}

4. Determine the string null or non-empty tool class

public class Stringutil {

public static Boolean isEmpty (String str) {
if ("". Equals (str) | | Str==null) {
return true;
}else{
return false;
}
}

public static Boolean isnotempty (String str) {
if (! "". Equals (str) &&str!=null) {
return true;
}else{
return false;
}
}
}

Web Development Tools Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.