日期:2014-05-17 浏览次数:21094 次
package com.study.util; import java.text.SimpleDateFormat; import java.util.Date; public class DateUtil { public static void main(String[] args) { Date d = new Date(); long i = d.getTime() - 10000000; System.out.println(getBefore(new Date(i))); } public static String getBefore(Date date){ long old = date.getTime(); long now = (new Date()).getTime(); long re = (now - old) / (1000 * 60);//分钟 long days = re / (24 * 60); StringBuffer sb = new StringBuffer(); if(days > 1){ SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 hh:mm:ss"); sb.append(sdf.format(date)); }else if(days == 1){ SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss"); sb.append("昨天").append(sdf.format(date)); }else{ re = re % (24 * 60); long hours = re / 60; long mins = re % 60; if(hours != 0){ sb.append(hours).append("小时"); } if(mins != 0){ sb.append(mins).append("分钟以前"); } } return sb.toString(); } }
------解决方案--------------------
import java.text.SimpleDateFormat; import java.util.Date; public class MyDate { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(new ChsDate(new Date())); System.out.println(new ChsDate(sdf.parse("2012-09-27 15:58:30"))); System.out.println(new ChsDate(sdf.parse("2012-09-27 15:50:00"))); System.out.println(new ChsDate(sdf.parse("2012-09-27 10:50:00"))); System.out.println(new ChsDate(sdf.parse("2012-08-27 10:50:00"))); } } class ChsDate { private static String NOW = "现在"; private static String HOURS_AGO = "小时前"; private static String MINUTES_AGO = "分钟前"; private static String SECONDS_AGO = "秒前"; private static long SECOND = 1000L; private static long MINUTE = 60 * 1000L; private static long HOUR = 60 * 60 * 1000L; private static long DAY = 24 * 60 * 60 * 1000L; private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); private Date date; public ChsDate(Date date) { this.date = date; } @Override public String toString() { long c = new Date().getTime(); long p = this.date.getTime(); if (c - p > DAY) { return sdf.format(date); } else if (c - p > HOUR) { return (c - p) / HOUR + HOURS_AGO; } else if (c - p > MINUTE) { return (c - p) / MINUTE + MINUTES_AGO; } else if (c - p > SECOND) { return (c - p) / SECOND + SECONDS_AGO; } return NOW; } }
------解决方案--------------------
package zl.framework.util.tool; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import zl.framework.entity.Settled; /** * 处理日期的工具类 * @version 2.0 */ public class DateUtil { private static DateUtil bean = new DateUtil(); /** * 单例模式获得bean * @return DateUtil */ public static Date