java封装常用方法

1、秒转换为时间显示

此方法适用于xxx秒的视频时间转换为00:00:00格式显示

1)转换为"00:00:00"格式字符串

/**
 * 将秒转换为 00:00:00格式
 * 
 * @param seconds
 * @return
 * @Description
 */
public String showTime(Integer seconds) {
	if (seconds != null) {
		int temp = 0;
		StringBuffer sb = new StringBuffer();
		temp = seconds / 3600;
		sb.append((temp < 10) ? "0" + temp + ":" : "" + temp + ":");

		temp = seconds % 3600 / 60;
		sb.append((temp < 10) ? "0" + temp + ":" : "" + temp + ":");

		temp = seconds % 3600 % 60;
		sb.append((temp < 10) ? "0" + temp : "" + temp);
		return sb.toString();
	}
	return null;
}

2)转化为"1小时1分1秒" 或 “1分1秒” 或 “1秒” 格式字符串

/**
 * 将秒转换为1小时1分1秒
 * 
 * @param seconds
 * @return
 * @Description
 */
public String showTime(Integer seconds) {
	if (seconds != null) {
		if(seconds == 0) {
			return "0秒";
		}
		int temp = 0;
		StringBuffer sb = new StringBuffer();
		temp = seconds / 3600;
		if (temp > 0) {
			sb.append((temp + "小时"));
		}
		temp = seconds % 3600 / 60;
		if (temp > 0) {
			sb.append(temp + "分");
		}
		temp = seconds % 3600 % 60;
		sb.append(temp + "秒");
		return sb.toString();
	}
	return null;
}

2、将时间精确到秒

适用于前台传来精确到“日”或“小时”或“分”的时间自动精确到秒进行保存到数据库
如:(2019-01-01 01:59:59)

1)将LocalDateTime类型的时间精确到秒后返回

/**
 * 将输入的结束时间精确到秒
 * @param LocalDateTime endTime
 * @return LocalDateTime
 * @Description 如果结束时间为2019-01-01则将日加一,然后秒减一变为2019-01-01 23:59:59
 */
public LocalDateTime setTimeToSecond(LocalDateTime endTime) {
	//LocalDateTime endTime = LocalDateTime.of(2019, 1, 1 , 0, 0, 0);
	if(endTime != null) {
		System.out.println("===============输入的结束时间为:"+ endTime + "=============");
		if (endTime.getHour() == 0 &&endTime.getMinute() == 0 && endTime.getSecond() == 0) {
			endTime = endTime.plusDays(1);
		} else if (endTime.getMinute() == 0 && endTime.getSecond() == 0) {
			endTime = endTime.plusHours(1);
		} else {
			endTime = endTime.plusMinutes(1);
		}
		endTime = endTime.minusSeconds(1);
		System.out.println("===============处理后的时间为:"+ endTime + "=============");
	}
	return endTime;
}

2)将Date类型的时间精确到秒后返回

		/**
	 * 将输入的结束时间精确到秒
	 * 
	 * @param Date
	 *            endTime
	 * @return Date
	 */
	public Date setTimeToSecond(Date endtime) {
		// 结束日期精确到秒
		if (endtime != null) {
			System.out.println("==========转换前的时间:" + endtime);
			Calendar cal = Calendar.getInstance();
			cal.setTime(endtime);
			if (cal.get(Calendar.HOUR) == 0 && cal.get(Calendar.MINUTE) == 0 && cal.get(Calendar.SECOND) == 0) {
				cal.add(Calendar.DATE, 1);
			} else if (cal.get(Calendar.MINUTE) == 0 && cal.get(Calendar.SECOND) == 0) {
				cal.add(Calendar.HOUR, 1);
			} else {
				cal.add(Calendar.SECOND, 1);
			}
			cal.add(Calendar.SECOND, -1);
			System.out.println("==========转换后的时间:" + cal.getTime());
			endtime = cal.getTime();
		}
		return endtime;
	}

3、计算当前时间所在月的开始结束时间

关键语句

	Calendar cal = Calendar.getInstance();
	cal.setTime(date);
	cal.set(Calendar.DATE, 1);//设置日为第一天
	
	cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));//设置日为当月最大天数

