package chapter04;
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Random; import java.util.UUID;
public class Util { public static void main(String[] args) throws Exception {
System.out.println(StringUtil.isEmpty(null)); System.out.println(StringUtil.isEmpty("")); System.out.println(StringUtil.isEmpty(" ")); System.out.println(StringUtil.isEmpty("abc"));
System.out.println(StringUtil.makeString()); System.out.println(StringUtil.makeString("abcdedf123456", 6));
System.out.println(StringUtil.formatDate(new Date(), "yyyy-MM-dd")); System.out.println(StringUtil.parseDate("2020-06-14", "yyyy-MM-dd")); } }
class StringUtil { public static boolean isEmpty(String str) {
if ( str == null || "".equals(str.trim()) ) { return true; } return false;
} public static boolean isNotEmpty(String str) { return !isEmpty(str); }
public static String makeString() { return UUID.randomUUID().toString(); }
public static String makeString(String from, int len) { if ( len < 1 ) { return ""; } else { char[] chars = from.toCharArray(); StringBuilder str = new StringBuilder(); for ( int i = 0; i < len; i++ ) { Random random = new Random(); int j = random.nextInt(chars.length); char c = chars[j]; str.append(c); } return str.toString(); } }
public static String transform( String source, String encodeFrom, String encodeTo ) throws Exception { byte[] bytes = source.getBytes(encodeFrom); return new String(bytes, encodeTo); }
public static Date parseDate( String dateString, String format ) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.parse(dateString); }
public static String formatDate( Date date, String format ) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(date); } }
|