Skip to content

Instantly share code, notes, and snippets.

@prietopa
Last active December 17, 2015 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prietopa/5677783 to your computer and use it in GitHub Desktop.
Save prietopa/5677783 to your computer and use it in GitHub Desktop.
Java utils
public class Utils{
public boolean isStrBlank(String str) {
if (str == null) {
return true;
} else if (str.equals("")) {
return true;
} else if (str.trim().isEmpty()) {
return true;
} else {
return false;
}
}
public static int safeLongToInt(long l) {
if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
LOGGER.warn("Cannot be cast to int without changing its value.");
throw new IllegalArgumentException(l
+ " Cannot be cast to int without changing its value.");
}
return (int) l;
}
public static XMLGregorianCalendar dateToXMLGregorianCalendar(Date date) {
if (date == null) {
return null;
}
Calendar cal = new GregorianCalendar();
cal.setTime(date);
try {
XMLGregorianCalendar tmStamp = DatatypeFactory.newInstance().newXMLGregorianCalendar(
DatatypeConverter.printDate(cal));
// se establace el tiempo de la fecha ya que no se porque
// con las lineas anteriores no se refleja y es necesario.
tmStamp.setHour(00);
tmStamp.setMinute(00);
tmStamp.setSecond(00);
return tmStamp;
} catch (DatatypeConfigurationException e) {
LOGGER.warn("Error to convert Date to XMLGregorianCalendar." + e);
return null;
}
}
public static Date xmlGregorianCalendarToDate(XMLGregorianCalendar calendar){
if(calendar == null) {
return null;
}
return calendar.toGregorianCalendar().getTime();
}
public static String decodeB64(String pp) {
byte[] xmlSignedDecoded = org.apache.commons.codec.binary.Base64.decodeBase64(pp);
return byteArrayToString(xmlSignedDecoded, ConstFacturae.ENCODING_UTF8);
}
public static String encodeB64(String pp) {
byte[] binaryData = pp.getBytes();
byte[] encodedBinaryData = org.apache.commons.codec.binary.Base64.encodeBase64(binaryData);
return byteArrayToString(encodedBinaryData, ConstFacturae.ENCODING_UTF8);
}
private static String byteArrayToString(byte[] byteArray, String encoding) {
if(byteArray == null || (byteArray != null && byteArray.length == 0)){
return null;
}
try {
return new String(byteArray, encoding);
} catch (UnsupportedEncodingException e) {
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment