码迷,mamicode.com
首页 > 编程语言 > 详细

java高仿微博日期显示格式化,日期辅助工具类

时间:2014-09-07 13:36:19      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:java   时间   工具   date   日期   

原文:java高仿微博日期显示格式化,日期辅助工具类

源代码下载地址:http://www.zuidaima.com/share/1550463377902592.htm

仿新浪微博日期格式化工具类:

例如:

1. 刚刚

2. xx分钟前

3. hh小时前

4. 今天 HH:mm

5. 昨天 HH:mm

6. 前天 HH:mm

7. 上个月 yyyy-MM-dd HH:mm

8. 去年 yyyy-MM-dd HH:mm

9. 前年 yyyy-MM-dd HH:mm

今天写的代码,结构还很粗糙,只是实现了功能而已。下次重构下支持格式描述符之后再发上来

package com.zuidaima.haohui.common.utils;

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

/**
 * 
 * import java.text.SimpleDateFormat;
 *@author www.zuidaima.com
**/
import java.util.Calendar;
public class Super2{
 public static void main(String args[]){
  Calendar cal = Calendar.getInstance();
  //n为推迟的周数,1本周,-1向前推迟一周,2下周,依次类推
  int n = 2;
  String monday;
  cal.add(Calendar.DATE, n*7);
  //想周几,这里就传几Calendar.MONDAY(TUESDAY...)
  cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
  monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
  System.out.println(monday);
 }
}
 * @project baidamei
 * @author cevencheng <cevencheng@gmail.com>
 * @create 2012-11-10 上午1:12:21
 */
public class DateUtil {
	public static void main(String[] args) throws ParseException {
//		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		java.util.Date begin = dfs.parse("2004-01-02 11:30:24");
//		java.util.Date end = dfs.parse("2004-03-26 13:31:40");
//		long between = (end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒
//
//		long day = between / (24 * 3600);
//		long hour = between % (24 * 3600) / 3600;
//		long minute = between % 3600 / 60;
//		long second = between % 60 / 60;
//		System.out.println("" + day + "天" + hour + "小时" + minute + "分" + second + "秒");
//		System.out.println(begin.getTime());
//		
//		System.out.println(getDaysBeforeNow(new Date()));
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date date = df.parse("2012-11-18");
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -7);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //上周一
		System.out.println(df.format(cal.getTime()));
		cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); //周末
		System.out.println(df.format(cal.getTime()));
		
	}

	/**
	 * 时间间隔计算
	 * 
	 */
	public static String getDaysBeforeNow(Date date) {
		long sysTime = Long.parseLong(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
		long ymdhms = Long.parseLong(new SimpleDateFormat("yyyyMMddHHmmss").format(date));
		String strYear = "年前";
		String strMonth = "月前";
		String strDay = "天前";
		String strHour = "小时前";
		String strMinute = "分钟前";
		try {
			if (ymdhms == 0) {
				return "";
			}
			long between = (sysTime / 10000000000L) - (ymdhms / 10000000000L);
			if (between > 0) {
				return between + strYear;
			}
			between = (sysTime / 100000000L) - (ymdhms / 100000000L);
			if (between > 0) {
				return between + strMonth;
			}
			between = (sysTime / 1000000L) - (ymdhms / 1000000L);
			if (between > 0) {
				return between + strDay;
			}
			between = (sysTime / 10000) - (ymdhms / 10000);
			if (between > 0) {
				return between + strHour;
			}
			between = (sysTime / 100) - (ymdhms / 100);
			if (between > 0) {
				return between + strMinute;
			}
			return "1" + strMinute;
		} catch (Exception e) {
			return "";
		}
	}
}

	    			


java高仿微博日期显示格式化,日期辅助工具类

标签:java   时间   工具   date   日期   

原文地址:http://blog.csdn.net/springmvc_springdata/article/details/39118379

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!