package com.bush.util;
import java.util.Date;
import java.text.*;
/**
* Useful Date utility methods not found in java.lang.Boolean
*
* @author Daniel Bush
*/
public class DateUtil {
private DateUtil() {}
/**
* Convert the specified {@link java.util.Date} into a {@link java.sql.Date}.
*
* @param date a Date value
* @return a java.sql.Date value
*/
public static java.sql.Date convert(Date date) {
return date != null ? new java.sql.Date(date.getTime()) : null;
}
/**
* Convert the specified {@link java.sql.Date} into a {@link java.util.Date}.
*
* @param timestamp a java.sql.Date value
* @return a Date value
*/
public static Date convert(java.sql.Date timestamp) {
return timestamp != null ? new Date(timestamp.getTime()) : null;
}
/**
* Convert the specified {@link java.util.Date} into a {@link java.sql.Timestamp}.
*
* @param date a Date value
* @return a java.sql.Timestamp value
*/
public static java.sql.Timestamp convertToTimestamp(Date date) {
return date != null ? new java.sql.Timestamp(date.getTime()) : null;
}
/**
* Convert the specified timestamp into a date.
*
* @param timestamp a java.sql.Timestamp value
* @return a Date value
*/
public static Date convertToDate(java.sql.Timestamp timestamp) {
return timestamp != null ? new Date(timestamp.getTime()) : null;
}
/**
* Give me a String and a pattern and I will try to give you the Date representation.
*
* @param date a String value
* @param pattern a String value
* @return a Date value
* @exception ParseException if an error occurs
*/
public static Date parse(String date, String pattern) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.parse(date);
}
/**
* Give me a Date and a pattern and I will try to give you the String representation.
*
* @param date a Date value
* @param pattern a String value
* @return a String value
* @exception ParseException if an error occurs
*/
public static String format(Date date, String pattern) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(date);
}
}
package com.bush.util;
import java.io.*;
import java.util.*;
import org.apache.log4j.*;
/**
* Useful IO utility methods.
*
* @author Daniel Bush
*/
public final class IOUtil {
private static final Logger LOG = Logger.getLogger(IOUtil.class);
private static final IOUtil _instance = new IOUtil();
private IOUtil() {}
/**
* Loads the specified resource from the CLASSPATH. *** Note: This
* class is designed as a Singleton and is expected to be, as per
* the packaging, loaded by a direct descendant of the system's
* class loader; preferably the application's class loader. This
* should insure that all resources requested to be loaded by an
* application class will be visible ... Caller is
* responsible for closing the InputStream!
*/
public static final InputStream loadResourceFromClasspath(String name) throws IOException {
LOG.debug("Attempting to load [{}] from the CLASSPATH.", name);
InputStream in = _instance.getClass().getResourceAsStream("/"+name);
if ( in == null ) {
LOG.warn("*** Failed to load resource. ***");
throw new IOException("["+name+"] could not be loaded from the CLASSPATH.");
}
else {
LOG.debug("Successfully loaded resource.");
return in;
}
}
/**
* And if you need the resource as a byte [] ...
*/
public static final byte [] loadResourceFromClasspathAsByteArray(String name) throws IOException {
InputStream in = loadResourceFromClasspath(name);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte [] buffer = new byte [2048];
int bytes = -1;
while ( (bytes = in.read(buffer)) != -1) {
out.write(buffer, 0, bytes);
}
out.close();
in.close();
return out.toByteArray();
}
public static final byte [] serialize(Object obj) throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bout);
oos.writeObject(obj);
oos.close();
return bout.toByteArray();
}
public static final Object deserialize(byte [] bytes) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
Object obj = ois.readObject();
ois.close();
return obj;
}
}