java日期与时间戳相互转换大全

97 篇文章 1 订阅
82 篇文章 1 订阅


各种时间戳与日期之间相互转换的工具,,,这么多方法肯定有你想要的功能233333333害羞

  1. package com.crm.util;  
  2.   
  3. import java.math.BigDecimal;  
  4. import java.text.DecimalFormat;  
  5. import java.text.ParseException;  
  6. import java.text.SimpleDateFormat;  
  7. import java.util.Calendar;  
  8. import java.util.Date;  
  9.   
  10.   
  11. /** 
  12.  * @author DingJiaCheng 
  13.  * */  
  14. public class DateFormatUtil {  
  15.       
  16.     /** 
  17.      * 时间戳转日期 
  18.      * @param ms 
  19.      * @return 
  20.      */  
  21.     public static Date transForDate(Integer ms){  
  22.         if(ms==null){  
  23.             ms=0;  
  24.         }  
  25.         long msl=(long)ms*1000;  
  26.         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  27.         Date temp=null;  
  28.         if(ms!=null){  
  29.             try {  
  30.                 String str=sdf.format(msl);  
  31.                 temp=sdf.parse(str);  
  32.             } catch (ParseException e) {  
  33.                 e.printStackTrace();  
  34.             }  
  35.         }  
  36.         return temp;  
  37.     }  
  38.       
  39.     /** 
  40.      * 获取晚上9点半的时间戳 
  41.      *  
  42.      * @return 
  43.      */  
  44.     public static int getTimes(int day, int hour, int minute) {  
  45.         Calendar cal = Calendar.getInstance();  
  46.         cal.add(Calendar.DATE, day);  
  47.         cal.set(Calendar.HOUR_OF_DAY, hour);  
  48.         cal.set(Calendar.SECOND, 0);  
  49.         cal.set(Calendar.MINUTE, minute);  
  50.         cal.set(Calendar.MILLISECOND, 0);  
  51.         return (int) (cal.getTimeInMillis() / 1000);  
  52.     }  
  53.       
  54.     /** 
  55.      * 获取当前时间往上的整点时间 
  56.      *  
  57.      * @return 
  58.      */  
  59.     public static int getIntegralTime() {  
  60.         Calendar cal = Calendar.getInstance();  
  61.         cal.add(Calendar.HOUR_OF_DAY, 1);  
  62.         cal.set(Calendar.SECOND, 0);  
  63.         cal.set(Calendar.MINUTE, 0);  
  64.         cal.set(Calendar.MILLISECOND, 0);  
  65.         return (int) (cal.getTimeInMillis() / 1000);  
  66.     }  
  67.       
  68.     public static int getIntegralTimeEnd() {  
  69.         Calendar cal = Calendar.getInstance();  
  70.         cal.set(Calendar.HOUR_OF_DAY, 24);  
  71.         cal.set(Calendar.SECOND, 0);  
  72.         cal.set(Calendar.MINUTE, 0);  
  73.         cal.set(Calendar.MILLISECOND, 0);  
  74.         return (int) (cal.getTimeInMillis() / 1000);  
  75.     }  
  76.       
  77.       
  78.       
  79.       
  80.       
  81.       
  82.       
  83.       
  84.   
  85.       
  86.       
  87.     /** 
  88.      * 时间戳转日期 
  89.      * @param ms 
  90.      * @return 
  91.      */  
  92.     public static Date transForDate3(Integer ms){  
  93.         if(ms==null){  
  94.             ms=0;  
  95.         }  
  96.         long msl=(long)ms*1000;  
  97.         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");  
  98.         Date temp=null;  
  99.         if(ms!=null){  
  100.             try {  
  101.                 String str=sdf.format(msl);  
  102.                 temp=sdf.parse(str);  
  103.             } catch (ParseException e) {  
  104.                 e.printStackTrace();  
  105.             }  
  106.         }  
  107.         return temp;  
  108.     }  
  109.     /** 
  110.      * 时间戳转日期 
  111.      * @param ms 
  112.      * @return 
  113.      */  
  114.     public static Date transForDate(Long ms){  
  115.         if(ms==null){  
  116.             ms=(long)0;  
  117.         }  
  118.         long msl=(long)ms*1000;  
  119.         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  120.         Date temp=null;  
  121.         if(ms!=null){  
  122.             try {  
  123.                 String str=sdf.format(msl);  
  124.                 temp=sdf.parse(str);  
  125.             } catch (ParseException e) {  
  126.                 e.printStackTrace();  
  127.             }  
  128.         }  
  129.         return temp;  
  130.     }  
  131.       
  132.       
  133.     public static String transForDate1(Integer ms){  
  134.         String str = "";  
  135.         if(ms!=null){  
  136.             long msl=(long)ms*1000;  
  137.             SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  138.               
  139.             if(ms!=null){  
  140.                 try {  
  141.                     str=sdf.format(msl);  
  142.                 } catch (Exception e) {  
  143.                     e.printStackTrace();  
  144.                 }  
  145.             }  
  146.         }         
  147.         return str;  
  148.     }  
  149.     public static String transForDate2(Integer ms){  
  150.         String str = "";  
  151.         if(ms!=null){  
  152.             long msl=(long)ms*1000;  
  153.             SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
  154.               
  155.             if(ms!=null){  
  156.                 try {  
  157.                     str=sdf.format(msl);  
  158.                 } catch (Exception e) {  
  159.                     e.printStackTrace();  
  160.                 }  
  161.             }  
  162.         }         
  163.         return str;  
  164.     }  
  165.       
  166.     public static String transForDate4(Integer ms){  
  167.         String str = "";  
  168.         if(ms!=null){  
  169.             long msl=(long)ms*1000;  
  170.             SimpleDateFormat sdf=new SimpleDateFormat("yyyy.MM.dd");  
  171.               
  172.             if(ms!=null){  
  173.                 try {  
  174.                     str=sdf.format(msl);  
  175.                 } catch (Exception e) {  
  176.                     e.printStackTrace();  
  177.                 }  
  178.             }  
  179.         }         
  180.         return str;  
  181.     }  
  182.       
  183.       
  184.     public static String transForDate5(Integer ms){  
  185.         String str = "";  
  186.         if(ms!=null){  
  187.             long msl=(long)ms*1000;  
  188.             SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");  
  189.               
  190.             if(ms!=null){  
  191.                 try {  
  192.                     str=sdf.format(msl);  
  193.                 } catch (Exception e) {  
  194.                     e.printStackTrace();  
  195.                 }  
  196.             }  
  197.         }         
  198.         return str;  
  199.     }  
  200.       
  201.     public static String transForDateInChinese(Integer ms){  
  202.         String str = "";  
  203.         if(ms!=null){  
  204.             long msl=(long)ms*1000;  
  205.             SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");  
  206.               
  207.             if(ms!=null){  
  208.                 try {  
  209.                     str=sdf.format(msl);  
  210.                 } catch (Exception e) {  
  211.                     e.printStackTrace();  
  212.                 }  
  213.             }  
  214.         }         
  215.         return str;  
  216.     }  
  217.       
  218.     /** 
  219.      * 日期转时间戳 
  220.      * @param date 
  221.      * @return 
  222.      */  
  223.     public static Integer transForMilliSecond(Date date){  
  224.         if(date==nullreturn null;  
  225.         return (int)(date.getTime()/1000);  
  226.     }  
  227.       
  228.     /** 
  229.      * 获取当前时间戳 
  230.      * @return 
  231.      */  
  232.     public static Integer currentTimeStamp(){  
  233.         return (int)(System.currentTimeMillis()/1000);  
  234.     }  
  235.       
  236.     /** 
  237.      * 日期字符串转时间戳 
  238.      * @param dateStr 
  239.      * @return 
  240.      */  
  241.     public static Integer transForMilliSecond(String dateStr){  
  242.         Date date = DateFormatUtil.formatDate(dateStr);  
  243.         return date == null ? null : DateFormatUtil.transForMilliSecond(date);  
  244.     }  
  245.     /** 
  246.      * 日期字符串转时间戳 
  247.      * @param dateStr 
  248.      * @return 
  249.      */  
  250.     public static Integer transForMilliSecond(String dateStr,String format){  
  251.         Date date = DateFormatUtil.formatDate(dateStr,format);  
  252.         return date == null ? null : DateFormatUtil.transForMilliSecond(date);  
  253.     }  
  254.     /** 
  255.      * 日期字符串转时间戳 
  256.      * @param dateStr 
  257.      * @param 格式 如"yyyy-mm-dd" 
  258.      * @return 
  259.      */  
  260.     public static Integer transForMilliSecondByTim(String dateStr,String tim){  
  261.         SimpleDateFormat sdf=new SimpleDateFormat(tim);  
  262.         Date date =null;  
  263.         try {  
  264.             date = sdf.parse(dateStr);  
  265.         } catch (ParseException e) {  
  266.             e.printStackTrace();  
  267.         }  
  268.         return date == null ? null : DateFormatUtil.transForMilliSecond(date);  
  269.     }  
  270.     /** 
  271.      * 字符串转日期,格式为:"yyyy-MM-dd HH:mm:ss" 
  272.      * @param dateStr 
  273.      * @return 
  274.      */  
  275.     public static Date formatDate(String dateStr){  
  276.         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  277.         Date result=null;  
  278.         try {  
  279.             result = sdf.parse(dateStr);  
  280.         } catch (ParseException e) {  
  281.             e.printStackTrace();  
  282.         }  
  283.         return result;  
  284.     }  
  285.     /** 
  286.      * 字符串转日期,格式为:"yyyy-MM-dd HH:mm:ss" 
  287.      * @param dateStr 
  288.      * @return 
  289.      */  
  290.     public static Date formatDate(String dateStr,String format){  
  291.         SimpleDateFormat sdf=new SimpleDateFormat(format);  
  292.         Date result=null;  
  293.         try {  
  294.             result = sdf.parse(dateStr);  
  295.         } catch (ParseException e) {  
  296.             e.printStackTrace();  
  297.         }  
  298.         return result;  
  299.     }  
  300.     /** 
  301.      * 日期转字符串 
  302.      * @param date 
  303.      * @return 
  304.      */  
  305.     public static String formatDate(Date date){  
  306.         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  307.         String result=null;  
  308.         result = sdf.format(date);  
  309.         return result;  
  310.     }  
  311.     /** 
  312.      * 日期转字符串 
  313.      * @param date 
  314.      * @return 
  315.      */  
  316.     public static String formatDate(Date date,String format){  
  317.         SimpleDateFormat sdf=new SimpleDateFormat(format);  
  318.         String result=null;  
  319.         result = sdf.format(date);  
  320.         return result;  
  321.     }  
  322.     /** 
  323.      * 时间戳格式化输出(httl模版用) 
  324.      *  
  325.      * @param ms        时间戳 
  326.      * @param format    格式化 
  327.      * @return 
  328.      */  
  329.     public static String transForDate(Integer ms, String format){  
  330.         String str = "";  
  331.         if(ms!=null){  
  332.             long msl=(long)ms*1000;  
  333.             SimpleDateFormat sdf=new SimpleDateFormat(format);  
  334.             if(!ms.equals(0)){  
  335.                 try {  
  336.                     str=sdf.format(msl);  
  337.                 } catch (Exception e) {  
  338.                     e.printStackTrace();  
  339.                 }  
  340.             }  
  341.         }  
  342.         return str;  
  343.     }     
  344.       
  345.     /** 
  346.      * 取BigDecimal类型数的整数或小数部分(httl模版用) 
  347.      *  
  348.      * @param b 值 
  349.      * @param mode  模式 0取整 1去小数部分 
  350.      * @return 
  351.      */  
  352.     public static String splitBigDecimal(BigDecimal b, int mode) {  
  353.         DecimalFormat df = new DecimalFormat("0.00");  
  354.         String s = df.format(b);  
  355.         if(mode==0){  
  356.             return s.split("\\.")[0];  
  357.         }else {  
  358.             return "."+s.split("\\.")[1];  
  359.         }  
  360.     }  
  361.       
  362.     /** 
  363.      * 计算两个日期之间差的天数(httl模版用) 
  364.      *  
  365.      * @param ts1   时间戳1 
  366.      * @param ts2   时间戳2 
  367.      * @return 
  368.      */  
  369.     public static int caculate2Days(Integer ts1, Integer ts2) {  
  370.         Date firstDate = DateFormatUtil.transForDate(ts1);  
  371.         Date secondDate = DateFormatUtil.transForDate(ts2);  
  372.         Calendar calendar = Calendar.getInstance();  
  373.         calendar.setTime(firstDate);  
  374.         int dayNum1 = calendar.get(Calendar.DAY_OF_YEAR);  
  375.         calendar.setTime(secondDate);  
  376.         int dayNum2 = calendar.get(Calendar.DAY_OF_YEAR);  
  377.         return Math.abs(dayNum1 - dayNum2);  
  378.     }  
  379.       
  380.     /** 
  381.      * 给手机加密中间四位加星号 
  382.      *  
  383.      * @param mobile 
  384.      * @return 
  385.      */  
  386.     public String mobileSerect(String mobile){  
  387.         if(!StringUtils.isBlank(mobile)){  
  388.             int between = mobile.length()/2;  
  389.             mobile = mobile.substring(0, between-2)+"****"+mobile.substring(between+2, mobile.length());  
  390.         }  
  391.         return mobile;  
  392.     }  
  393.       
  394.     /** 
  395.      * 给邮箱加密加星号 
  396.      *  
  397.      * @param email 
  398.      * @return 
  399.      */  
  400.     public String emailSerect(String email) {  
  401.         if(!StringUtils.isBlank(email)){  
  402.             int length = email.lastIndexOf("@");  
  403.             email = email.substring(02)+"****"+email.substring(length-2, email.length());  
  404.         }  
  405.         return email;  
  406.     }  
  407.       
  408.     /** 
  409.      * BigDecimal类型数据相加 
  410.      *  
  411.      * @param BigDecimal source 
  412.      * @param BigDecimal target 
  413.      * @return 
  414.      */  
  415.     public BigDecimal sumBigDicimal(BigDecimal source, BigDecimal target) {  
  416.         source = source.add(target);  
  417.         return source;  
  418.     }  
  419.       
  420.     /** 
  421.      * BigDecimal类型数据相加 
  422.      *  
  423.      * @param BigDecimal source 
  424.      * @param BigDecimal target 
  425.      * @return 
  426.      */  
  427.     public BigDecimal sumBigDicimalAndDouble(BigDecimal source, Double target) {  
  428.         BigDecimal new_target = new BigDecimal(target);  
  429.         source = source.add(new_target);  
  430.         return source;  
  431.     }  
  432.       
  433.     /** 
  434.      * BigDecimal类型数据相减 
  435.      *  
  436.      * @param BigDecimal source 
  437.      * @param BigDecimal target 
  438.      * @return 
  439.      */  
  440.     public BigDecimal subBigDicimal(BigDecimal source, BigDecimal target) {  
  441.         source = source.subtract(target);  
  442.         return source;  
  443.     }  
  444.       
  445.     /** 
  446.      * 获取传入时间和当前时间的时间差 
  447.      * @return 
  448.      */  
  449.     public static Long getTimediff(int timeStamp){  
  450.         Date d1 = DateFormatUtil.transForDate(timeStamp);  
  451.         Date today = new Date();  
  452.         if(d1.getTime()<today.getTime()){  
  453.             return null;  
  454.         }  
  455.         return (d1.getTime()-today.getTime())/1000;  
  456.     }  
  457.   
  458.     /** 
  459.      * 获取某周的第一天日期 
  460.      * @param week 0 当周 1 上一周 -1 下一周 
  461.      * @return 
  462.      */  
  463.     public static String weekFirstDay(int week){  
  464.         Calendar c1 = Calendar.getInstance();  
  465.         int dow = c1.get(Calendar.DAY_OF_WEEK);  
  466.         c1.add(Calendar.DATE, -dow-7*(week-1)-5 );  
  467.         String d1 = new SimpleDateFormat("yyyy-MM-dd").format(c1.getTime());  
  468.         return d1+" 00:00:00";  
  469.     }  
  470.       
  471.     /** 
  472.      * 当前时间加一年 
  473.      */  
  474.     public static String addYear(int startTime){  
  475.         Date firstDate = DateFormatUtil.transForDate(startTime);  
  476.         Calendar calendar = Calendar.getInstance();  
  477.         calendar.setTime(firstDate);  
  478.         calendar.add(Calendar.YEAR,1);  
  479.         String d1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());  
  480.         return d1;  
  481.     }  
  482.       
  483.     /** 
  484.      * 获取某周的最后一天日期 
  485.      * @param week 
  486.      * @return 
  487.      */  
  488.     public static String weekLastDay(int week){  
  489.         Calendar c1 = Calendar.getInstance();  
  490.         int dow = c1.get(Calendar.DAY_OF_WEEK);  
  491.         c1.add(Calendar.DATE, -dow-7*(week-1)+1);  
  492.         String d1 = new SimpleDateFormat("yyyy-MM-dd").format(c1.getTime());  
  493.         return d1+" 23:59:59";  
  494.     }     
  495.       
  496.     /** 
  497.      * 和当前时间比对 
  498.      * @return 
  499.      */  
  500.     public static boolean greaterThanNow(int timeStamp){  
  501.         Date d1 = DateFormatUtil.transForDate(timeStamp);  
  502.         Date today = new Date();  
  503.         if(d1.getTime()>=today.getTime()){  
  504.             return true;  
  505.         }  
  506.         return false;  
  507.     }  
  508.   
  509.       
  510.       
  511.     /** 
  512.      * HH:mm:ss格式时间转换为1970-01-01日的时间戳(也就是只有时间没有日期的情况要求使用时间戳表示时间) 
  513.      * @author DingJiaCheng 
  514.      * */  
  515.     public static int transFromTime(String time){  
  516.         return transForMilliSecond("1970-01-01 "+time,"yyyy-mm-dd HH:mm:ss");  
  517.     }  
  518.       
  519.     /** 
  520.      * 时间戳转换为HH:mm:ss格式时间(日期去除) 
  521.      * @author DingJiaCheng 
  522.      * */  
  523.     public static String transToTime(int time){  
  524.         String s = new String(transForDate1(time));  
  525.         String ss[] = s.split(" ");  
  526.         return ss[1];  
  527.     }  
  528.       
  529.     public static int transToChuo(String dateString){  
  530.         SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");  
  531.         int res = 0;  
  532.         try {  
  533.             Date date=simpleDateFormat .parse(dateString);  
  534.             res = (int) date.getTime();  
  535.         } catch (ParseException e) {  
  536.             e.printStackTrace();  
  537.         }  
  538.         return res;  
  539.     }  
  540.       
  541.     public static void main(String[] args) {  
  542.           
  543.         //System.out.println(getIntegralTimeEnd());  
  544.         System.out.println(transForDate2(transForMilliSecond("2015-02-25 00:00:00")));  
  545.         //System.out.println(transForMilliSecond("2016-01-25","yyyy-mm-dd"));  
  546.         //System.out.println(transForDate1(transForMilliSecond("1970-01-01 00:00:00","yyyy-mm-dd HH:mm:ss")));  
  547.         //System.out.println(currentTimeStamp());  
  548.         //System.out.println(transForDate(currentTimeStamp()));  
  549.         //System.out.println(new Date());  
  550.         //System.out.println(DateUtils.getDate());  
  551.         System.out.println(transFromTime("00:00:01"));  
  552.         System.out.println(transToTime(transFromTime("15:01:13")));  
  553.     }  
  554. }  

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值