package com.hellogame.platform.util;
?
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
?
import com.hellogame.platform.platformbean.WeekDaysData;
?
public
class TimeUtils {
?
/**
?* 获取年月日字符串yyyy-MM-dd HH:mm:ss
?* */
public static String getStringTime(long time) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(new Date(time));
return dateString;
}
?
/**
?* 获取昨天日期字符串 yyyyMMdd
?* */
?
public static String getYesterdayString() {
Date date = new Date();// 取时间
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(Calendar.DATE, -1);// 把日期往后增加一天.整数往后推,负数往前移动
date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
String dateString = formatter.format(date);
return dateString;
}
?
/**
?* 获取yyyy-MM-dd类型日期
?* */
public static String getStringDate(long time) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(new Date(time));
return dateString;
}
?
/**
?* 获取一段日期内的所有日期集合yyyy-MM-dd
?* */
?
public static List<String> getDateList(String startDate, String endDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<String> list = new ArrayList<String>();
try {
Date start = sdf.parse(startDate);
Date end = sdf.parse(endDate);
?
while (start.compareTo(end) <= 0) {
list.add(sdf.format(start));
Calendar calendar = Calendar.getInstance();
calendar.setTime(start);
calendar.add(Calendar.DATE, 1);
start = calendar.getTime();
}
?
} catch (ParseException e1) {
e1.printStackTrace();
list.clear();
return list;
}
return list;
}
?
/**
?* 获取整周 日期集合yyyy-MM-dd
?* */
public static List<WeekDaysData> getWhol
eWeeks(String startDay, String endDay) {
List<WeekDaysData> list = new ArrayList<WeekDaysData>();
String dayInList = "";
List<String> dayList = getDateList(startDay, endDay);
for(String day : dayList){
String monday = getMonday(day);
String sunday = getSunday(day);
if(!dayInList.contains(monday)){
WeekDaysData weekDaysData = new WeekDaysData();
weekDaysData.setStartTime(monday);
weekDaysData.setEndTime(sunday);
list.add(weekDaysData);
dayInList += monday+",";
}
}
return list;
}
?
/**
?* 获取整月 日期集合yyyy-MM
?* */
public static List<WeekDaysData> getWholeMonths(String startDay, String endDay) {
List<WeekDaysData> list = new ArrayList<WeekDaysData>();
String dayInList = "";
List<String> dayList = getDateList(startDay, endDay);
for(String day : dayList){
String monty = day.substring(0, 7);
if(!dayInList.contains(monty)){
WeekDaysData weekDaysData = new WeekDaysData();
weekDaysData.setMonthTime(monty);
list.add(weekDaysData);
dayInList += monty+",";
}
}
return list;
}
?
/**
?* 获取前三日 日期集合yyyy-MM-dd包含当天
?* */
public static List<WeekDaysData> get
ThreeDays(String startDay, String endDay) {
List<WeekDaysData> list = new ArrayList<WeekDaysData>();
List<String> dayList = getDateList(startDay, endDay);
for(String day : dayList){
String preDay = getPreOrAfterDayTime(day, -2);
WeekDaysData weekDaysData = new WeekDaysData();
weekDaysData.setStartTime(preDay);
weekDaysData.setEndTime(day);
list.add(weekDaysData);
}
return list;
}
?
/**
?* 获取前七日 日期集合yyyy-MM-dd包含当天
?* */
public static List<WeekDaysData> getSevenDays(String startDay, String endDay) {
List<WeekDaysData> list = new ArrayList<WeekDaysData>();
List<String> dayList = getDateList(startDay, endDay);
for(String day : dayList){
String preDay = getPreOrAfterDayTime(day, -6);
WeekDaysData weekDaysData = new WeekDaysData();
weekDaysData.setStartTime(preDay);
weekDaysData.setEndTime(day);
list.add(weekDaysData);
}
return list;
}
?
/**
?* 获取前三十 日期集合yyyy-MM-dd包含当天
?* */
public static List<WeekDaysData> getThirtyDays(String startDay, String endDay) {
List<WeekDaysData> list = new ArrayList<WeekDaysData>();
List<String> dayList = getDateList(startDay, endDay);
for(String day : dayList){
String preDay = getPreOrAfterDayTime(day, -29);
WeekDaysData weekDaysData = new WeekDaysData();
weekDaysData.setStartTime(preDay);
weekDaysData.setEndTime(day);
list.add(weekDaysData);
}
return list;
}
?
/**
?* 获取当前日期
周一yyyy-MM-dd
?* */
public static String getMonday(String day){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
try {
cal.setTime(df.parse(day));
} catch (ParseException e) {
e.printStackTrace();
}
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //
获取本周一的日期
return df.format(cal.getTime());
}
/**
?* 获取当前日期
周日yyyy-MM-dd
?* */
public static String getSunday(String day){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
try {
cal.setTime(df.parse(day));
} catch (ParseException e) {
e.printStackTrace();
}
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
cal.add(Calendar.WEEK_OF_YEAR, 1);
return df.format(cal.getTime());
}
?
/**
?* 获取当前日期前后几日日期 yyyy-MM-dd
?* */
public static String getPreOrAfterDayTime(String day,int number){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
try {
cal.setTime(df.parse(day));
} catch (ParseException e) {
e.printStackTrace();
}
cal.add(Calendar.DATE, number);
return df.format(cal.getTime());
}
?
public static void main(String[] args) {
List<WeekDaysData> list = getThirtyDays("2013-04-25", "2013-04-30");
for(WeekDaysData d : list){
System.out.println(d.getStartTime()+":::"+d.getEndTime()+":::"+d.getMonthTime());
}
}
}
package com.hellogame.platform.platformbean;
?
public class WeekDaysData {
private String startTime = "";//yyyy-MM-dd
private String endTime = "";//yyyy-MM-dd
private String monthTime = "";//yyyy-MM
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public String getMonthTime() {
return monthTime;
}
public void setMonthTime(String monthTime) {
this.monthTime = monthTime;
}
?
}