欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java时间戳与日期相互转换工具详解

程序员文章站 2024-02-23 16:56:46
本文为大家分享了java日期与时间戳相互转换大全,供大家参考,具体内容如下 package com.crm.util; import java.math...

本文为大家分享了java日期与时间戳相互转换大全,供大家参考,具体内容如下

package com.crm.util; 
 
import java.math.bigdecimal; 
import java.text.decimalformat; 
import java.text.parseexception; 
import java.text.simpledateformat; 
import java.util.calendar; 
import java.util.date; 
 
 
/** 
 * @author dingjiacheng 
 * */ 
public class dateformatutil { 
   
  /** 
   * 时间戳转日期 
   * @param ms 
   * @return 
   */ 
  public static date transfordate(integer ms){ 
    if(ms==null){ 
      ms=0; 
    } 
    long msl=(long)ms*1000; 
    simpledateformat sdf=new simpledateformat("yyyy-mm-dd hh:mm:ss"); 
    date temp=null; 
    if(ms!=null){ 
      try { 
        string str=sdf.format(msl); 
        temp=sdf.parse(str); 
      } catch (parseexception e) { 
        e.printstacktrace(); 
      } 
    } 
    return temp; 
  } 
   
  /** 
   * 获取晚上9点半的时间戳 
   * 
   * @return 
   */ 
  public static int gettimes(int day, int hour, int minute) { 
    calendar cal = calendar.getinstance(); 
    cal.add(calendar.date, day); 
    cal.set(calendar.hour_of_day, hour); 
    cal.set(calendar.second, 0); 
    cal.set(calendar.minute, minute); 
    cal.set(calendar.millisecond, 0); 
    return (int) (cal.gettimeinmillis() / 1000); 
  } 
   
  /** 
   * 获取当前时间往上的整点时间 
   * 
   * @return 
   */ 
  public static int getintegraltime() { 
    calendar cal = calendar.getinstance(); 
    cal.add(calendar.hour_of_day, 1); 
    cal.set(calendar.second, 0); 
    cal.set(calendar.minute, 0); 
    cal.set(calendar.millisecond, 0); 
    return (int) (cal.gettimeinmillis() / 1000); 
  } 
   
  public static int getintegraltimeend() { 
    calendar cal = calendar.getinstance(); 
    cal.set(calendar.hour_of_day, 24); 
    cal.set(calendar.second, 0); 
    cal.set(calendar.minute, 0); 
    cal.set(calendar.millisecond, 0); 
    return (int) (cal.gettimeinmillis() / 1000); 
  } 
   
  /** 
   * 时间戳转日期 
   * @param ms 
   * @return 
   */ 
  public static date transfordate3(integer ms){ 
    if(ms==null){ 
      ms=0; 
    } 
    long msl=(long)ms*1000; 
    simpledateformat sdf=new simpledateformat("yyyy-mm-dd hh:mm"); 
    date temp=null; 
    if(ms!=null){ 
      try { 
        string str=sdf.format(msl); 
        temp=sdf.parse(str); 
      } catch (parseexception e) { 
        e.printstacktrace(); 
      } 
    } 
    return temp; 
  } 
  /** 
   * 时间戳转日期 
   * @param ms 
   * @return 
   */ 
  public static date transfordate(long ms){ 
    if(ms==null){ 
      ms=(long)0; 
    } 
    long msl=(long)ms*1000; 
    simpledateformat sdf=new simpledateformat("yyyy-mm-dd hh:mm:ss"); 
    date temp=null; 
    if(ms!=null){ 
      try { 
        string str=sdf.format(msl); 
        temp=sdf.parse(str); 
      } catch (parseexception e) { 
        e.printstacktrace(); 
      } 
    } 
    return temp; 
  } 
   
   
  public static string transfordate1(integer ms){ 
    string str = ""; 
    if(ms!=null){ 
      long msl=(long)ms*1000; 
      simpledateformat sdf=new simpledateformat("yyyy-mm-dd hh:mm:ss"); 
       
      if(ms!=null){ 
        try { 
          str=sdf.format(msl); 
        } catch (exception e) { 
          e.printstacktrace(); 
        } 
      } 
    }     
    return str; 
  } 
  public static string transfordate2(integer ms){ 
    string str = ""; 
    if(ms!=null){ 
      long msl=(long)ms*1000; 
      simpledateformat sdf=new simpledateformat("yyyy-mm-dd"); 
       
      if(ms!=null){ 
        try { 
          str=sdf.format(msl); 
        } catch (exception e) { 
          e.printstacktrace(); 
        } 
      } 
    }     
    return str; 
  } 
   
