JAVA如何实现留言时间(几秒前,几分钟前,几小时前....)的功能

@toc

简介

最近在web前端需要实现将一个发布或创建日期转换显示为是几秒前,几分钟前…参考来自stackoverflow上的代码:

import java.util.Date;

public class TimeCountUtil {
   
   
   	private static final long ONE_MINUTE = 60000L;  
       private static final long ONE_HOUR = 3600000L;  
       private static final long ONE_DAY = 86400000L;  
       private static final long ONE_WEEK = 604800000L;  
     
       private static final String ONE_SECOND_AGO = "秒前";  
       private static final String ONE_MINUTE_AGO = "分钟前";  
       private static final String ONE_HOUR_AGO = "小时前";  
       private static final String ONE_DAY_AGO = "天前";  
       private static final String ONE_MONTH_AGO = "月前";  
       private static final String ONE_YEAR_AGO = "年前";  
     
       //main方法测试  
       /*public static void main(String[] args) throws ParseException {  
           SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
          
           Date date = format.parse("2019-03-18 13:25:40");  
           System.out.println(format(date));  
       }*/
       
       
       //实际调用方法
       public String timeCount(Date dateTime) {
       	String format = format(dateTime);
       	return format;
       }
       //时间转换  
       public static String format(Date date) {  
           long delta = new Date().getTime() - date.getTime();  
           if (delta < 1L * ONE_MINUTE) {  
               long seconds = toSeconds(delta);  
               return (seconds <= 0 ? 1 : seconds) + ONE_SECOND_AGO;  
           }  
           if (delta < 45L * ONE_MINUTE) {  
               long minutes = toMinutes(delta);  
               return (minutes <= 0 ? 1 : minutes) + ONE_MINUTE_AGO;  
           }  
           if (delta < 24L * ONE_HOUR) {  
               long hours = toHours(delta);  
               return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO;  
           }  
           if (delta < 48L * ONE_HOUR) {  
               return "昨天";  
           }  
           if (delta < 30L * ONE_DAY) {  
               long days = toDays(delta);  
               return (days <= 0 ? 1 : days) + ONE_DAY_AGO;  
           }  
           if (delta < 12L * 4L * ONE_WEEK) {  
               long months = toMonths(delta);  
               return (months <= 0 ? 1 : months) + ONE_MONTH_AGO;  
           } else {  
               long years = toYears(delta);  
               return (years <= 0 ? 1 : years) + ONE_YEAR_AGO;  
           }  
       }  
     
       private static long toSeconds(long date) {  
           return date / 1000L;  
       }  
     
       private static long toMinutes(long date) {  
           return toSeconds(date) / 60L;  
       }  
     
       private static long toHours(long date) {  
           return toMinutes(date) / 60L;  
       }  
     
       private static long toDays(long date) {  
           return toHours(date) / 24L;  
       }  
     
       private static long toMonths(long date) {  
           return toDays(date) / 30L;  
       }  
     
       private static long toYears(long date) {  
           return toMonths(date) / 365L;  
       }  
}

注意:
在这个类中main只是起到了测试作用,但是实际开发时需要涉及到调用问题,所以定义了timeCount()方法用于外部调用,直接外部创建工具类TimeCountUtil 对象,然后调用timeCount()方法即可,此方法传入的参数是Date类型,如果是String类型可以另外的进行格式转换即可。

例如:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {

   public static void main(String[] args) {
   	TimeCountUtil timeCountUtil = new TimeCountUtil();
   	
   	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
       Date date = null;
   	try {
   		date = format.parse("2019-03-18 14:01:40");
   	} catch (ParseException e) {
   		// TODO Auto-generated catch block
   		e.printStackTrace();
   	}
       
   	System.out.println(timeCountUtil.timeCount(date));
   }
}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
以下是Java实现几秒几分钟、几天、几年的源码示例: ```java import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class TimeAgo { public static void main(String[] args) { // 获取当时间 LocalDateTime now = LocalDateTime.now(); // 格式化时间 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = now.format(formatter); // 输出当时间 System.out.println("当时间:" + formattedDateTime); // 计算一秒的时间 LocalDateTime oneSecondAgo = now.minusSeconds(1); String formattedOneSecondAgo = oneSecondAgo.format(formatter); System.out.println("一秒时间:" + formattedOneSecondAgo); // 计算一分钟的时间 LocalDateTime oneMinuteAgo = now.minusMinutes(1); String formattedOneMinuteAgo = oneMinuteAgo.format(formatter); System.out.println("一分钟时间:" + formattedOneMinuteAgo); // 计算一天的时间 LocalDateTime oneDayAgo = now.minusDays(1); String formattedOneDayAgo = oneDayAgo.format(formatter); System.out.println("一天时间:" + formattedOneDayAgo); // 计算一年的时间 LocalDateTime oneYearAgo = now.minusYears(1); String formattedOneYearAgo = oneYearAgo.format(formatter); System.out.println("一年时间:" + formattedOneYearAgo); } } ``` 这个示例程序使用Java 8的`LocalDateTime`类和`DateTimeFormatter`类来实现几秒几分钟、几天、几年时间计算。通过调用`minusSeconds`、`minusMinutes`、`minusDays`和`minusYears`方法来计算一秒、一分钟、一天和一年的时间,并使用`DateTimeFormatter`类将时间格式化为指定的字符串格式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值