http://yoonbumtae.com/?p=2776

public static String plusZero(int num) {
int temp = num;
int length = (int) (Math.log10(temp) + 1);
String d = "";

if (length == 1) {
d = String.format("%02d", temp);
} else {
d = String.valueOf(temp);
}

return d;
}

/**
* GZip를 이용한 스트링 압축
* @author jksong
* @Param
* @since 2019-09-09 오전 8:30
**/
public static byte[] zipStringToBytes(String input) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(gzipOutputStream);
bufferedOutputStream.write(input.getBytes());
bufferedOutputStream.close();
byteArrayOutputStream.close();
return byteArrayOutputStream.toByteArray();
}

/**
* GZIPInputStream을 이용하여 byte배열 압축해제하기
* @author jksong
* @Param
* @since 2019-09-09 오전 8:30
**/
public String unzipStringFromBytes(byte[] bytes) throws IOException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream);
BufferedInputStream bufferedInputStream = new BufferedInputStream(gzipInputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

byte[] buffer = new byte[100];
int length;
while((length = bufferedInputStream.read(buffer)) > 0) {
byteArrayOutputStream.write(buffer, 0, length);
}

bufferedInputStream.close();
gzipInputStream.close();
byteArrayInputStream.close();
byteArrayOutputStream.close();

return byteArrayOutputStream.toString();
}


public static boolean
isIntegerNumeric(String s) {
try {
Integer.parseInt(s);
return true;
} catch(NumberFormatException e) {
return false;
}
}


public static boolean isDoubleNumeric(String s) {
try {
Double.parseDouble(s);
return true;
} catch(NumberFormatException e) {
return false;
}
}


public static boolean isFloatNumeric(String s) {
try {
Float.parseFloat(s);
return true;
} catch(NumberFormatException e) {
return false;
}
}


활용 방법


if (isIntegerNumeric(String) == false) {
logger.info("숫자만 입력 가능");
}


  public static String addDate(String fromDate, String format, int addYear, int addMonth, int addDate, int addHour, int addMinute, int addSecond) throws ParseException {

    SimpleDateFormat sdf = new SimpleDateFormat(format);

    Date date = sdf.parse(fromDate);

    Calendar cal = new GregorianCalendar();


    cal.setTime(date);

    cal.add(Calendar.YEAR, +addYear);

    cal.add(Calendar.MONTH, +addMonth);

    cal.add(Calendar.DATE, +addDate);

    cal.add(Calendar.HOUR, +addHour);

    cal.add(Calendar.MINUTE, +addMinute);

    cal.add(Calendar.SECOND, +addSecond);


    SimpleDateFormat sdf2 = new SimpleDateFormat(format);

    String toDate = sdf2.format(cal.getTime());


    return toDate;

  }


  1년 후

  String addDate(format, "yyyy-MM-dd HH:mm:ss", 1, 0, 0, 0, 0, 0);

  10분 후

  String addDate(format, "yyyy-MM-dd HH:mm:ss", 0, 0, 0, 0, 10, 0);

  public static String humanReadableByteCount(long bytes, boolean si) {

    int unit = si ? 1000 : 1024;

    

    if (bytes < unit) return bytes + " B";

    int exp = (int) (Math.log(bytes) / Math.log(unit));

    String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");

    return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);

  }

+ Recent posts