1)获取当月第一天对应日期

	/**
	 * 获取指定时间当月第一天对应的日期
	 * @param date 参数可以为null,默认当前时间
	 * @return
	 */
	public Date getNowMothFistDate(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		if(date != null) {
			cal.setTime(date);
		}	
		cal.set(Calendar.DATE, 1);//设置日为第一天
		try {
			return sdf.parse(sdf.format(cal.getTime()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

2)获取当月最后一天对应日期

	/**
	 * 获取指定时间当月最后一天对应的日期
	 * @param date  
	 * @return
	 */
	public Date getNowMothLastDate(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		if(date != null) {
			cal.setTime(date);
		}	
		cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));//设置日为当月最大天数
		try {
			return sdf.parse(sdf.format(cal.getTime()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

4、计算当前时间所在周的开始结束时间

关键语句:

	Calendar cal = Calendar.getInstance();
	cal.setTime(date);
	cal.set(Calendar.DAY_OF_WEEK, 2);//设置为周一,1是周日,2才是周一

	cal.set(Calendar.DAY_OF_WEEK, 7);//设置时间到本周最后一天(周六)
	cal.add(Calendar.DATE, 1);//天加一设置到周日

1)获取指定时间当周第一天对应日期

/**
	 * 获取指定时间当周第一天对应的日期
	 * @param date 参数可以为null,默认当前时间
	 * @return
	 */
	public Date getNowWeekFistDate(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		if(date != null) {
			cal.setTime(date);
		}	
		cal.set(Calendar.DAY_OF_WEEK, 2);//设置为周一,1是周日,2才是周一
		try {
			return sdf.parse(sdf.format(cal.getTime()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	

2)获取指定时间当周最后一天对应日期

/**
	 * 获取指定时间当周最后一天对应的日期
	 * @param date  
	 * @return
	 */
	public Date getNowWeekLastDate(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		if(date != null) {
			cal.setTime(date);
		}	
		cal.set(Calendar.DAY_OF_WEEK, 7);//设置时间到本周最后一天(周六)
		cal.add(Calendar.DATE, 1);//天加一设置到周日
		try {
			return sdf.parse(sdf.format(cal.getTime()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

5、计算百分率

	/**
	 * 计算百分率  a/b
	 * 使用 BigDecimal  
	 * @param a
	 * @param b
	 * @param accuracy 保留位数
	 * @return
	 */
	public String getRate(String a, String b, int accuracy) {
		if (b.equals("0")) {
			return "0%";
		}
		BigDecimal decimala = new BigDecimal(a);
		BigDecimal decimalb = new BigDecimal(b);
		
		BigDecimal decimalc = decimala.divide(decimalb, 10, RoundingMode.HALF_UP).multiply(new BigDecimal("100"));//四舍五入方式保留十位
		
		if (decimalc.doubleValue() == 0) {
			return 0 + "%";
		} else if (decimalc.intValue() == 100) {
			return 100 + "%";
		} else {
			//HALF_UP:四舍五入、ROUND_DOWN:向下取整、ROUND_UP: 向上取整
			return decimalc.setScale(accuracy, RoundingMode.HALF_UP).toString() + "%";//设置保留位数,舍入方式
		}
	}

java IO读写文件

public static String getJsonStr() {
        String reStr = "";
        String file = "D:/Java/work/json.txt";
        String line = "";
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
            while ((line = reader.readLine()) != null) {
                System.out.println("Line" + line);
                reStr = reStr + line.trim();
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return reStr;
    }

工具类:

查找某个文件夹下所有的文件,查找关键字所在的文件位名称,以及在文件中行数

public static void main(String[] args) {
        String str = "C:\\Users\\Administrator\\Desktop\\新建文件夹";
        String key = "P3100C02007000200027853";
        File dir = new File(str);
        String[] children = dir.list();
        if (children == null) {
            System.out.println("目录不存在");
        } else {
            for (int i = 0; i < children.length; i++) {
                String filename = children[i];
                File n = new File(str+"\\"+filename);
                BufferedReader reader;
                try {
                    InputStreamReader read = new InputStreamReader(new FileInputStream(n));
                    reader = new BufferedReader(read);
                    String tempStr;
                    int line = 0;
                    while ((tempStr = reader.readLine()) != null) {
                        line++;
                        if(tempStr.indexOf(key) > -1){
                            System.out.println(line + " "+filename);
                        }
                    }
                    reader.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("已查找完!");
        }
    }

递归查询文件下包含关键词得文件名称,行数

    protected static String  str = "D:\\java\\work\\代码变更";
    protected static String key = "汪师傅渠道";

    public static void main(String[] args) {
        File dir = new File(str);
        String dir1 = "";
        searchKeywords(dir,dir1);
        System.out.println("已查找完!");
    }

    public static void searchKeywords(File file, String dir1) {
        String[] fileArr = file.list();
        if(fileArr == null){
            return;
        }
        for (int i = 0; i < fileArr.length; i++) {
            String filename = fileArr[i];
            File newfile = new File(str + dir1 + "\\" + filename);
            if (newfile.isDirectory()) {
                String dir2 = dir1 + "\\" + filename;
                searchKeywords(newfile, dir2);
            } else {
                BufferedReader reader;
                try {
                    InputStreamReader read = new InputStreamReader(new FileInputStream(newfile));
                    reader = new BufferedReader(read);
                    String tempStr;
                    int line = 0;
                    while ((tempStr = reader.readLine()) != null) {
                        line++;
                        if (tempStr.indexOf(key) > -1) {
                            System.out.println(dir1 + ":" + line + " " + filename);
                        }
                    }
                    reader.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值