  public static string transfordate4(integer ms){ 
    string str = ""; 
    if(ms!=null){ 
      long msl=(long)ms*1000; 
      simpledateformat sdf=new simpledateformat("yyyy.mm.dd"); 
       
      if(ms!=null){ 
        try { 
          str=sdf.format(msl); 
        } catch (exception e) { 
          e.printstacktrace(); 
        } 
      } 
    }     
    return str; 
  } 
   
   
  public static string transfordate5(integer ms){ 
    string str = ""; 
    if(ms!=null){ 
      long msl=(long)ms*1000; 
      simpledateformat sdf=new simpledateformat("yyyy/mm/dd hh:mm:ss"); 
       
      if(ms!=null){ 
        try { 
          str=sdf.format(msl); 
        } catch (exception e) { 
          e.printstacktrace(); 
        } 
      } 
    }     
    return str; 
  } 
   
  public static string transfordateinchinese(integer ms){ 
    string str = ""; 
    if(ms!=null){ 
      long msl=(long)ms*1000; 
      simpledateformat sdf=new simpledateformat("yyyy年mm月dd日 hh:mm:ss"); 
       
      if(ms!=null){ 
        try { 
          str=sdf.format(msl); 
        } catch (exception e) { 
          e.printstacktrace(); 
        } 
      } 
    }     
    return str; 
  } 
   
  /** 
   * 日期转时间戳 
   * @param date 
   * @return 
   */ 
  public static integer transformillisecond(date date){ 
    if(date==null) return null; 
    return (int)(date.gettime()/1000); 
  } 
   
  /** 
   * 获取当前时间戳 
   * @return 
   */ 
  public static integer currenttimestamp(){ 
    return (int)(system.currenttimemillis()/1000); 
  } 
   
  /** 
   * 日期字符串转时间戳 
   * @param datestr 
   * @return 
   */ 
  public static integer transformillisecond(string datestr){ 
    date date = dateformatutil.formatdate(datestr); 
    return date == null ? null : dateformatutil.transformillisecond(date); 
  } 
  /** 
   * 日期字符串转时间戳 
   * @param datestr 
   * @return 
   */ 
  public static integer transformillisecond(string datestr,string format){ 
    date date = dateformatutil.formatdate(datestr,format); 
    return date == null ? null : dateformatutil.transformillisecond(date); 
  } 
  /** 
   * 日期字符串转时间戳 
   * @param datestr 
   * @param 格式 如"yyyy-mm-dd" 
   * @return 
   */ 
  public static integer transformillisecondbytim(string datestr,string tim){ 
    simpledateformat sdf=new simpledateformat(tim); 
    date date =null; 
    try { 
      date = sdf.parse(datestr); 
    } catch (parseexception e) { 
      e.printstacktrace(); 
    } 
    return date == null ? null : dateformatutil.transformillisecond(date); 
  } 
  /** 
   * 字符串转日期,格式为:"yyyy-mm-dd hh:mm:ss" 
   * @param datestr 
   * @return 
   */ 
  public static date formatdate(string datestr){ 
    simpledateformat sdf=new simpledateformat("yyyy-mm-dd hh:mm:ss"); 
    date result=null; 
    try { 
      result = sdf.parse(datestr); 
    } catch (parseexception e) { 
      e.printstacktrace(); 
    } 
    return result; 
  } 
  /** 
   * 字符串转日期,格式为:"yyyy-mm-dd hh:mm:ss" 
   * @param datestr 
   * @return 
   */ 
  public static date formatdate(string datestr,string format){ 
    simpledateformat sdf=new simpledateformat(format); 
    date result=null; 
    try { 
      result = sdf.parse(datestr); 
    } catch (parseexception e) { 
      e.printstacktrace(); 
    } 
    return result; 
  } 
  /** 
   * 日期转字符串 
   * @param date 
   * @return 
   */ 
  public static string formatdate(date date){ 
    simpledateformat sdf=new simpledateformat("yyyy-mm-dd hh:mm:ss"); 
    string result=null; 
    result = sdf.format(date); 
    return result; 
  } 
  /** 
   * 日期转字符串 
   * @param date 
   * @return 
   */ 
  public static string formatdate(date date,string format){ 
    simpledateformat sdf=new simpledateformat(format); 
    string result=null; 
    result = sdf.format(date); 
    return result; 
  } 
  /** 
   * 时间戳格式化输出(httl模版用) 
   * 
   * @param ms    时间戳 
   * @param format  格式化 
   * @return 
   */ 
  public static string transfordate(integer ms, string format){ 
    string str = ""; 
    if(ms!=null){ 
      long msl=(long)ms*1000; 
      simpledateformat sdf=new simpledateformat(format); 
      if(!ms.equals(0)){ 
        try { 
          str=sdf.format(msl); 
        } catch (exception e) { 
          e.printstacktrace(); 
        } 
      } 
    } 
    return str; 
  }   
   
