Timetool 显示时间 如 2天前

/**
 * 获取当前系统时间
 *
 */
public class Timetool {
/**
* 将时间戳转为代表"距现在多久之前"的字符串
* @param timeStr 时间戳
* @return
*/
public static String getStandardDate(String timeStr) {
if(timeStr == null || timeStr.trim().equals("")){
return null;
}
StringBuffer sb = new StringBuffer();
long t = Long.parseLong(timeStr);
long time = System.currentTimeMillis() - (t*1000);
long mill = (long) Math.ceil(time /1000);//秒前


long minute = (long) Math.ceil(time/60/1000.0f);// 分钟前


long hour = (long) Math.ceil(time/60/60/1000.0f);// 小时


long day = (long) Math.ceil(time/24/60/60/1000.0f);// 天前


// if (day - 1 > 0) {
// sb.append(day + "天");
// } else if (hour - 1 > 0) {
// if (hour >= 24) {
// sb.append("1天");
// } else {
// sb.append(hour + "小时");
// }
if (day - 1 > 0) {
sb.append(trans_time(Long.parseLong(timeStr)).substring(0, 10));
} else if (hour - 1 > 0) {
if (hour >= 24) {
sb.append(trans_time(Long.parseLong(timeStr)).substring(0, 10));
} else {
sb.append(trans_time(Long.parseLong(timeStr)).substring(11));
}
} else if (minute - 1 > 0) {
if (minute == 60) {
sb.append("1小时前");
} else {
sb.append(minute + "分钟前"); 
}
} else if (mill - 1 > 0) {
if (mill == 60) {
sb.append("1分钟前");
} else {
sb.append(mill + "秒前");
}
} else {
sb.append("刚刚");
}
return sb.toString();
}
public static String gettime(){
Time time = new Time("GMT+8");       
    time.setToNow();      
    int year = time.year;      
    int month = time.month;      
    int day = time.monthDay;      
    int minute = time.minute;      
    int hour = time.hour;      
    int sec = time.second;      
//     myTextView.setText("当前时间为:" + year +       
//                         "年 " + month +       
//                         "月 " + day +       
//                         "日 " + hour +       
//                         "时 " + minute +       
//                         "分 " + sec +       
//                         "秒");  
return year + 
"- " + month +       
"-" + day +       
"\t " + hour + 
":" + minute +       
":" + sec;
}

public static int getyear(){
Time time = new Time("GMT+8");       
    time.setToNow();      
    int year = time.year;      
    int month = time.month;      
    int day = time.monthDay;      
    int minute = time.minute;      
    int hour = time.hour;      
    int sec = time.second;      
//     myTextView.setText("当前时间为:" + year +       
//                         "年 " + month +       
//                         "月 " + day +       
//                         "日 " + hour +       
//                         "时 " + minute +       
//                         "分 " + sec +       
//                         "秒");  
return year;
}
public static long timetool(long day){
long ll=(long) (day*24*60*60*1000.0f);
System.out.println("day="+day+"时间戳="+ll);
return ll;
}
/**
* 获取当前时间
*/
public static String getnowtime(){
return trans_time( (long) (System.currentTimeMillis()/1000f));
}
/**
* 获取当前时间戳
*/
public static long getnowtime_long(){
return  (long) (System.currentTimeMillis()/1000f);
}
/**
* 将时间转戳换成字符串
*/
public static String trans_time(long time){
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
long lcc_time = Long.valueOf(time); 
String re_StrTime = sdf.format(new Date(time * 1000L));  
return re_StrTime; 

catch (Exception e) {
return ""; 
}
}
/**
*返回day天前的日期
*/
public static String daybefor(long day){
long time1=(long) (System.currentTimeMillis()-timetool(day));//当前时间减去day天的时间
return trans_time((long) (time1/1000f));
}


/**
*时间转换成时间戳
*/
public static long  getTime(String user_time) {
String re_time = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d;
try {
d = sdf.parse(user_time);
long l = d.getTime();
String str = String.valueOf(l);
re_time = str.substring(0, 10);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


return Integer.parseInt(re_time);
}
/**
*时间转换成时间戳
*/
public static long  getTime1(String user_time) {
String re_time = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d;
try {
d = sdf.parse(user_time);
long l = d.getTime();
String str = String.valueOf(l);
re_time = str.substring(0, 10);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


return Integer.parseInt(re_time);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值