package net.sc.common.util;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
/**
* @Author Aaron
* @CreateTime 2014-5-12上午11:39:54
* @Version 1.0
* @Desc 公用的时间处理的类
*/
public class TimeUtil {
public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public static synchronized String executeTimeNow(long startTime) {
return TimeUtil.executeTime(System.currentTimeMillis() - startTime);
}
public static synchronized String executeTime(long spendTime) {
long spendMilliSencond = spendTime % 1000;
long second = spendTime / 1000;
long spendSecond = second % 60;
long minute = second / 60;
long spendMinute = minute % 60;
long hour = minute / 60;
long spendHour = hour % 24;
long day = hour / 24;
return day + "天" + spendHour + "时" + spendMinute + "分" + spendSecond + "秒" + spendMilliSencond + "毫秒";
}
/**
* 获取当前日期 格式:yyyy-MM-dd
*
* @return
*/
public static synchronized String getCurrentDate() {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateNowStr = sdf.format(d);
return dateNowStr;
}
public static synchronized List<String> getFiveYears() {
List<String> yearList = new ArrayList<>();
long nowTime = System.currentTimeMillis();
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis(nowTime);
gc.set(Calendar.DAY_OF_MONTH, 1);
gc.set(Calendar.MONDAY, 0);
String stopDate = sdf.format(gc.getTime());
yearList.add(stopDate + ";" + sdf.format(new Date()));
for (int i = 1; i <= 5; i++) {
gc.set(Calendar.YEAR, gc.get(Calendar.YEAR) - 1);
yearList.add(sdf.format(gc.getTime()) + ";" + stopDate);
stopDate = sdf.format(gc.getTime());
}
return yearList;
}
public static void main(String args[]) {
for (String year : getFiveYears()) {
System.out.println(year.split(";")[0] +" "+ year.split(";")[1]);
}
}
}