java从入门到放弃

好记性不如烂笔头

自己写的工具类,获取日期,读取properties,xml,io流

一些自己平时写的工具类,不是很完美,但是可以节约一些平时的代码量。

1.获取年,月,日,当前时间,当月第一天最后一天等等。。。

package kang.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 * @author zhaoyao 2017-9-26
 *
 */
public class CalendarUtil {

    // 当前时间
    public static String getCurrentTimeDate()
            throws ParseException {
        
        Calendar c = Calendar.getInstance();
        Date date= c.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String time = format.format(date);
        return time;
    }
    
    // 当月第一天
    public static String getCurrentMonthFirstDate(String time)
            throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse(time);

        Calendar currentFirstCalender = Calendar.getInstance();
        currentFirstCalender.setTime(date);
        currentFirstCalender.add(Calendar.MONTH, 0);
        currentFirstCalender.set(Calendar.DAY_OF_MONTH, 1);
        String first = format.format(currentFirstCalender.getTime());
        return first;
    }

    // 当月最后一天
    public static String getCurrentMonthLastDate(String time)
            throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse(time);

        Calendar currentLastCalender = Calendar.getInstance();
        currentLastCalender.setTime(date);
        currentLastCalender.set(Calendar.DAY_OF_MONTH,
                currentLastCalender.getActualMaximum(Calendar.DAY_OF_MONTH));
        String last = format.format(currentLastCalender.getTime());
        return last;
    }

    // 上月第一天
    public static String getPreMonthFirstDate(String time)
            throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse(time);

        Calendar preFirstCalender = Calendar.getInstance();
        preFirstCalender.setTime(date);
        preFirstCalender.add(Calendar.MONTH, -1);
        preFirstCalender.set(Calendar.DAY_OF_MONTH, 1);
        String first = format.format(preFirstCalender.getTime());
        return first;
    }

    // 上月最后一天
    public static String getPreMonthLastDate(String time) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse(time);

        Calendar preLastCalender = Calendar.getInstance();
        preLastCalender.setTime(date);
        int month = preLastCalender.get(Calendar.MONTH);
        preLastCalender.set(Calendar.MONTH, month - 1);
        preLastCalender.set(Calendar.DAY_OF_MONTH,
                preLastCalender.getActualMaximum(Calendar.DAY_OF_MONTH));
        String last = format.format(preLastCalender.getTime());

        return last;
    }

    public static String getFirstDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cl = Calendar.getInstance();
        // cl.setTime(dateNow);
        // cl.add(Calendar.DAY_OF_YEAR, -1);
        // cl.add(Calendar.WEEK_OF_YEAR, -1);
        cl.add(Calendar.MONTH, -1);
        Date dateFrom = cl.getTime();
        return sdf.format(dateFrom);
    }
    
	public static String getLastOfWeek(String now){
		//
		Calendar c=Calendar.getInstance();
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		//得到本周周日的时间
		try {
			c.setTime(format.parse(now));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long l=c.getTimeInMillis();
		//System.out.println(l);
		int day =c.get(Calendar.DAY_OF_WEEK)-1;
		
		if(day==0) day=7;
		int i = 7-day;
	     long t = (l+i*3600*24*1000);
	
		return format.format(new Date(t));
	}
	/**
	 * @author yeruizu at 10-28-17 作用是给定日期,寻找一周周一
	 * @param now 给定的时间
	 * @return 字符串类型的
	 */
	public static String getFirstOfWeek(String now) {
		
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		try {
			c.setTime(format.parse(now));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long l = c.getTimeInMillis();
		
		String date =now.substring(now.lastIndexOf("-")+1);

		int i = 0;
		if (c.get(Calendar.DAY_OF_WEEK)==1) {
		i=6;	
		}else {
			
			i = c.get(Calendar.DAY_OF_WEEK)-1-1;
		}
	    long t = (l-i*3600*24*1000);
		
	/*	int i =  -(c.get(Calendar.DAY_OF_WEEK)-1)+7;
	     long t = (l+i*3600*24*1000);*/
	     return format.format(new Date(t));
	}


}
package kang.date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	/**
	 * @ps string转为date类
	 * @param dateString 日期字符串
	 * @param format 日期格式字符串
	 * @return
	 */
	public static Date stringTransToDate(String dateString,String format){
		
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = null;
		try {
			 date = sdf.parse(dateString);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			System.err.println("日期转换错误");
			e.printStackTrace();
			
		}
		
		return date;
		
	}
	
	/**
	 * @ps date转为string类
	 * @param date 要转化的日期对象
	 * @param format 日期格式字符串
	 * @return
	 */
	public static String dateTransToString(Date date,String formatString){
		
		SimpleDateFormat sdf = new SimpleDateFormat(formatString);
		String str = sdf.format(date);
		return str;
	}
	
	/**
	 * @ps 获取年-月-日字符串
	 * @return
	 */
	public static String getdateToDay() {
		
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH)+1;
		int day = c.get(Calendar.DAY_OF_MONTH);
		return year+"-"+month+"-"+day;
	}
	

	/**
	 * @ps 获取年-月字符串
	 * @return
	 */
	public static String getdateToMonth() {
		
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH)+1;
		//int day = c.get(Calendar.DAY_OF_MONTH);
		return year+"-"+month;
	}	
	
	/**
	 * @ps 获取年-月-日-小时字符串
	 * @return
	 */
	public static String getdateToHour() {
		
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH)+1;
		int day = c.get(Calendar.DAY_OF_MONTH);
		int hour = c.get(Calendar.HOUR_OF_DAY);
		return year+"-"+month+"-"+day+"-"+hour;
	}	
	
	
	/**
	 * @ps 获取年-月-日-小时-分钟字符串
	 * @return
	 */
	public static String getdateToMinute() {
		
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH)+1;
		int day = c.get(Calendar.DAY_OF_MONTH);
		int hour = c.get(Calendar.HOUR_OF_DAY);
		int minute = c.get(Calendar.MINUTE);
		return year+"-"+month+"-"+day+"-"+hour+"-"+minute;
	}	
	
	
	/**
	 * @ps 获取年-月-日-小时-分钟-秒字符串
	 * @return
	 */
	public static String getdateToSecond() {
		
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH)+1;
		int day = c.get(Calendar.DAY_OF_MONTH);
		int hour = c.get(Calendar.HOUR_OF_DAY);
		int minute = c.get(Calendar.MINUTE);
		int second = c.get(Calendar.SECOND);
		return year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
	}
	
	
	public static String getSecond() {
		
		Calendar c = Calendar.getInstance();
//		int year = c.get(Calendar.YEAR);
//		int month = c.get(Calendar.MONTH)+1;
//		int day = c.get(Calendar.DAY_OF_MONTH);
//		int hour = c.get(Calendar.HOUR_OF_DAY);
//		int nimute = c.get(Calendar.MINUTE);
		int second = c.get(Calendar.SECOND);
		return ""+second;
	}
	
	
	public static String getMinute() {
		
		Calendar c = Calendar.getInstance();
//		int year = c.get(Calendar.YEAR);
//		int month = c.get(Calendar.MONTH)+1;
//		int day = c.get(Calendar.DAY_OF_MONTH);
//		int hour = c.get(Calendar.HOUR_OF_DAY);
		int minute = c.get(Calendar.MINUTE);
		//int second = c.get(Calendar.SECOND);
		return ""+minute;
	}	
	
	
	public static String getHour() {
		
		Calendar c = Calendar.getInstance();
//		int year = c.get(Calendar.YEAR);
//		int month = c.get(Calendar.MONTH)+1;
//		int day = c.get(Calendar.DAY_OF_MONTH);
		int hour = c.get(Calendar.HOUR_OF_DAY);
		//int minute = c.get(Calendar.MINUTE);
		//int second = c.get(Calendar.SECOND);
		return ""+hour;
	}	
	
	
	public static String getDay() {
		
		Calendar c = Calendar.getInstance();
//		int year = c.get(Calendar.YEAR);
//		int month = c.get(Calendar.MONTH)+1;
		int day = c.get(Calendar.DAY_OF_MONTH);
		//int hour = c.get(Calendar.HOUR_OF_DAY);
		//int minute = c.get(Calendar.MINUTE);
		//int second = c.get(Calendar.SECOND);
		return ""+day;
	}	
	
	public static String getMonth() {
		
		Calendar c = Calendar.getInstance();
//		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH)+1;
		//int day = c.get(Calendar.DAY_OF_MONTH);
		//int hour = c.get(Calendar.HOUR_OF_DAY);
		//int minute = c.get(Calendar.MINUTE);
		//int second = c.get(Calendar.SECOND);
		return ""+month;
	}	
	
	
	public static String getYear() {
		
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		//int month = c.get(Calendar.MONTH)+1;
		//int day = c.get(Calendar.DAY_OF_MONTH);
		//int hour = c.get(Calendar.HOUR_OF_DAY);
		//int minute = c.get(Calendar.MINUTE);
		//int second = c.get(Calendar.SECOND);
		return ""+year;
	}		
	
}

