时间转换工具类

这是一个Java实现的时间转换工具类,包含将时间戳转化为描述性文字的方法,如今天、昨天、具体日期等,并能处理星期几的显示。适用于处理社交应用中时间显示的需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class TimeUtil {

   /**
    * 把一个时间戳转为描述性的文字
    * 如果时间戳代表的时间是今天14:35 返回“14:35”
    * 如果时间戳代表的时间是昨天14:35 返回“昨天 14:35”
    *
    * 如果时间戳代表的时间是更往前的14:35 返回“xxxx-xx-xx 14:35”
    *
    * @param time 秒值
    * @return
    */
   public static String getTime(long time) {
      String result = "";
      Calendar calendar = Calendar.getInstance();
      int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
      //根据time与当前时间的差距计算差了多少天
      long now = System.currentTimeMillis();//毫秒值
      //计算差了多少天
      int day = (int) (now / 1000 / 3600 / 24 - time / 1000 / 3600 / 24);

      if (week!=0){
         int dayCha = week - day;
         if (day == 0) {
            result = new SimpleDateFormat("HH:mm").format(time);
            return result;
         }
         if (day == 1) {
            result = "昨天 " + new SimpleDateFormat("HH:mm").format(time);
            return result;
         }
         if (day >=2) {
            if (dayCha >= 1 && dayCha <= 6) {
               String weekTime = getWeekTime(time);
               result = weekTime;
               return result;
            }else {
               result = new SimpleDateFormat("yyyy-MM-dd").format(time);
               return result;
            }
         }
      }else {
         //周日
         week=7;
         int dayCha = week - day;
         if (day == 0) {
            result = new SimpleDateFormat("HH:mm").format(time);
            return result;
         }
         if (day == 1) {
            result = "昨天 " + new SimpleDateFormat("HH:mm").format(time);
            return result;
         }
         if (day >=2) {
            if (dayCha >= 1 && dayCha <= 6) {
               String weekTime = getWeekTime(time);
               result = weekTime;
               return result;
            }else {
               result = new SimpleDateFormat("yyyy-MM-dd").format(time);
               return result;
            }
         }

      }

       return result;

   }

   private static String getWeekTime(long date) {
      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
      String time = df.format(date);

      String Week = "";
      Calendar c = Calendar.getInstance();
      try {
         c.setTime(df.parse(time));
         } catch (ParseException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
         }
      int i = c.get(Calendar.DAY_OF_WEEK);

      if (i== 1) {
         Week += "星期天";
         }
      if (i ==2) {
         Week += "星期一";
         }
      if (i ==3) {
         Week += "星期二";
         }
      if (i == 4) {
         Week += "星期三";
         }
      if (i == 5) {
         Week += "星期四";
         }
      if (i == 6) {
         Week += "星期五";
         }
      if (i == 7) {
         Week += "星期六";
         }
      return Week;

   }

   public static String getMyTime(long time){
      Calendar calendar=Calendar.getInstance();
      String result = "";
      int week =calendar.get(Calendar.DAY_OF_WEEK);
      //根据time与当前时间的差距计算差了多少天
      long now = System.currentTimeMillis();//毫秒值
      //计算差了多少天
      int day = (int)(now/1000/3600/24-time/1000/3600/24);

      switch (day) {
         case 0:
            result = new SimpleDateFormat("HH:mm").format(time);
            break;
         case 1:
            result = "昨天 "+new SimpleDateFormat("HH:mm").format(time);
            break;
         case 2:
         case 3:
         case 4:
         case 5:
         case 6:
            //result = "前天 "+new SimpleDateFormat("HH:mm").format(time*1000);
            String weekTime = getWeekTime(time);
            result=weekTime;
            //显示周几
            break;
         default:
            result = new SimpleDateFormat("yyyy-MM-dd").format(time);
            break;
      }

      return result;

   }





   public static String getTimestr(Date date){
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
      String time = format.format(date);
      return time;
   }
   public static String getTimeStrWithSec(Date date){
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String time = format.format(date);
      return time;
   }
   public static Date stringToDate(String dateString) {
      ParsePosition position = new ParsePosition(0);
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
      Date dateValue = simpleDateFormat.parse(dateString, position);
      return dateValue;
   }
   public static long stringToLong(String dateString) {
      ParsePosition position = new ParsePosition(0);
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
      Date dateValue = simpleDateFormat.parse(dateString, position);
      return dateValue.getTime();
   }
   public static long getDateTime(String sTime){
      SimpleDateFormat format= new SimpleDateFormat("yyyy.MM");
      String time=sTime;
      Date date= null;
        try {
            date = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (date!=null){
            return date.getTime();
        }
        System.out.print("Format To times:"+date.getTime());
        return date.getTime();
   }


   public static long getDateTimeForXx(String sTime){
      SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String time=sTime;
      Date date= null;
      try {
         date = format.parse(time);
      } catch (ParseException e) {
         e.printStackTrace();
      }
      if (date!=null){
         return date.getTime();
      }
      System.out.print("Format To times:"+date.getTime());
      return date.getTime();
   }
}

对于时间的处理,在社交类的app需求最多。



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值