  /** 
   * 取bigdecimal类型数的整数或小数部分(httl模版用) 
   * 
   * @param b 值 
   * @param mode 模式 0取整 1去小数部分 
   * @return 
   */ 
  public static string splitbigdecimal(bigdecimal b, int mode) { 
    decimalformat df = new decimalformat("0.00"); 
    string s = df.format(b); 
    if(mode==0){ 
      return s.split("\\.")[0]; 
    }else { 
      return "."+s.split("\\.")[1]; 
    } 
  } 
   
  /** 
   * 计算两个日期之间差的天数(httl模版用) 
   * 
   * @param ts1  时间戳1 
   * @param ts2  时间戳2 
   * @return 
   */ 
  public static int caculate2days(integer ts1, integer ts2) { 
    date firstdate = dateformatutil.transfordate(ts1); 
    date seconddate = dateformatutil.transfordate(ts2); 
    calendar calendar = calendar.getinstance(); 
    calendar.settime(firstdate); 
    int daynum1 = calendar.get(calendar.day_of_year); 
    calendar.settime(seconddate); 
    int daynum2 = calendar.get(calendar.day_of_year); 
    return math.abs(daynum1 - daynum2); 
  } 
   
  /** 
   * 给手机加密中间四位加星号 
   * 
   * @param mobile 
   * @return 
   */ 
  public string mobileserect(string mobile){ 
    if(!stringutils.isblank(mobile)){ 
      int between = mobile.length()/2; 
      mobile = mobile.substring(0, between-2)+"****"+mobile.substring(between+2, mobile.length()); 
    } 
    return mobile; 
  } 
   
  /** 
   * 给邮箱加密加星号 
   * 
   * @param email 
   * @return 
   */ 
  public string emailserect(string email) { 
    if(!stringutils.isblank(email)){ 
      int length = email.lastindexof("@"); 
      email = email.substring(0, 2)+"****"+email.substring(length-2, email.length()); 
    } 
    return email; 
  } 
   
  /** 
   * bigdecimal类型数据相加 
   * 
   * @param bigdecimal source 
   * @param bigdecimal target 
   * @return 
   */ 
  public bigdecimal sumbigdicimal(bigdecimal source, bigdecimal target) { 
    source = source.add(target); 
    return source; 
  } 
   
  /** 
   * bigdecimal类型数据相加 
   * 
   * @param bigdecimal source 
   * @param bigdecimal target 
   * @return 
   */ 
  public bigdecimal sumbigdicimalanddouble(bigdecimal source, double target) { 
    bigdecimal new_target = new bigdecimal(target); 
    source = source.add(new_target); 
    return source; 
  } 
   
