Java时间戳转化为今天、昨天、明天(字符串格式)

原文:http://www.open-open.com/code/view/1435301895825

 

  时间戳,相信大家一定都不陌生,服务器经常会传回来时间戳,需要我们对时间戳进行处理。各种麻烦不断,比如转为为明天,今天,昨天,或者跟现在时间相对比,过了多长时间,转化为标准格式啊...各种情况,写了一个工具类,来分享下

 

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 + "";  
        }  
  
}  

 

转载于:https://www.cnblogs.com/shihaiming/p/7048511.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值