2.io流输入输出

package kang.io;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class OutputUtil {

	public static void main(String[] args) {
		//getenv获取当前系统的temp文件路径,并在后面拼接新的路径
		//String address=System.getenv("TEMP"));
		
	}
	
	
	/***
	 * 
	 * @ps 将string写到指定的文件中
	 * @param path 文件路径(/)
	 * @param docName 文件全名
	 * @param text 要写的内容
	 * @param append 写入时是否拼接。true拼接,false覆盖
	 * @return 是否成功
	 */
	public static boolean OutputAction(String path,String docName,String text,boolean append) {
		
		
		File file = new File(path);
		File file2 = new File(path+"/"+docName);
		//判断文件夹是否存在,不存在就创建
		if (!file.exists()) {
			file.mkdirs();
		}
		//判断文件是否存在,不存在就创建
		if (!file2.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		try {
			//true代表写入时是否拼接
			FileWriter writer = new FileWriter(file2,append);
			
			writer.write(text);
			writer.flush();
			writer.close();
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return false;
	}
	
	
}

package kang.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class InputUtil {

	public static void main(String[] args) {

	}

	/***
	 * 
	 * @ps 将一个文件的内容读取到内存中,以string类返回
	 * @param path 文件路径
	 * @param docName 文件名
	 * @return 文件内容
	 */
	public static String InputAction(String path, String docName) {
		File file = new File(path + "/" + docName);
		if (!file.exists()) {
			
			return null;
		}
		String line = "";
		BufferedReader br = null;
		FileReader reader = null;
		StringBuffer buffer = new StringBuffer();
		try {
			reader = new FileReader(file);
			br = new BufferedReader(reader);
			while ((line = br.readLine()) != null) {
				buffer.append(line + "\r\n");
				// System.out.println(line);
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		//System.out.println(buffer.toString());
		return buffer.toString();
	}

}

3.读取xml,properties文件

	/**
	 * @ps 解析xml文件,获取根节点。此方法在jar包和war包中依然有效(xml文件放在src下)
	 * @param xml文件全名
	 * @return org.dom4j.Element
	 */
	public Element getRootElement(String name) {
		SAXReader reader = new SAXReader();
		Document document=null;
		InputStream in = this.getClass().getClassLoader().getResourceAsStream(name);
		try {
			document = reader.read(in);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(in);
		Element root = document.getRootElement();
		
		return root;
	}



	/**
	 * @ps 解析properties文件,获取对应key的值,在jar包和war包情况下同样适用(properties文件要在src下)
	 * @param 文件名全称
	 * @param key
	 * @return valus
	 */
	public String getValue(String name,String key) {
		Properties p = new Properties();
		InputStream in = null;
		in = this.getClass().getClassLoader().getResourceAsStream(name);
		try {
			p.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		String value = p.getProperty(key);
		return value;
	}


阅读更多
个人分类: java 技术
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

自己写的工具类,获取日期,读取properties,xml,io流

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