  /** 
   * bigdecimal类型数据相减 
   * 
   * @param bigdecimal source 
   * @param bigdecimal target 
   * @return 
   */ 
  public bigdecimal subbigdicimal(bigdecimal source, bigdecimal target) { 
    source = source.subtract(target); 
    return source; 
  } 
   
  /** 
   * 获取传入时间和当前时间的时间差 
   * @return 
   */ 
  public static long gettimediff(int timestamp){ 
    date d1 = dateformatutil.transfordate(timestamp); 
    date today = new date(); 
    if(d1.gettime()<today.gettime()){ 
      return null; 
    } 
    return (d1.gettime()-today.gettime())/1000; 
  } 
 
  /** 
   * 获取某周的第一天日期 
   * @param week 0 当周 1 上一周 -1 下一周 
   * @return 
   */ 
  public static string weekfirstday(int week){ 
    calendar c1 = calendar.getinstance(); 
    int dow = c1.get(calendar.day_of_week); 
    c1.add(calendar.date, -dow-7*(week-1)-5 ); 
    string d1 = new simpledateformat("yyyy-mm-dd").format(c1.gettime()); 
    return d1+" 00:00:00"; 
  } 
   
  /** 
   * 当前时间加一年 
   */ 
  public static string addyear(int starttime){ 
    date firstdate = dateformatutil.transfordate(starttime); 
    calendar calendar = calendar.getinstance(); 
    calendar.settime(firstdate); 
    calendar.add(calendar.year,1); 
    string d1 = new simpledateformat("yyyy-mm-dd hh:mm:ss").format(calendar.gettime()); 
    return d1; 
  } 
   
  /** 
   * 获取某周的最后一天日期 
   * @param week 
   * @return 
   */ 
  public static string weeklastday(int week){ 
    calendar c1 = calendar.getinstance(); 
    int dow = c1.get(calendar.day_of_week); 
    c1.add(calendar.date, -dow-7*(week-1)+1); 
    string d1 = new simpledateformat("yyyy-mm-dd").format(c1.gettime()); 
    return d1+" 23:59:59"; 
  }   
   
  /** 
   * 和当前时间比对 
   * @return 
   */ 
  public static boolean greaterthannow(int timestamp){ 
    date d1 = dateformatutil.transfordate(timestamp); 
    date today = new date(); 
    if(d1.gettime()>=today.gettime()){ 
      return true; 
    } 
    return false; 
  } 
 
   
   
  /** 
   * hh:mm:ss格式时间转换为1970-01-01日的时间戳(也就是只有时间没有日期的情况要求使用时间戳表示时间) 
   * @author dingjiacheng 
   * */ 
  public static int transfromtime(string time){ 
    return transformillisecond("1970-01-01 "+time,"yyyy-mm-dd hh:mm:ss"); 
  } 
   
  /** 
   * 时间戳转换为hh:mm:ss格式时间(日期去除) 
   * @author dingjiacheng 
   * */ 
  public static string transtotime(int time){ 
    string s = new string(transfordate1(time)); 
    string ss[] = s.split(" "); 
    return ss[1]; 
  } 
   
  public static int transtochuo(string datestring){ 
    simpledateformat simpledateformat =new simpledateformat("yyyy-mm-dd"); 
    int res = 0; 
    try { 
      date date=simpledateformat .parse(datestring); 
      res = (int) date.gettime(); 
    } catch (parseexception e) { 
      e.printstacktrace(); 
    } 
    return res; 
  } 
   
  public static void main(string[] args) { 
     
    //system.out.println(getintegraltimeend()); 
    system.out.println(transfordate2(transformillisecond("2015-02-25 00:00:00"))); 
    //system.out.println(transformillisecond("2016-01-25","yyyy-mm-dd")); 
    //system.out.println(transfordate1(transformillisecond("1970-01-01 00:00:00","yyyy-mm-dd hh:mm:ss"))); 
    //system.out.println(currenttimestamp()); 
    //system.out.println(transfordate(currenttimestamp())); 
    //system.out.println(new date()); 
    //system.out.println(dateutils.getdate()); 
    system.out.println(transfromtime("00:00:01")); 
    system.out.println(transtotime(transfromtime("15:01:13"))); 
  } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。