package com.wxf.demo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;

public class Test {
	private static final HashMap<String, String> MONTH_MAP = new HashMap<String, String>();

	public static void main(String[] args) {
		inital();
		BufferedReader read = null;
		PrintWriter out = null;
		if(!checkParams(args)) return;
		
		long startTime = sysdate();
		System.out.println("====开始时间===="+startTime);
		try {
			//缓冲 默认大小 8192字符
			read = new BufferedReader(new InputStreamReader(new FileInputStream(args[0])),5*1024*1024);
			out = new PrintWriter(new BufferedWriter(new FileWriter(args[1])));
			String str;
			while((str=read.readLine())!=null){
				str = executeStr(str)+"\r\n";
				out.write(str);
			}
			long endTime = sysdate();
			System.out.println("====结束时间===="+endTime+"\nOK!!!=花费时间: "+(endTime - startTime)+" HaoMiao");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (read != null) {
				try {
					read.close();
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 *检验输入参数
	 * @param args
	 * @return
	 */
	public static boolean checkParams(String[] args){
		if(args == null || args.length == 0 || args.length < 2){
			System.out.println("请输入文件路径(输入和输出)...");
			return false;
		}
		if(args[0] == null || "".equals(args[0])){
			System.out.println("请输入需要处理的文件路径...");
			return false;
		}
		if(args[1] == null || "".equals(args[1])){
			System.out.println("请输入处理生成的文件路径...");
			return false;
		}
		return true;
	}
	
	/**
	 * 处理读取的数据
	 * @param str
	 * @return
	 */
	public static String executeStr(String str) {
		StringBuffer resBuff = new StringBuffer("");
		String[] arr = str.split("\\|");
		for (int i = 0; i < arr.length; i++) {
			String temp = arr[i];
			if (temp.indexOf("PM") != -1) {
				temp = temp.substring(0, temp.length() - 2);
				temp = parseDate(temp,"PM");
				resBuff.append("|" + temp);
			} else if(temp.indexOf("AM") != -1){
				temp = temp.substring(0, temp.length() - 2);
				temp = parseDate(temp,"AM");
				resBuff.append("|" + temp);
			}else {
				resBuff.append("|" + arr[i]);
			}
		}
		return resBuff.toString().substring(1);
	}
	
	/**
	 * 时间格式处理
	 * @param str
	 * @param dateFlag
	 * @return
	 */
	public static String parseDate(String str, String dateFlag) {
		str = str.replace("  ", " ");
		String[] arr = str.split(" ");
		String endTime = arr[3];
		if("PM".equalsIgnoreCase(dateFlag)){
			endTime = Integer.parseInt(endTime.substring(0, endTime.indexOf(":")))+12 + endTime.substring(endTime.indexOf(":"));
		}
		return arr[2] + "-" + MONTH_MAP.get(arr[0].toLowerCase()) + "-" + arr[1] + " " + endTime;
	}
	
	/**
	 * 月份
	 */
	public static void inital() {
		MONTH_MAP.put("jan", "01");
		MONTH_MAP.put("feb", "02");
		MONTH_MAP.put("mar", "03");
		MONTH_MAP.put("apr", "04");
		MONTH_MAP.put("may", "05");
		MONTH_MAP.put("jun", "06");
		MONTH_MAP.put("jul", "07");
		MONTH_MAP.put("aug", "08");
		MONTH_MAP.put("sep", "09");
		MONTH_MAP.put("oct", "10");
		MONTH_MAP.put("nov", "11");
		MONTH_MAP.put("dec", "12");
	}
	
	/**
	 * 获取时间
	 * @return
	 */
	public static long sysdate(){
		return new Date().getTime();
	}
}
最近下载更多
shuiqing_cjn  LV1 2023年4月7日
双鱼座程序员7号  LV6 2022年4月23日
huyi131479  LV16 2021年7月11日
hhhhjay  LV7 2021年5月13日
zuidaima_liuzg  LV1 2021年5月13日
李大大的图书  LV1 2021年3月18日
1473850331  LV1 2021年2月3日
Coincidance  LV8 2020年12月3日
zmy001  LV11 2020年12月2日
lljx19960329  LV1 2020年7月10日
最近浏览更多
xiaoyumao  LV2 8月29日
东北虎啸  LV3 2024年4月17日
微信网友_6248713511227392  LV11 2022年12月5日
liys1234  LV9 2022年4月27日
双鱼座程序员7号  LV6 2022年4月23日
charles1256  LV11 2021年12月20日
hechaojun  LV5 2021年11月4日
lfzy2016 2021年8月25日
暂无贡献等级
416039033  LV1 2021年7月29日
huyi131479  LV16 2021年7月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友