package com.gist.report.chart.show;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Iterator;
public class TT {
//按日分割时间段
public static List<String> getTimeWeaveByDay(Date minDate, Date maxDate){
List<String> list = new ArrayList<String>();
Calendar minc = Calendar.getInstance();
Calendar maxc = Calendar.getInstance();
minc.setTime(minDate);
maxc.setTime(maxDate);
list.add(minc.get(Calendar.DATE)+"/"+(minc.get(Calendar.MONTH)+1)+"/"+minc.get(Calendar.YEAR)+"");
minc.add(Calendar.DATE, 1);
while(minc.before(maxc)){
list.add(minc.get(Calendar.DATE)+"/"+(minc.get(Calendar.MONTH)+1)+"/"+minc.get(Calendar.YEAR)+"");
minc.add(Calendar.DATE, 1);
}
list.add(minc.get(Calendar.DATE)+"/"+(minc.get(Calendar.MONTH)+1)+"/"+minc.get(Calendar.YEAR)+"");
return list;
}
//按月分割时间段
public static List<String> getTimeWeaveByMonth(Date minDate, Date maxDate){
List<String> list = new ArrayList<String>();
Calendar minc = Calendar.getInstance();
Calendar maxc = Calendar.getInstance();
minc.setTime(minDate);
maxc.setTime(maxDate);
list.add((minc.get(Calendar.MONTH)+1)+"/"+minc.get(Calendar.YEAR)+"");
minc.add(Calendar.MONTH, 1);
while(minc.before(maxc)){
list.add((minc.get(Calendar.MONTH)+1)+"/"+minc.get(Calendar.YEAR)+"");
minc.add(Calendar.MONTH, 1);
}
list.add((minc.get(Calendar.MONTH)+1)+"/"+minc.get(Calendar.YEAR)+"");
return list;
}
//按年分割时间段
public static List<String> getTimeWeaveByYear(Date minDate, Date maxDate){
int splitValue = 0;
List<String> list = new ArrayList<String>();
Calendar minc = Calendar.getInstance();
Calendar maxc = Calendar.getInstance();
minc.setTime(minDate);
maxc.setTime(maxDate);
list.add(minc.get(Calendar.YEAR)+"");
minc.add(Calendar.YEAR, 1);
while(minc.before(maxc)){
list.add(minc.get(Calendar.YEAR)+"");
minc.add(Calendar.YEAR, 1);
}
list.add(minc.get(Calendar.YEAR)+"");
return list;
}
//格式化日期
public static List<String> formatDate(List<String> dates){
List<String> list = new ArrayList<String>();
list.addAll(dates);
for(int i=0; i<list.size(); i++){
String [] s = list.get(i).split("/");
if(s.length == 1){
list.set(i, "01/01/"+s[0]);
}else if(s.length == 2){
list.set(i, "01/"+s[0]+"/"+s[1]);
}
}
return list;
}
//获得起始日期
public static Map<String,String[]> getStartEndGroup(List<String> dates){
// TreeMap<String, String[]> map = new TreeMap<String, String[]>(new Comparator<String>(){
//
// @Override
// public int compare(String o1, String o2) {
// // TODO Auto-generated method stub
// SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
// Date d1 = null;
// Date d2 = null;
// try {
// d1 = sdf.parse(o1);
// d2 = sdf.parse(o2);
// } catch (ParseException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// if(d1.after(d2)){
// return 1;
// }
// return 0;
// }
// });
Map<String, String[]> map = new TreeMap<String, String[]>();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date d = null;
String [] startAndEnd = new String [2];
List<String> list = new ArrayList<String>();
List<String> cloneList = new ArrayList<String>();
List<String> years = new ArrayList<String>();
List<String> months = new ArrayList<String>();
cloneList.addAll(dates);
for(int i=0; i<cloneList.size(); i++){
String [] s = cloneList.get(i).split("/");
if(!years.contains(s[2])){
years.add(s[2]);
}
if(!months.contains(s[1])){
months.add(s[1]);
}
}
if(years.size()>1){//按年
for(String year : years){
for(int i=0; i<cloneList.size(); i++){
String [] s = cloneList.get(i).split("/");
try {
d = sdf.parse(cloneList.get(i));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(year.equals(s[2])){
map.put(cloneList.get(i), getStartAndEnd(d,"year"));
}
}
}
}else if(months.size()>1){//按月
for(String month : months){
for(int i=0; i<cloneList.size(); i++){
String [] s = cloneList.get(i).split("/");
try {
d = sdf.parse(cloneList.get(i));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(month.equals(s[1])){
map.put(cloneList.get(i), getStartAndEnd(d,"month"));
}
}
}
}else{ //按日
for(int i=0; i<cloneList.size(); i++){
String [] s = cloneList.get(i).split("/");
map.put(cloneList.get(i), new String[]{cloneList.get(i),cloneList.get(i)});
}
}
// Set set = map.entrySet();
// java.util.Map.Entry entries[] = (java.util.Map.Entry[])set.toArray(new java.util.Map.Entry[set.size()]);
// Arrays.sort(entries, new Comparator() {
// public int compare(Object arg0, Object arg1){
// SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
// Date d1 = null;
// Date d2 = null;
// try {
//
// String s1 = ((Map.Entry)arg0).getKey().toString();
// String s2 = ((Map.Entry)arg1).getKey().toString();
// System.out.println(s1+" / " + s2);
// d1 = sdf.parse(s1);
// d2 = sdf.parse(s2);
// } catch (ParseException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// int i=0;
// if(d1.after(d2)){
// i=1;
// }
// //System.out.println(i);
// return i;
// }
// });
return map;
}
//获得起始日期
public static String [] getStartAndEnd(Date date, String type){
String [] sae = new String[2];
Calendar c = Calendar.getInstance();
SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
c.setTime(date);
String start = "";
String end = "";
if(type.equals("year")){ //按年
start = "01/01/"+c.get(Calendar.YEAR);
c.set(Calendar.MONTH, 11);
end = c.getActualMaximum(Calendar.DAY_OF_MONTH)+"/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.YEAR);
sae[0] = start;
sae[1] = end;
}else if(type.equals("month")){ //按月
start = "01/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.YEAR);
end = c.getActualMaximum(Calendar.DAY_OF_MONTH)+"/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.YEAR);
sae[0] = start;
sae[1] = end;
}else if(type.equals("day")){ //按日
start = c.get(Calendar.DATE)+"/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.YEAR);
end = c.get(Calendar.DATE)+"/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.YEAR);
sae[0] = start;
sae[1] = end;
}
return sae;
}
//获得起始日期
public static String [] getStartAndEnd(Date date){
String [] sae = new String[2];
Calendar c = Calendar.getInstance();
SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
c.setTime(date);
String start = "01/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.YEAR);
String end = c.getActualMaximum(Calendar.DAY_OF_MONTH)+"/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.YEAR);
sae[0] = start;
sae[1] = end;
return sae;
}
//获得中文或英文的月份号
public static String getMonth(Date date, int type){
String [] chiMonths = {"","一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
String [] engMonths = {"","JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"};
String [] simMonths = {"","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
Calendar c = Calendar.getInstance();
String s = "";
if(type == 1)
s = chiMonths[c.get(Calendar.MONTH)+1];
else if(type == 2)
s = engMonths[c.get(Calendar.MONTH)+1];
else
s = simMonths[c.get(Calendar.MONTH)+1];
return s;
}
//获得中文或英文的星期号
public static String getWeek(Date date, int type){
Calendar c = Calendar.getInstance();
String [] chiWeeks = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
String [] engWeeks = {"","SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY"};
String [] simWeeks = {"","Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
c.setTime(date);
String s = "";
if(type == 1)
s = chiWeeks[c.get(Calendar.DAY_OF_WEEK)];
else if(type ==2)
s = engWeeks[c.get(Calendar.DAY_OF_WEEK)];
else
s = simWeeks[c.get(Calendar.DAY_OF_WEEK)];
return s;
}
//得到当前日期的明天或昨天
public static long caculateResetTime(long currentTimestamp, int type){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(currentTimestamp);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.roll(Calendar.DATE, type);
return cal.getTimeInMillis();
}
//得到当前日期的明天或昨天
public static long caculateResetTimestamp(long currentTimestamp, int type){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(currentTimestamp);
cal.roll(Calendar.DATE, type);
return cal.getTimeInMillis();
}
//相差年数
public static int getYears(Date date_1, Date date_2){
int year = 0;
Calendar littleC = Calendar.getInstance(); //小的日期
littleC.setTime(date_1);
Calendar greatC = Calendar.getInstance(); //大的日期
greatC.setTime(date_2);
if(date_1.after(date_2)){
greatC.setTime(date_1);
littleC.setTime(date_2);
}else if(date_1.before(date_2)){
greatC.setTime(date_2);
littleC.setTime(date_1);
}
littleC.add(Calendar.YEAR, 1);
while(greatC.after(littleC)){
littleC.add(Calendar.YEAR, 1);
year++;
}
return year;
}
//相差月数
public static int getMonths(Date date1, Date date2){
int iMonth = 0;
int flag = 0;
try{
Calendar objCalendarDate1 = Calendar.getInstance();
objCalendarDate1.setTime(date1);
Calendar objCalendarDate2 = Calendar.getInstance();
objCalendarDate2.setTime(date2);
if (objCalendarDate2.equals(objCalendarDate1))
return 0;
if (objCalendarDate1.after(objCalendarDate2)){
Calendar temp = objCalendarDate1;
objCalendarDate1 = objCalendarDate2;
objCalendarDate2 = temp;
}
if (objCalendarDate2.get(Calendar.DAY_OF_MONTH) < objCalendarDate1.get(Calendar.DAY_OF_MONTH))
flag = 1;
if (objCalendarDate2.get(Calendar.YEAR) > objCalendarDate1.get(Calendar.YEAR))
iMonth = ((objCalendarDate2.get(Calendar.YEAR) - objCalendarDate1.get(Calendar.YEAR))
* 12 + objCalendarDate2.get(Calendar.MONTH) - flag)
- objCalendarDate1.get(Calendar.MONTH);
else
iMonth = objCalendarDate2.get(Calendar.MONTH)
- objCalendarDate1.get(Calendar.MONTH) - flag;
} catch (Exception e){
e.printStackTrace();
}
return iMonth;
}
//相差天数
public static int getDays2(Date g1, Date g2) {
int elapsed = 0;
Calendar gc1 = Calendar.getInstance();
Calendar gc2 = Calendar.getInstance();
Date d1, d2;
d1 = (Date) g1.clone();
d2 = (Date) g2.clone();
if (g2.after(g1)) {
gc2.setTime(d2);
gc1.setTime(d1);
} else {
gc2.setTime(d1);
gc1.setTime(d2);
}
gc1.clear(Calendar.MILLISECOND);
gc1.clear(Calendar.SECOND);
gc1.clear(Calendar.MINUTE);
gc1.clear(Calendar.HOUR_OF_DAY);
gc2.clear(Calendar.MILLISECOND);
gc2.clear(Calendar.SECOND);
gc2.clear(Calendar.MINUTE);
gc2.clear(Calendar.HOUR_OF_DAY);
while (gc1.before(gc2)) {
gc1.add(Calendar.DATE, 1);
elapsed++;
}
return elapsed;
}
//相差天数
public static int getDays(GregorianCalendar g1, GregorianCalendar g2) {
int elapsed = 0;
GregorianCalendar gc1, gc2;
if (g2.after(g1)) {
gc2 = (GregorianCalendar) g2.clone();
gc1 = (GregorianCalendar) g1.clone();
} else {
gc2 = (GregorianCalendar) g1.clone();
gc1 = (GregorianCalendar) g2.clone();
}
gc1.clear(Calendar.MILLISECOND);
gc1.clear(Calendar.SECOND);
gc1.clear(Calendar.MINUTE);
gc1.clear(Calendar.HOUR_OF_DAY);
gc2.clear(Calendar.MILLISECOND);
gc2.clear(Calendar.SECOND);
gc2.clear(Calendar.MINUTE);
gc2.clear(Calendar.HOUR_OF_DAY);
while (gc1.before(gc2)) {
gc1.add(Calendar.DATE, 1);
elapsed++;
}
return elapsed;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
// try {
// Date d = sdf.parse("2010/04/12");
// Date d2 = sdf.parse("2010/04/17");
// Calendar c = Calendar.getInstance();
// Calendar c2 = Calendar.getInstance();
// c.setTime(d);
// c2.setTime(d2);
// if(d.getTime()>d2.getTime()){
// System.out.println("大于");
// }else{
// System.out.println("小于");
// }
// System.out.println(d.getDate());
// } catch (ParseException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
String [] timeArrys = {"2010/2/22","2010/3/3","2010/4/21","2010/03/1","2010/1/28","2010/02/16","2010/4/09","2010/02/06"};
Date maxDate = null;
Date minDate = null;
SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd");
//日号与月号补零
for(int i=0; i<timeArrys.length; i++){
String [] a = timeArrys[i].split("/");
if(a[1].length()==1){
a[1]="0"+a[1];
}
if(a[2].length()==1){
a[2]="0"+a[2];
}
timeArrys[i]=a[0]+"/"+a[1]+"/"+a[2];
}
Date [] dates = new Date[timeArrys.length];
List dateList = Arrays.asList(timeArrys);
//利用List排序获得最大值和最小值
Collections.sort(dateList);
try{
maxDate = sf.parse(dateList.get(dateList.size()-1)+"");
minDate = sf.parse(dateList.get(0)+"");
}catch(ParseException e){
}
System.out.println("利用List排序获得最大值和最小值: ");
System.out.println(dateList);
System.out.println("最大值:"+ maxDate.toLocaleString());
System.out.println("最小值: "+ minDate.toLocaleString());
//使用TreeSe排序
for(int i=0; i<dateList.size(); i++){
try {
dates[i] = sf.parse(timeArrys[i]);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
SortedSet<Date> sset = new TreeSet<Date>(Arrays.asList(dates));
maxDate = sset.last();
minDate = sset.first();
System.out.println("使用TreeSe排序: ");
System.out.println("最大值:"+ maxDate.toLocaleString());
System.out.println("最小值: "+ minDate.toLocaleString());
//使用自定义排序方法
List<Date> dList = new ArrayList<Date>(Arrays.asList(dates));
Collections.sort(dList,new Comparator<Date>(){
@Override
public int compare(Date o1, Date o2) {
// TODO Auto-generated method stub
if(o1.after(o2)){
return 1;
}
return 0;
}
});
maxDate = dList.get(dList.size()-1);
minDate = dList.get(0);
System.out.println("使用自定义排序方法: ");
System.out.println("最大值:"+ maxDate.toLocaleString());
System.out.println("最小值: "+ minDate.toLocaleString());
int years = getYears(maxDate, minDate);
System.out.println("相差年数:" + years);
int result = 0;
Date d = null;
Date d2 = null;
try {
d = sdf.parse("2010/02/16");
d2 = sdf.parse("2010/02/17");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
GregorianCalendar gc1 = new GregorianCalendar();
GregorianCalendar gc2 = new GregorianCalendar();
gc1.setTime(d);
gc2.setTime(d2);
result = getDays2(maxDate, minDate);
System.out.println("相差天数: "+result);
result = getMonths(d,d2);
System.out.println("相差月数: "+result);
Calendar cc = Calendar.getInstance();
cc.setTime(d);
//得到该月最大的天数
System.out.println(cc.get(Calendar.YEAR)+"/"+(cc.get(Calendar.MONTH)+1)+"/"+cc.get(Calendar.DATE)+"该月有: "+cc.getActualMaximum(Calendar.DAY_OF_MONTH)+" 天");
// long timestamp = caculateResetTimestamp(new Date().getTime(),1);
// long timestamp2 = caculateResetTimestamp(new Date().getTime(), -1);
// Date tomorrow = new Date(timestamp);
// Date yesterday = new Date(timestamp2);
// System.out.println("明天日期:" + tomorrow.toLocaleString());
// System.out.println("昨天日期:" + yesterday.toLocaleString());
//
// long timeTomorrom = caculateResetTime(new Date().getTime(),1);
// long timeYesterday = caculateResetTime(new Date().getTime(), -1);
// Date tomorrowReset = new Date(timeTomorrom);
// Date yesterdayReset = new Date(timeYesterday);
// System.out.println("明天起始日期:" + tomorrowReset.toLocaleString());
// System.out.println("昨天起始日期:" + yesterdayReset.toLocaleString());
//
// String week = getWeek(new Date(), 1);
// System.out.println("当前日期的中文星期号是:" + week );
// week = getWeek(new Date(),0);
// System.out.println("当前日期的英文星期号是:" + week );
//
// String month = getMonth(new Date(), 1);
// System.out.println("当前日期的中文月份号是:" + month );
// month = getMonth(new Date(),0);
// System.out.println("当前日期的英文月份号是:" + month );
// String [] sae = getStartAndEnd(new Date());
// System.out.println("当前时间的第一天和最后一天:"+ sae[0]+" : " + sae[1]);
try {
d = sdf.parse("2005/02/16");
d2 = sdf.parse("2010/02/17");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("按年分割时间段:" + getTimeWeaveByYear(d,d2));
System.out.println("格式化日期: " + formatDate(getTimeWeaveByYear(d,d2)));
Map<String, String[]> map = getStartEndGroup(formatDate(getTimeWeaveByYear(d,d2)));
// Set set = map.entrySet();
// java.util.Map.Entry entries[] = (java.util.Map.Entry[])set.toArray(new java.util.Map.Entry[set.size()]);
// Arrays.sort(entries, new Comparator() {
// public int compare(Object arg0, Object arg1){
// SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
// Date d1 = null;
// Date d2 = null;
// try {
//
// String s1 = ((Map.Entry)arg0).getKey().toString();
// String s2 = ((Map.Entry)arg1).getKey().toString();
// System.out.println(s1+" / " + s2);
// d1 = sdf.parse(s1);
// d2 = sdf.parse(s2);
// } catch (ParseException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// int i=0;
// if(d1.after(d2)){
// i=1;
// }
// System.out.println(i);
// return i;
// }
// });
Set<String> st = map.keySet();
for(Iterator<String> it = st.iterator(); it.hasNext(); ){
String key = it.next();
String [] ss = map.get(key);
System.out.println("2222222key: " + key + " value: " + ss[0]+" : " + ss[1]);
}
try {
d = sdf.parse("2010/02/16");
d2 = sdf.parse("2010/09/17");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("按月分割时间段:" + getTimeWeaveByMonth(d,d2));
System.out.println("格式化日期: " + formatDate(getTimeWeaveByMonth(d,d2)));
Map<String, String[]> map2 = getStartEndGroup(formatDate(getTimeWeaveByMonth(d,d2)));
Set<String> set2 = map2.keySet();
for(Iterator<String> it = set2.iterator(); it.hasNext(); ){
String key = it.next();
String [] ss = map2.get(key);
System.out.println("key: " + key + " value: " + ss[0]+" : " + ss[1]);
}
try {
d = sdf.parse("2010/02/16");
d2 = sdf.parse("2010/02/28");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("按天分割时间段:" + getTimeWeaveByDay(d,d2));
System.out.println("格式化日期: " + formatDate(getTimeWeaveByDay(d,d2)));
Map<String, String[]> map3 = getStartEndGroup(formatDate(getTimeWeaveByDay(d,d2)));
Set<String> set3 = map3.keySet();
for(Iterator<String> it = set3.iterator(); it.hasNext(); ){
String key = it.next();
String [] ss = map3.get(key);
System.out.println("key: " + key + " value: " + ss[0]+" : " + ss[1]);
}
Calendar cccc = Calendar.getInstance();
cccc.setTime(new Date());
cccc.set(Calendar.MONTH, 3);
System.out.println(cccc.getActualMaximum(Calendar.DAY_OF_MONTH));
String [] sss = getStartAndEnd(new Date(), "day");
for(String s : sss){
System.out.println(s);
}
}
}