custom 메소드 2

공부는 혼자하는 거·2021년 12월 8일
0

JAVA & Kotlin Tip

목록 보기
5/11
 public static String converToGUEBGOrGUEEN(String oldDate) {

        //  f_resident_date : 법정생년월일(LBIRTH) 값을 YYMMDD 형식으로 변경
        String strDate = null;

        SimpleDateFormat recvSimpleFormat = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);

        // 여기에 원하는 포맷을 넣어주면 된다
        SimpleDateFormat tranSimpleFormat = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);

        try {
            Date data = recvSimpleFormat.parse(oldDate);
            strDate = tranSimpleFormat.format(data);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        log.info("변환된 날짜" + strDate);

        return strDate;
    }




	public static String parseLocalDateTime() {

		String dummydate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

		return dummydate;
	}
    
    
    
    	public static Timestamp stringToDate(String from) {

		Timestamp timestamp = null;

		log.info("파싱 전 문자열: " + from);

		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yy.MM.dd-hh:mm:ss.SSS"); //형식을 맞춰줘야함ㄴ

			Date parsedDate = dateFormat.parse(from);

			timestamp = new java.sql.Timestamp(parsedDate.getTime());
		} catch (Exception e) { // this generic but you can control another types of exception
			// look the origin of excption
			log.info("Convert to Timestamp Error");
		}

		log.info("timeStamp 초 단위이하 표기 check: " + timestamp);
		return timestamp;
	}
    
    
    
    	public static String getNowTime(Integer i){ //1이면 startTime, 아니면 endTime
	    long now = System.currentTimeMillis();
	    Date mDate = new Date(now);
	    
	    Calendar cal = Calendar.getInstance();
        cal.setTime(mDate);
        
        cal.add(Calendar.MILLISECOND, 20);

	    SimpleDateFormat sdfNow = new SimpleDateFormat("yy.MM.dd-HH:mm:ss:SS");
	    
	    String Time;
	    
	    if(i==1) {
	    	
		    Time = sdfNow.format(mDate);

	    }else {
	    	 Time = sdfNow.format(cal.getTime());
	    }
	    
	    String nowTime =Time;
	    return nowTime;

	    }



	/**
	 * 현재 시간 반환
	 * 
	 * @return
	 */
	public String getCurrentTime() {

		SimpleDateFormat df = new SimpleDateFormat("hh:mm:ss");
		String now = df.format(new Date());

		return now;
	}

	/**
	 * 주기에 따라 날짜 반환
	 * 
	 * @param period
	 * @return
	 */
	public String getPastTime(int period) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(Calendar.DATE, period);

		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		String past = df.format(cal.getTime());

		return past;
	}

  public static String byteArrayToHexaString(byte[] bytes) {
        StringBuffer builder = new StringBuffer();
        for (byte data : bytes) {
            builder.append(String.format("%02X ", data));
        }
        return builder.toString();
    }



	public static String getFieldName(Method method)
	{
	    try
	    {
	        Class<?> clazz=method.getDeclaringClass();
	        BeanInfo info = Introspector.getBeanInfo(clazz);  
	        PropertyDescriptor[] props = info.getPropertyDescriptors();  
	        for (PropertyDescriptor pd : props) 
	        {  
	            if(method.equals(pd.getWriteMethod()) || method.equals(pd.getReadMethod()))
	            {
	                System.out.println(pd.getDisplayName());
	                return pd.getName();
	            }
	        }
	    }
	    catch (IntrospectionException e) 
	    {
	        e.printStackTrace();
	    }
	    catch (Exception e) 
	    {
	        e.printStackTrace();
	    }


	    return null;
	}
	
	
	
	public static String parseToBigDecimalList(List<String> values) {
		
	
		List<BigDecimal> bigDecimalList = values.stream()
		        .map(BigDecimal::new)
		        .collect(Collectors.toList());

//		System.out.println(bigDecimalList);
		
		
		String joinedString = StringUtils.join(bigDecimalList, "^");
		System.out.println(joinedString);
		
		return joinedString;
		
	}
	

	public static <T> List<String> getValueType(Parameter<T> parameter) { //리플렉션 활용

		List<String> filedNames = new ArrayList<>();
		
		try {
			Object obj = parameter;
			for (Field field : obj.getClass().getDeclaredFields()) {
				field.setAccessible(true);
				Object value = field.get(obj);
				System.out.println(field.getName() + ",   value: " + value);
				
				filedNames.add(field.getName());
						
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		
		return filedNames;
	}
 public static String parsingFilepath(String string) {
         if (string == null || string.length() == 0) {
             return "";
         }

         
         StringBuffer sb = new StringBuffer();
         char         c = 0;
         int          i;
         int          len = string.length();

         for (i = 0; i < len; i += 1) {
             c = string.charAt(i);
             switch (c) {
             case '\\':
            	 log.debug("...." + c);
                 sb.append("\\" +File.separatorChar);
                 break;
             default:
            	 sb.append(c);
             }
         }

         return sb.toString();
     }
     
     	public static List<String> generateRandomString() {

		int length = 10;
		boolean useLetters = true;
		boolean useNumbers = false;

		List<String> test = new ArrayList<>();

		for (int i = 0; i < 100; i++) {
			String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
			test.add(generatedString);
		}

		return test;
	}
     
profile
시간대비효율

0개의 댓글