前言
记录用到过的时间相关小脚本。
一、时间相关小脚本
1.1字符串转日期
/** * 字符串转换成日期 * * @作者:张帅 * @日期:2019-04-22 * @param str * @return date */ public static Date strToDate(String str) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); //ZCLG20170630 添加日期格式 mod LiGe 20170630 if(str.indexOf(‘年‘) > -1 && str.indexOf(‘月‘) > -1 && str.indexOf(‘日‘) > -1){ format = new SimpleDateFormat("yyyy年MM月dd日"); }else if(str.indexOf(‘年‘) > -1 && str.indexOf(‘月‘) > -1 && str.indexOf(‘号‘) > -1){ format = new SimpleDateFormat("yyyy年MM月dd号"); } Date date = null; try { date = format.parse(str); } catch (ParseException e) { e.printStackTrace(); } return date; }
1.2calendar设置时间
1、
//给存储过程的参数设置值 java.util.Date d = new java.util.Date(); Calendar calendar = new GregorianCalendar(); calendar.setTime(d); calendar.add(calendar.DATE,-1);//把日期往后增加一天.整数往后推,负数往前移动 d = calendar.getTime();//这个时间就是日期往后推一天的结果 java.sql.Date date = new java.sql.Date(d.getTime());//会丢失时分秒 Calendar calendar = new GregorianCalendar(); int currentMonth = calendar.get(Calendar.MONTH )+1; calendar.set(calendar.get(Calendar.YEAR), currentMonth, 1); calendar.add(calendar.DATE,-1);//把日期往后增加一天.整数往后推,负数往前移动 int day=calendar.get(Calendar.DAY_OF_MONTH); int year = calendar.get(Calendar.YEAR); java.sql.Date date = null; try { date = (Date)new SimpleDateFormat("yyyy-MM-dd").parse(String.valueOf(year)+"-"+String.valueOf(currentMonth)+"-"+String.valueOf(day)); //date=(Date)TimeUtils.getDate("yyyy-MM-dd",String.valueOf(year+"-"+currentMonth+"-"+day)); } catch (ParseException e) { e.printStackTrace(); ConnectionUtil.rollBackTransaction(conn); }
2、java中 日期增加指定天数
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date=new Date(); String dateFormat1=sdf.format(date); System.out.println(dateFormat1); System.out.println(date); Calendar calendar=Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DATE, 4); Date date2=calendar.getTime(); System.out.println(date2); String dateFormat=sdf.format(date2); System.out.println(dateFormat);
1.3获取时间的年月日
1、jdk7
String year1=String.format("%tY", startDate); String year2=String.format("%tY", endDate); String mon1=String .format("%tm", startDate); String mon2=String .format("%tm", endDate); String day1=String .format("%td", startDate); String day2=String .format("%td", endDate);
2、jdk8关于时间的新函数及其使用
ChronoUnit
Duration
1.4时间戳
1、方式一
//得到执行时间 /*String times="2018-03-31"; Date date=null; try { date= TimeUtils.getDate(times,"yyyy-MM-dd"); } catch (ParseException e) { e.printStackTrace(); } long t0 = System.currentTimeMillis(); // 代码执行前时间 BigDecimal exch=null; for(int i=0;i<=400;i++){ exch=dataDictionaryService.getExch("CNY","CNY",date); } long t1 = System.currentTimeMillis(); // 代码执行后时间 Calendar c = Calendar.getInstance(); c.setTimeInMillis(t1 - t0); System.out.println("耗时: " + c.get(Calendar.MINUTE) + "分 " + c.get(Calendar.SECOND) + "秒 " + c.get(Calendar.MILLISECOND) + " 微秒");
2、方式二
package com.asd.common.utils.datetag; import java.util.Date; public class DateTag { private Date startDate; private Date lastDate; private StringBuilder info = new StringBuilder(); private int messageIndex = 0; public DateTag(String message) { this.lastDate = new Date(); this.startDate = new Date(); info.append(message); info.append(":"); this.messageIndex = message.length()+1; } public void appendDate(String message){ info.append(message); info.append(":"); info.append(new Date().getTime()-lastDate.getTime()); info.append(";"); this.lastDate = new Date(); } public void addFist(String message){ info.insert(messageIndex, ";"); info.insert(messageIndex, new Date().getTime()-startDate.getTime()); info.insert(messageIndex, ":"); info.insert(messageIndex, message); this.lastDate = new Date(); } public String getInfo(){ return info.toString(); } } DateTag tag = new DateTag("sss"); BigDecimal ss = exch == null ? BigDecimal.ZERO : exch.getExchRate().divide(new BigDecimal(exch.getBase())); tag.appendDate("从数据字段获取用时"); tag.addFist("总用时"); System.out.println(tag.getInfo());