zoukankan      html  css  js  c++  java
  • java时间戳转换

    import java.text.SimpleDateFormat;  
    import java.util.Calendar;  
    import java.util.Date;  
      
    public class DateUtils {  
              
            /** 
             * 返回unix时间戳 (1970年至今的秒数) 
             * @return 
             */  
            public static long getUnixStamp(){  
                    return System.currentTimeMillis()/1000;  
            }  
              
            /** 
             * 得到昨天的日期 
             * @return 
             */  
            public static String getYestoryDate() {  
                    Calendar calendar = Calendar.getInstance();    
                    calendar.add(Calendar.DATE,-1);  
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
                    String yestoday = sdf.format(calendar.getTime());  
                    return yestoday;  
            }  
              
            /** 
             * 得到今天的日期 
             * @return 
             */  
            public static  String getTodayDate(){  
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
                    String date = sdf.format(new Date());  
                    return date;  
            }  
              
            /** 
             * 时间戳转化为时间格式 
             * @param timeStamp 
             * @return 
             */  
            public static String timeStampToStr(long timeStamp) {  
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
                    String date = sdf.format(timeStamp * 1000);  
                    return date;  
            }  
              
            /** 
             * 得到日期   yyyy-MM-dd 
             * @param timeStamp  时间戳 
             * @return 
             */  
            public static String formatDate(long timeStamp) {     
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
                    String date = sdf.format(timeStamp*1000);  
                    return date;  
            }  
              
            /** 
             * 得到时间  HH:mm:ss 
             * @param timeStamp   时间戳 
             * @return 
             */  
            public static String getTime(long timeStamp) {    
                    String time = null;  
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
                    String date = sdf.format(timeStamp * 1000);  
                    String[] split = date.split("\s");  
                    if ( split.length > 1 ){  
                            time = split[1];  
                    }  
                    return time;  
            }  
              
            /** 
             * 将一个时间戳转换成提示性时间字符串,如刚刚,1秒前 
             *  
             * @param timeStamp 
             * @return 
             */  
            public static String convertTimeToFormat(long timeStamp) {  
                    long curTime =System.currentTimeMillis() / (long) 1000 ;  
                    long time = curTime - timeStamp;  
      
                    if (time < 60 && time >= 0) {  
                            return "刚刚";  
                    } else if (time >= 60 && time < 3600) {  
                            return time / 60 + "分钟前";  
                    } else if (time >= 3600 && time < 3600 * 24) {  
                            return time / 3600 + "小时前";  
                    } else if (time >= 3600 * 24 && time < 3600 * 24 * 30) {  
                            return time / 3600 / 24 + "天前";  
                    } else if (time >= 3600 * 24 * 30 && time < 3600 * 24 * 30 * 12) {  
                            return time / 3600 / 24 / 30 + "个月前";  
                    } else if (time >= 3600 * 24 * 30 * 12) {  
                            return time / 3600 / 24 / 30 / 12 + "年前";  
                    } else {  
                            return "刚刚";  
                    }  
            }  
              
            /** 
             * 将一个时间戳转换成提示性时间字符串,(多少分钟) 
             *  
             * @param timeStamp 
             * @return 
             */  
            public static String timeStampToFormat(long timeStamp) {  
                    long curTime =System.currentTimeMillis() / (long) 1000 ;  
                    long time = curTime - timeStamp;  
                    return time/60 + "";  
            }  
      
    }  
  • 相关阅读:
    LoadRunner参数化&关联
    面试题
    Python---Pycharm如何直接上传自己的代码到GitHub
    深入详解美团点评CAT跨语言服务监控(九)CAT管理平台MVC框架
    深入详解美团点评CAT跨语言服务监控(八)报表持久化
    深入详解美团点评CAT跨语言服务监控(七)消息分析器与报表(二)
    深入详解美团点评CAT跨语言服务监控(六)消息分析器与报表(一)
    深入详解美团点评CAT跨语言服务监控(五)配置与数据库操作
    深入详解美团点评CAT跨语言服务监控(四)服务端消息分发
    深入详解美团点评CAT跨语言服务监控(三)CAT客户端原理
  • 原文地址:https://www.cnblogs.com/dzcWeb/p/7228610.html
Copyright © 2011-2022 走看看