一段业务相关的解析代码

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class TestPid {

	public static void main(String[] args) {
		StringBuilder str = new StringBuilder();
		str.append("252063002-41604,2014/04/08 00:00:00\\|");
		str.append("252063003-41609,2014/04/09 00:00:00\\|");
		str.append("252063004-41614,2014/04/10 00:00:00\\|");
		str.append("251941028-41491,2014/04/04 00:00:00\\|");
		str.append("251830039-41486,2014/04/05 00:00:00\\|");
		str.append("252072008-41599,2014/04/07 00:00:00\\|");
		str.append("251830040-41481,2014/04/06 00:00:00\\|");
		System.out.println(str.toString());
		//StringUtil
		String[] strarray = str.toString().split("\\|");
		System.out.println(strarray.length);
		if (strarray.length != 7) {
			System.out.println("长度不为7,拒绝入库");
		} else {
			Map<String, String> promMap = new HashMap<String, String>();
			for (int i = 0; i < strarray.length; i++) {
				String field = strarray[i];

				String str_time = getSplitDay(field);
				String str_key = getFormatDay(str_time);
				String str_value = getSplitPromId(field);

				// System.out.println(field);
				System.out.println("str_time:" + str_time);
				System.out.println("str_key:" + str_key);
				System.out.println("str_value:" + str_value);
				promMap.put(str_key, str_value);
				// promMap.put(key, value);
				System.out.println("week:" + getDayofweek(str_key));
			}

			Iterator iter = promMap.entrySet().iterator();
			while (iter.hasNext()) {
				Map.Entry entry = (Map.Entry) iter.next();
				Object key = entry.getKey();
				Object val = entry.getValue();
				System.out.println("key:" + key);
				System.out.println("val:" + val);
				
				long differ = dateDiffer(key.toString());
				System.out.println("dateDiffer:"+differ);
				if(differ>=0&&differ<=6) {
					System.out.println("执行入库操作");
				} else {
					System.out.println("不执行入库操作");
				}
			}

			System.out.println("hashmap.size=" + promMap.size());

		}

	}

	/*
	 * public static void main(String[] args) { String
	 * str="252063002-41604,2014/04/08 00:00:00\\|";
	 * 
	 * String str_time=getSplitDay(str); String str_key=getFormatDay(str_time);
	 * System.out.println("str_time:"+str_time);
	 * System.out.println("str_key:"+str_key);
	 * 
	 * String str_value=getSplitPromId(str);
	 * System.out.println("str_value:"+str_value); }
	 */

	private static String getSplitDay(String field) {
		int startIndex = field.indexOf(",") + 1;
		int endIndex = field.indexOf("\\");
		return field.substring(startIndex, endIndex);
	}

	/***
	 * 获取PromId
	 * 
	 * @param field
	 * @return
	 */
	private static String getSplitPromId(String field) {
		int startIndex = 0;
		int endIndex = field.indexOf("-");
		return field.substring(startIndex, endIndex);
	}

	/***
	 * 获得日期键值Key
	 * 
	 * @param field
	 * @return
	 */
	private static String getFormatDay(String field) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		Date date = null;
		try {
			date = sdf.parse(field);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sdf.format(date);
	}

	/***
	 * 周一到周日对应的时间分别是1,2,3,4,5,6,0
	 * 
	 * @param date
	 * @return
	 */
	public static int getDayofweek(String date) {
		Calendar cal = Calendar.getInstance();
		// cal.setTime(new Date(System.currentTimeMillis()));
		if (date.equals("")) {
			cal.setTime(new Date(System.currentTimeMillis()));
		} else {
			cal.setTime(new Date(getDateByStr2(date).getTime()));
		}
		return cal.get(Calendar.DAY_OF_WEEK) - 1;
	}

	public static Date getDateByStr2(String dd) {

		SimpleDateFormat sd = new SimpleDateFormat("yyyy/MM/dd");
		Date date;
		try {
			date = sd.parse(dd);
		} catch (ParseException e) {
			date = null;
			e.printStackTrace();
		}
		return date;
	}

	public static long dateDiffer(String key) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
		Date d1 = null;

		long diffDays = 0;
		try {
			d1 = format.parse(key);

			// 毫秒ms
			long diff = d1.getTime()-new Date(System.currentTimeMillis()).getTime();

			diffDays = diff / (24 * 60 * 60 * 1000);

			System.out.print("两个时间相差:");
			System.out.print(diffDays + " 天, ");
			
			

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return diffDays;
	}
}

你可能感兴趣的:(一段业务相关的解析代码)