TimeUtil

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

public class TimeUtil {

	public static String converTime(long timestamp) {

		long currentSeconds = System.currentTimeMillis() / 1000;
		long timeGap = currentSeconds - timestamp;// 与现在时间相差秒数

		String timeStr = null;
		if (timeGap > 24 * 60 * 60) {// 1天以上
			timeStr = timeGap / (24 * 60 * 60) + "天前";
		} else if (timeGap > 60 * 60) {// 1小时-24小时
			timeStr = timeGap / (60 * 60) + "小时前";
		} else if (timeGap > 60) {// 1分钟-59分钟
			timeStr = timeGap / 60 + "分钟前";
		} else {// 1秒钟-59秒钟
			timeStr = "刚刚";
		}
		return timeStr;
	}

	public static boolean converForecastTime(String timestamp) {
		long ForecastTime = Long.valueOf(timestamp);
		long currentSeconds = System.currentTimeMillis() / 1000;
		long timeGap = ForecastTime - currentSeconds;// 与现在时间相差秒数
		if (timeGap > 60*30) {
			return true;
		}
		return false;
	}
	
	public static Date getDateFromStamp(long timestamp) {
		Date date = new Date(timestamp * 1000);
		return date;
	}

	public static String getForecastTime(String timestamp) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 HH:mm");
		Date date = new Date(Long.valueOf(timestamp) * 1000);
		sdf.format(date);
		return sdf.format(date);
	}

	public static String getMsgTime(long timestamp) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 HH:mm");
		Date date = new Date(timestamp * 1000);
		sdf.format(date);
		return sdf.format(date);
	}
	
	public static String getStandardTime(long timestamp) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
		Date date = new Date(timestamp * 1000);
		sdf.format(date);
		return sdf.format(date);
	}
//	2012-01-02 01:47:17.0
	public static String getLoginTime(String LoginTime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.0");
		try {
			String time = converTime(sdf.parse(LoginTime).getTime()/1000);
			return "上次登陆:"+time;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "这家伙很懒。";
	}
	
	public static String getYearAndMouthOfDate(long timestamp) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date date = new Date(timestamp * 1000);
		sdf.format(date);
		return sdf.format(date);
	}
	
	public static String getDayOfDate(long timestamp) {
		SimpleDateFormat sdf = new SimpleDateFormat("d");
		Date date = new Date(timestamp * 1000);
		sdf.format(date);
		return sdf.format(date);
	}
	public static String getWeekOfDate(long timestamp) {
		Date dt = new Date(timestamp*1000);
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }
}


你可能感兴趣的:(TimeUtil)