Android 时间戳的转换

  在Android应用中,经常会碰到后台的时间是时间戳而现实的需要今天什么时候,昨天什么时候,就像微博的时间显示一样。现在我上一个把时间戳转换的代码:

  public static String getTime(int timestamp) {
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String time = null;
  try {
   java.util.Date currentdate = new java.util.Date();// 当前时间

   long i = (currentdate.getTime() / 1000 - timestamp) / (60);
   Timestamp now = new Timestamp(System.currentTimeMillis());// 获取系统当前时间
   String str = sdf.format(new Timestamp(IntToLong(timestamp)));
   time = str.substring(11, 16);
   String year = str.substring(0, 4);
   String month = str.substring(5, 7);
   String day = str.substring(8, 10);
   System.out.println(str);
   System.out.println(time);
   System.out.println(getDate(year, month, day));
   time = getDate(year, month, day) + time;
  } catch (Exception e) {
   e.printStackTrace();
  }
  return time;
 }

 // java Timestamp构造函数需传入Long型
 public static long IntToLong(int i) {
  long result = (long) i;
  result *= 1000;
  return result;
 }

 public static String getDate(final String year, final String month,
   final String day) {
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 24小时制
  java.util.Date d = new java.util.Date();
  String str = sdf.format(d);
  String nowmonth = str.substring(5, 7);
  String nowday = str.substring(8, 10);
  String result = null;

  int temp = Integer.parseInt(nowday) - Integer.parseInt(day);
  switch (temp) {
  case 0:
   result = "今天";
   break;
  case 1:
   result = "昨天";
   break;
  case 2:
   result = "前天";
   break;
  default:
   StringBuilder sb = new StringBuilder();
   sb.append(year).append("-");
   sb.append(month).append("-");
   sb.append(day).append(" ");
   result = sb.toString();
   break;
  }
  return result;
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值