记录一些个人编写java工具类方法(持续更新)
admin
2024-03-09 08:43:00
0

记录一些个人编写java工具类方法

  • 一、增加年数日期方法
  • 二、传入年月日字符串返回年份字符串方法
  • 三、输入yyyy-MM-dd日期字符串返回yyyyMMdd方法
  • 四、传入文件路径(包含文件名)获取 文件路径(不包含文件名)方法
  • 五、带逗号的字符串转String数组
  • 六、字符串转List(用到第五个方法)
  • 七、ZIP压缩包解压方法

一、增加年数日期方法

/*** 增加年数日期方法* * @param dateString 日期* @param yearNum    增加年数* @return 增加年数后的日期*/public String addYearString(String dateString, int yearNum) {String addYearString = "";if (!dateString.equals("") && null != dateString) {try {Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateString);Calendar ca = Calendar.getInstance();ca.setTime(date);ca.add(Calendar.YEAR, yearNum);SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");addYearString = sdf.format(ca.getTime());} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return addYearString;}

二、传入年月日字符串返回年份字符串方法

/*** 返回年份字符串方法* * @param dateString 日期字符串* @return 年份字符串*/public String getYearsString(String dateString) {String yearString = "";if (!dateString.equals("") && null != dateString) {try {Date date = new SimpleDateFormat("yyyy").parse(dateString);Calendar ca = Calendar.getInstance();ca.setTime(date);int year = ca.get(Calendar.YEAR);// 年份数值yearString = String.valueOf(year);} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return yearString;}

三、输入yyyy-MM-dd日期字符串返回yyyyMMdd方法

/*** 返回yyyyMMdd日期字符串方法* * @param dateString 日期字符串* @return yyyyMMdd格式的日期字符串*/public String getYearMonDay(String dateString) {String yearMonDay = null;if (!dateString.equals("") && null != dateString) {SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");try {Date dt = simpleDateFormat.parse(dateString);String year = String.format("%tY", dt);String mon = String.format("%tm", dt);String day = String.format("%td", dt);yearMonDay = year.concat(mon).concat(day);} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return yearMonDay;}

四、传入文件路径(包含文件名)获取 文件路径(不包含文件名)方法

/*** 传入文件路径(包含文件名)获取 文件路径(不包含文件名)方法* * @param filePathName 文件路径(包含文件名)* @return 文件路径(不包含文件名)*/public String getAbsolutePath(String filePathName) {String newTwoString = "";if (!filePathName.equals("") && null != filePathName) {// 取得最后一个/的下标int index = filePathName.lastIndexOf("\\");// substring(int beginIndex)返回一个新的字符串,它是此字符串的一个子字符串。newTwoString = filePathName.substring(0, index + 1);}return newTwoString;}

五、带逗号的字符串转String数组

/*** 字符串转化为String数组* * @param stringValue 带逗号的字符串* @return String数组*/private String[] privateGetStringArray(String stringValue) {String[] arrString = stringValue.split(",");return arrString;}

六、字符串转List(用到第五个方法)

/*** * 字符串转list* * @param docString   要转换的字符串* @param logString   要转换的列名* @param contractNum 流程编号* @return list数组*/public List getDocIdList(String docString, String logString, String contractNum) {List docIdList = new ArrayList();if (!docString.equals("") && null != docString) {String[] docStringArray = privateGetStringArray(docString, logString, contractNum);for (int i = 0; i < docStringArray.length; i++) {docIdList.add(docStringArray[i]);}return docIdList;} else {return docIdList;}}

七、ZIP压缩包解压方法

/*** zip压缩包解压方法* * @param pathNameString 文件路径* @param fileNameString 解压后的文件名* @return File 类*/public File getFile(String pathNameString, String fileNameString) {try {
//			TransferUtils transferUtils=new TransferUtils();ZipInputStream zin = new ZipInputStream(new FileInputStream(pathNameString));// 输入源zip路径BufferedInputStream bin = new BufferedInputStream(zin);String parent = privateAbsolutePath(pathNameString); // 输出路径(文件夹目录)File fout = null;ZipEntry entry;try {while ((entry = zin.getNextEntry()) != null && !entry.isDirectory()) {fout = new File(parent, fileNameString);if (!fout.exists()) {(new File(fout.getParent())).mkdirs();}FileOutputStream out = new FileOutputStream(fout);BufferedOutputStream bout = new BufferedOutputStream(out);int b;while ((b = bin.read()) != -1) {bout.write(b);}bout.close();out.close();}bin.close();zin.close();} catch (IOException e) {e.printStackTrace();}return fout;} catch (FileNotFoundException e) {e.printStackTrace();}return null;}

相关内容

热门资讯

原创 暴... 2025年上半年,泰国旅游业面临严峻挑战,特别是中国游客数量大幅减少,对行业造成沉重打击。据统计,今...
如果这个世界上有八号当铺,你愿... 如果这个世界上有八号当铺,你愿意进去典当吗?我愿意进去典当,因为我觉得用自己不是那么在意的东西去换取...
能不能推荐几本校园青春小说,不... 能不能推荐几本校园青春小说,不要连载的,要有大结局的,求你啦!拽丫头与校草同居左耳,离歌,沙漏,会有...
电影《穿靴子的猫》十四分钟半左... 电影《穿靴子的猫》十四分钟半左右那段音乐,好像是小号吹的吧是americano
“贞子”是什么? “贞子”是什么?山上长得一种植物.果实外边有一层硬皮砸开之后瓤可以吃1998年,由松岛菜菜子、真田广...
海贼王火影死神哪部好看 海贼王火影死神哪部好看这三部动画都很出名,我想问下大家他们各自的含义,还有听听看大家的意见火影胜在剧...
求穿越搞笑的小说 求穿越搞笑的小说俏皮小妞之天下无双极品家丁......
胃镜检查需要全麻吗,大约需要多... 胃镜检查需要全麻吗,大约需要多长时间胃镜根本不用麻醉。。。
《我为歌狂》续集的顺序是什么? 《我为歌狂》续集的顺序是什么?《我为歌狂》有两部互相无关的续集,一部是《我为歌狂终结本8848》,另...
彼岸花开,花开彼岸。花开不见叶... 彼岸花开,花开彼岸。花开不见叶,叶生不见花,相念相惜不相见。传说种的花和叶永不相见,就这样吗?彼岸花...
参天小银瓶好不好? 参天小银瓶好不好?建议买小样试试,每个人肤质都不同。
大耳朵图图简介? 大耳朵图图简介?该剧讲述了小朋友图图和他的父母一家三日的生活琐事,其中图图是个小捣蛋儿,脑瓜里充满了...
狱血魔神觉醒流程 狱血魔神觉醒流程觉醒任务:  达到48级后,找G.S.D接取觉醒任务任务名称 完成条件 觉醒-狂暴...
红莲是怎么死的? 红莲是怎么死的?谁能给我讲一下从红莲和三尾斗,昏迷后被幽鬼丸叫醒一直到红莲死有关红莲的事?先谢谢了,...
中级口译? 中级口译?想考中级口译,不太了解这个考试,有2个问题1,中口先考笔译然后才能口译,中间可以间隔多长时...
实体书是什么意思 实体书是什么意思实体书是什么意思顾名思义,就是指已经出版发行了,并且印刷完成推向市场,读者能够在书店...
求男主角回到过去,又遇上了以前... 求男主角回到过去,又遇上了以前的自己,然后和以前的自己在一起的小说你是的这 是小说?自己爱上自己.那...
治疗肺癌的名老中医 治疗肺癌的名老中医你好,我最近老是觉得腹胀的厉害,经常咳嗽的上不来气,老是觉得嗓子跟有东西似的,开始...
肉唐僧为什么不在嫣牛博上写博客... 肉唐僧为什么不在嫣牛博上写博客了?他可能去西天取经路上到了没有wifi的地方,过两天有网了他就写了~...
给我挚爱的或给我的挚爱用法语怎... 给我挚爱的或给我的挚爱用法语怎么说!“给我挚爱的XXX ”或 “给我的挚爱XXX ”用法语怎么说!~...