package com.happy.Until; import org.apache.commons.lang.time.DateUtils; import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 时间转化工具 date转为时间戳 时间戳转date 互相与String的转换 * 所有出现的String time 格式都必须为(yyyy-MM-dd HH:mm:ss),否则出错 * @author 赵仁杰 * */ public class TimeExchange { /** * String(yyyy-MM-dd HH:mm:ss) 转 Date * * @param time * @return * @throws ParseException */ // String date = "2010/05/04 12:34:23"; public static Date StringToDate(String time) throws ParseException { Date date = new Date(); // 注意format的格式要与日期String的格式相匹配 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { date = dateFormat.parse(time); System.out.println(date.toString()); } catch (Exception e) { e.printStackTrace(); } return date; } /** * Date转为String(yyyy-MM-dd HH:mm:ss) * * @param time * @return */ public static String DateToString(Date time) { String dateStr = ""; Date date = new Date(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH/mm/ss"); try { dateStr = dateFormat.format(time); System.out.println(dateStr); } catch (Exception e) { e.printStackTrace(); } return dateStr; } /** * String(yyyy-MM-dd HH:mm:ss)转10位时间戳 * @param time * @return */ public static Integer StringToTimestamp(String time){ int times = 0; try { times = (int) ((Timestamp.valueOf(time).getTime())/1000); } catch (Exception e) { e.printStackTrace(); } if(times==0){ System.out.println("String转10位时间戳失败"); } return times; } /** * 10位int型的时间戳转换为String(yyyy-MM-dd HH:mm:ss) * @param time * @return */ public static String timestampToString(Integer time){ //int转long时,先进行转型再进行计算,否则会是计算结束后在转型 long temp = (long)time*1000; Timestamp ts = new Timestamp(temp); String tsStr = ""; DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { //方法一 tsStr = dateFormat.format(ts); System.out.println(tsStr); } catch (Exception e) { e.printStackTrace(); } return tsStr; } /** * 10位时间戳转Date * @param time * @return */ public static Date TimestampToDate(Integer time){ long temp = (long)time*1000; Timestamp ts = new Timestamp(temp); Date date = new Date(); try { date = ts; //System.out.println(date); } catch (Exception e) { e.printStackTrace(); } return date; } /** * Date类型转换为10位时间戳 * @param time * @return */ public static Integer DateToTimestamp(Date time){ Timestamp ts = new Timestamp(time.getTime()); return (int) ((ts.getTime())/1000); } // 当前时间加5分钟 public static String TimeRangeI(String time) throws ParseException { // 当前时间+5分钟 Date endTime = DateUtils.addMinutes(StringToDate(time), 300); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return simpleDateFormat.format(endTime); } // 当前时间加2分钟 public static String TimeRangeI10(String time,int m) throws ParseException { Calendar nowTime2 = Calendar.getInstance(); nowTime2.setTime(StringToDate(time)); nowTime2.add(Calendar.SECOND, m);//10分钟前的时间 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return simpleDateFormat.format(nowTime2.getTime()); } // 当前时间减5分钟 public static String TimeRangeD(String time) throws ParseException { Calendar nowTime2 = Calendar.getInstance(); nowTime2.setTime(StringToDate(time)); nowTime2.add(Calendar.MINUTE, -300);//5分钟前的时间 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return simpleDateFormat.format(nowTime2.getTime()); } // 获取当前日期 public static String getDate(){ SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd"); return sp.format(new Date()); } // 获取当前时间 public static String getTime(){ SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sp.format(new Date()); } public static String getYear(){ SimpleDateFormat sp = new SimpleDateFormat("yyyy"); return sp.format(new Date()); } public static String getMonth(){ SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM"); return sp.format(new Date()); } /** * 计算两个日期的时间差 * @param time1 * @param time2 * @return */ public static double getTimeDifference(String time1, String time2) { SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long t1 = 0L; long t2 = 0L; try { t1 = timeformat.parse(time1).getTime(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { t2 = timeformat.parse(time2).getTime(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } //因为t1-t2得到的是毫秒级,所以要初3600000得出小时.算天数或秒同理 double hours=(double) ((t2 - t1)/3600000); double minutes=(double) (((t2 - t1)/1000-hours*3600)/60/60); return hours+minutes; } public static void main(String[] args) throws ParseException { String time1 = "2022-07-22 11:21:20"; String time2 = "2022-07-22 12:31:20"; System.out.println(getTimeDifference(time1,time2)); } }